In [1]:
import numpy as np

In [2]:
np.tanh(1)


Out[2]:
0.76159415595576485

In [3]:
a = np.arange(50).reshape(2,5,5)

In [4]:
a


Out[4]:
array([[[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9],
        [10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19],
        [20, 21, 22, 23, 24]],

       [[25, 26, 27, 28, 29],
        [30, 31, 32, 33, 34],
        [35, 36, 37, 38, 39],
        [40, 41, 42, 43, 44],
        [45, 46, 47, 48, 49]]])

In [7]:
print a.shape


(2L, 5L, 5L)

In [9]:
#change the order as we need 5,2,5 say for example

b = a.transpose(1,0,2)

In [10]:
print b


[[[ 0  1  2  3  4]
  [25 26 27 28 29]]

 [[ 5  6  7  8  9]
  [30 31 32 33 34]]

 [[10 11 12 13 14]
  [35 36 37 38 39]]

 [[15 16 17 18 19]
  [40 41 42 43 44]]

 [[20 21 22 23 24]
  [45 46 47 48 49]]]

In [11]:
print b.shape


(5L, 2L, 5L)

In [12]:
c = b #copying here is just reference copy

In [14]:
c[1,1,1] = 18

In [15]:
b[1,1,1]


Out[15]:
18

In [16]:
b


Out[16]:
array([[[ 0,  1,  2,  3,  4],
        [25, 26, 27, 28, 29]],

       [[ 5,  6,  7,  8,  9],
        [30, 18, 32, 33, 34]],

       [[10, 11, 12, 13, 14],
        [35, 36, 37, 38, 39]],

       [[15, 16, 17, 18, 19],
        [40, 41, 42, 43, 44]],

       [[20, 21, 22, 23, 24],
        [45, 46, 47, 48, 49]]])

In [19]:
d = b.copy() #this is literal copy as we know it

In [20]:
d


Out[20]:
array([[[ 0,  1,  2,  3,  4],
        [25, 26, 27, 28, 29]],

       [[ 5,  6,  7,  8,  9],
        [30, 18, 32, 33, 34]],

       [[10, 11, 12, 13, 14],
        [35, 36, 37, 38, 39]],

       [[15, 16, 17, 18, 19],
        [40, 41, 42, 43, 44]],

       [[20, 21, 22, 23, 24],
        [45, 46, 47, 48, 49]]])

In [21]:
d[1,1,1] = 20

In [22]:
c[1,1,1]


Out[22]:
18

In [25]:
d[-1]


Out[25]:
array([[20, 21, 22, 23, 24],
       [45, 46, 47, 48, 49]])

In [26]:
range(10,0,-1)


Out[26]:
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Sequence to vocab


In [46]:
#from 2 d to 3d
N = 30
T = 20
p = np.zeros((N,T))

In [47]:
np.random.random_sample(30)


Out[47]:
array([ 0.57441317,  0.73944063,  0.01101198,  0.58470261,  0.56516472,
        0.71226917,  0.61581655,  0.20180607,  0.74294943,  0.40559716,
        0.21572442,  0.18058342,  0.34770329,  0.02345066,  0.83316521,
        0.2345284 ,  0.18718154,  0.04277529,  0.02133108,  0.6263475 ,
        0.9309065 ,  0.28536627,  0.87911406,  0.84329321,  0.93762343,
        0.12611862,  0.13058249,  0.05109961,  0.87389639,  0.54155944])

In [48]:
import random

In [49]:
p[np.arange(N)] = [random.sample(range(T),T) for _ in range(N)]

In [51]:
#p

In [52]:
D = 10
W = np.zeros((T,D))

In [53]:
W[np.arange(T)] = [random.sample(range(D),D) for _ in range(T)]

In [54]:
W


Out[54]:
array([[ 0.,  1.,  4.,  8.,  3.,  9.,  2.,  5.,  6.,  7.],
       [ 1.,  8.,  2.,  5.,  0.,  3.,  4.,  7.,  9.,  6.],
       [ 3.,  7.,  0.,  8.,  6.,  5.,  2.,  4.,  1.,  9.],
       [ 6.,  8.,  9.,  7.,  4.,  2.,  5.,  3.,  0.,  1.],
       [ 2.,  9.,  3.,  6.,  5.,  0.,  7.,  1.,  4.,  8.],
       [ 0.,  3.,  2.,  1.,  5.,  8.,  7.,  6.,  4.,  9.],
       [ 4.,  9.,  5.,  1.,  6.,  0.,  3.,  8.,  2.,  7.],
       [ 8.,  9.,  2.,  3.,  0.,  6.,  5.,  7.,  1.,  4.],
       [ 4.,  2.,  0.,  6.,  1.,  9.,  8.,  5.,  3.,  7.],
       [ 0.,  2.,  9.,  6.,  8.,  1.,  4.,  3.,  7.,  5.],
       [ 8.,  5.,  3.,  7.,  2.,  4.,  0.,  6.,  9.,  1.],
       [ 5.,  2.,  3.,  6.,  7.,  0.,  9.,  1.,  8.,  4.],
       [ 2.,  8.,  3.,  7.,  1.,  5.,  0.,  6.,  4.,  9.],
       [ 9.,  3.,  0.,  4.,  1.,  7.,  2.,  5.,  8.,  6.],
       [ 6.,  8.,  9.,  5.,  1.,  3.,  7.,  4.,  2.,  0.],
       [ 0.,  3.,  5.,  6.,  4.,  9.,  1.,  2.,  8.,  7.],
       [ 1.,  3.,  7.,  2.,  6.,  4.,  9.,  8.,  5.,  0.],
       [ 7.,  0.,  6.,  8.,  5.,  1.,  3.,  9.,  4.,  2.],
       [ 7.,  3.,  1.,  8.,  4.,  6.,  9.,  5.,  2.,  0.],
       [ 7.,  9.,  2.,  4.,  0.,  5.,  3.,  1.,  8.,  6.]])

In [56]:
W[p,:]


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-56-3c454ae1556c> in <module>()
----> 1 W[p,:]

IndexError: arrays used as indices must be of integer (or boolean) type

In [58]:
type(W)


Out[58]:
numpy.ndarray

In [59]:
type(p)


Out[59]:
numpy.ndarray

In [60]:
W.shape


Out[60]:
(20L, 10L)

In [62]:
p.shape


Out[62]:
(30L, 20L)

In [69]:
W[p,:] #not sure why it is giving error


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-69-3c454ae1556c> in <module>()
----> 1 W[p,:]

IndexError: arrays used as indices must be of integer (or boolean) type

In [77]:
np.add.at(p,[[0,1],[2,3]],[2,3])

In [79]:
np.add.at(p,[[0,1],[2,3]],np.arange(50).reshape(2,5,5))


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-79-5b3528f45cf7> in <module>()
----> 1 np.add.at(p,[[0,1],[2,3]],np.arange(50).reshape(2,5,5))

ValueError: array is not broadcastable to correct shape

In [ ]: