In [2]:
import numpy as np

In [3]:
x=np.zeros((3,4,5))

1D called vector 2D called matrix 3D nad so on tensor


In [4]:
print x


[[[ 0.  0.  0.  0.  0.]
  [ 0.  0.  0.  0.  0.]
  [ 0.  0.  0.  0.  0.]
  [ 0.  0.  0.  0.  0.]]

 [[ 0.  0.  0.  0.  0.]
  [ 0.  0.  0.  0.  0.]
  [ 0.  0.  0.  0.  0.]
  [ 0.  0.  0.  0.  0.]]

 [[ 0.  0.  0.  0.  0.]
  [ 0.  0.  0.  0.  0.]
  [ 0.  0.  0.  0.  0.]
  [ 0.  0.  0.  0.  0.]]]

In [5]:
type(x)


Out[5]:
numpy.ndarray

In [6]:
y=np.ones((2,3))

In [7]:
print y


[[ 1.  1.  1.]
 [ 1.  1.  1.]]

I need a matrix like this

[[2,3],
         [4,5],
         [6,7]]

In [8]:
z=np.arange(2,8,1)
alpha=np.reshape(z,(3,2))
print alpha


[[2 3]
 [4 5]
 [6 7]]

In [9]:
beta= np.random.randn(3,4)
print beta


[[-0.60655405  0.6175917   1.6962759   1.89094038]
 [ 0.56150403 -1.25588071 -0.58245218  0.87327847]
 [ 0.49423431 -0.30754252 -0.56212643  1.129681  ]]

In [10]:
gamma=beta*2.0
print gamma


[[-1.21310811  1.23518339  3.3925518   3.78188075]
 [ 1.12300805 -2.51176142 -1.16490437  1.74655694]
 [ 0.98846862 -0.61508505 -1.12425287  2.259362  ]]

In [11]:
a=[3,4,5]
a=np.array(a)
type(a)


Out[11]:
numpy.ndarray

np array operator


In [12]:
a=np.random.randint(0,10,(2,3))
b=np.random.randint(0,10,(2,3))
print a
print b


[[2 9 4]
 [1 6 0]]
[[4 6 4]
 [7 1 0]]

In [13]:
print "element-wise addition:\n%s"%(a + b)


element-wise addition:
[[ 6 15  8]
 [ 8  7  0]]

In [14]:
print "element-wise addition:\n%s"%(a * b)


element-wise addition:
[[ 8 54 16]
 [ 7  6  0]]

In [15]:
print a
print b.T
print '-----'
print np.dot(a,b.T)


[[2 9 4]
 [1 6 0]]
[[4 7]
 [6 1]
 [4 0]]
-----
[[78 23]
 [40 13]]

Sliccing


In [16]:
a=np.random.randint(0,10,(4,5))
print a


[[6 8 5 8 8]
 [7 8 9 4 1]
 [3 7 3 0 4]
 [0 5 1 9 8]]

In [17]:
a[0,2]


Out[17]:
5

In [18]:
a[3,3]=9
print a


[[6 8 5 8 8]
 [7 8 9 4 1]
 [3 7 3 0 4]
 [0 5 1 9 8]]

In [19]:
print a[:,:3]


[[6 8 5]
 [7 8 9]
 [3 7 3]
 [0 5 1]]

In [20]:
print a[:3,:]


[[6 8 5 8 8]
 [7 8 9 4 1]
 [3 7 3 0 4]]

In [21]:
print a[:3,:3]


[[6 8 5]
 [7 8 9]
 [3 7 3]]

In [22]:
print a[-3:,-3:]


[[9 4 1]
 [3 0 4]
 [1 9 8]]

In [23]:
b=np.array([2,3,5,7,8])
print b


[2 3 5 7 8]

In [24]:
b[::-1]


Out[24]:
array([8, 7, 5, 3, 2])

In [25]:
a=np.random.randint(0,10,(4,5))
print a


[[0 2 6 9 7]
 [3 9 6 3 3]
 [8 1 8 2 4]
 [4 5 3 2 3]]

In [26]:
a[::-1]


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

In [27]:
print np.fliplr(a)


[[7 9 6 2 0]
 [3 3 6 9 3]
 [4 2 8 1 8]
 [3 2 3 5 4]]

In [28]:
a.astype(float)


Out[28]:
array([[ 0.,  2.,  6.,  9.,  7.],
       [ 3.,  9.,  6.,  3.,  3.],
       [ 8.,  1.,  8.,  2.,  4.],
       [ 4.,  5.,  3.,  2.,  3.]])
  1. Goto opencv/build/python/2.7 folder.
  2. Copy cv2.pyd to C:/Python27/lib/site-packeges.
    http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_setup_in_windows/py_setup_in_windows.html

In [35]:
print np.arange(16)[::2]


[ 0  2  4  6  8 10 12 14]

In [45]:
(-1,)+(1,2,3)+(4,5)


Out[45]:
(-1, 1, 2, 3, 4, 5)

In [ ]: