Scalar


In [47]:
x=25.0

Vector


In [48]:
pos=[2,4,5]

Matrix


In [49]:
a=[[1,2,3],[4,5,6]]

In [50]:
a


Out[50]:
[[1, 2, 3], [4, 5, 6]]

In [51]:
a[1][1]


Out[51]:
5

numpy array


In [52]:
import numpy as np

In [53]:
np.zeros([3,4])


Out[53]:
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])

In [54]:
np.ones([2,3,4],dtype=int)


Out[54]:
array([[[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]],

       [[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]]])

In [55]:
np.random.rand(3,4)


Out[55]:
array([[ 0.33066173,  0.2559184 ,  0.92839795,  0.02755838],
       [ 0.69754227,  0.7681915 ,  0.22302007,  0.53866205],
       [ 0.09346035,  0.64775226,  0.05781645,  0.62395045]])

In [56]:
a=[[1,2,3],[4,5,6]]
x=np.array(a)

In [57]:
x.shape


Out[57]:
(2, 3)

Slicing


In [58]:
x=np.random.rand(7)
print(x)


[ 0.28237045  0.13296619  0.77677009  0.07883874  0.71175605  0.65052565
  0.30366707]

In [59]:
x[0]


Out[59]:
0.28237044947570589

In [60]:
x[2]


Out[60]:
0.77677008764588318

In [61]:
x[6]


Out[61]:
0.30366707243592983

In [62]:
x[-1]


Out[62]:
0.30366707243592983

In [63]:
x[-2]


Out[63]:
0.65052564782042432

In [64]:
x[:2]


Out[64]:
array([ 0.28237045,  0.13296619])

In [65]:
x[-2:]


Out[65]:
array([ 0.65052565,  0.30366707])

In [66]:
x[2:5]


Out[66]:
array([ 0.77677009,  0.07883874,  0.71175605])

In [67]:
x=np.random.rand(4,5)
print(x)


[[ 0.33003162  0.62709619  0.23331205  0.49044732  0.91758581]
 [ 0.10654041  0.08715837  0.87734153  0.47795825  0.32568299]
 [ 0.51915732  0.11149859  0.24749932  0.71335898  0.83078321]
 [ 0.6989782   0.78096246  0.45149481  0.3285072   0.71969224]]

In [68]:
x[1:,2:4]


Out[68]:
array([[ 0.87734153,  0.47795825],
       [ 0.24749932,  0.71335898],
       [ 0.45149481,  0.3285072 ]])

In [69]:
x[2,1]


Out[69]:
0.11149859209216317

In [70]:
x[:,:2]


Out[70]:
array([[ 0.33003162,  0.62709619],
       [ 0.10654041,  0.08715837],
       [ 0.51915732,  0.11149859],
       [ 0.6989782 ,  0.78096246]])

In [71]:
x[1:3, 1:3]


Out[71]:
array([[ 0.08715837,  0.87734153],
       [ 0.11149859,  0.24749932]])

In [72]:
x=np.random.rand(4,3,2)
print(x)


[[[ 0.2099229   0.20793103]
  [ 0.62183639  0.44567042]
  [ 0.99479313  0.42945244]]

 [[ 0.46534569  0.88825702]
  [ 0.49318478  0.75533067]
  [ 0.1275958   0.18675608]]

 [[ 0.78913182  0.44515932]
  [ 0.68122275  0.39810836]
  [ 0.09759379  0.05996145]]

 [[ 0.2448322   0.04435026]
  [ 0.4707253   0.10579906]
  [ 0.40232842  0.3134782 ]]]

In [73]:
x.sum(axis=2)


Out[73]:
array([[ 0.41785393,  1.0675068 ,  1.42424557],
       [ 1.35360271,  1.24851546,  0.31435188],
       [ 1.23429114,  1.07933112,  0.15755524],
       [ 0.28918246,  0.57652436,  0.71580662]])

In [ ]: