Numpy array slicing


In [2]:
import numpy as np

One dimensional arrays are easy


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

In [4]:
x


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

In [5]:
x[1]


Out[5]:
2

In [6]:
x[:2]


Out[6]:
array([1, 2])

In [7]:
x[-3:]


Out[7]:
array([3, 4, 5])

2 dimensional arrays


In [8]:
x = np.array([[11,12,13,14,15,], [21, 22, 23, 24, 25, ], [31, 32, 33, 34, 35,]])

In [9]:
x


Out[9]:
array([[11, 12, 13, 14, 15],
       [21, 22, 23, 24, 25],
       [31, 32, 33, 34, 35]])

In [10]:
x.shape


Out[10]:
(3, 5)

In [11]:
x[0]


Out[11]:
array([11, 12, 13, 14, 15])

In [15]:
x[0] == x[0,:]


Out[15]:
array([ True,  True,  True,  True,  True], dtype=bool)

In [12]:
x[0, 1]


Out[12]:
12

In [13]:
x[1, 1:-2]


Out[13]:
array([22, 23])

In [16]:
x[:,1]


Out[16]:
array([12, 22, 32])

In [17]:
x.mean(axis=0)


Out[17]:
array([ 21.,  22.,  23.,  24.,  25.])

In [18]:
x.mean(axis=1)


Out[18]:
array([ 13.,  23.,  33.])

In [19]:
x.flatten()


Out[19]:
array([11, 12, 13, 14, 15, 21, 22, 23, 24, 25, 31, 32, 33, 34, 35])

In [20]:
x.T


Out[20]:
array([[11, 21, 31],
       [12, 22, 32],
       [13, 23, 33],
       [14, 24, 34],
       [15, 25, 35]])

3 dimensional arrays


In [22]:
x = np.array([
    [
        range(111, 116),
        range(121, 126),
        range(131, 136),
        range(141, 146),
    ],
        [
        range(211, 216),
        range(221, 226),
        range(231, 236),
        range(241, 246),
    ],
        [
        range(311, 316),
        range(321, 326),
        range(331, 336),
        range(341, 346),
    ],
    ]
)

In [23]:
x


Out[23]:
array([[[111, 112, 113, 114, 115],
        [121, 122, 123, 124, 125],
        [131, 132, 133, 134, 135],
        [141, 142, 143, 144, 145]],

       [[211, 212, 213, 214, 215],
        [221, 222, 223, 224, 225],
        [231, 232, 233, 234, 235],
        [241, 242, 243, 244, 245]],

       [[311, 312, 313, 314, 315],
        [321, 322, 323, 324, 325],
        [331, 332, 333, 334, 335],
        [341, 342, 343, 344, 345]]])

In [24]:
x[0]


Out[24]:
array([[111, 112, 113, 114, 115],
       [121, 122, 123, 124, 125],
       [131, 132, 133, 134, 135],
       [141, 142, 143, 144, 145]])

In [25]:
x.mean(axis=0)


Out[25]:
array([[ 211.,  212.,  213.,  214.,  215.],
       [ 221.,  222.,  223.,  224.,  225.],
       [ 231.,  232.,  233.,  234.,  235.],
       [ 241.,  242.,  243.,  244.,  245.]])

In [26]:
x.mean(axis=1)


Out[26]:
array([[ 126.,  127.,  128.,  129.,  130.],
       [ 226.,  227.,  228.,  229.,  230.],
       [ 326.,  327.,  328.,  329.,  330.]])

In [27]:
x.mean(axis=2)


Out[27]:
array([[ 113.,  123.,  133.,  143.],
       [ 213.,  223.,  233.,  243.],
       [ 313.,  323.,  333.,  343.]])

In [28]:
x.shape


Out[28]:
(3, 4, 5)

In [30]:
y = x.reshape(x.shape[0], x.shape[1]*x.shape[2])

In [31]:
y.shape


Out[31]:
(3, 20)

In [32]:
y.mean(axis=0)


Out[32]:
array([ 211.,  212.,  213.,  214.,  215.,  221.,  222.,  223.,  224.,
        225.,  231.,  232.,  233.,  234.,  235.,  241.,  242.,  243.,
        244.,  245.])

Matrix multiplication


In [34]:
a = x[0]
a


Out[34]:
array([[111, 112, 113, 114, 115],
       [121, 122, 123, 124, 125],
       [131, 132, 133, 134, 135],
       [141, 142, 143, 144, 145]])

In [36]:
b = x[1]
b


Out[36]:
array([[211, 212, 213, 214, 215],
       [221, 222, 223, 224, 225],
       [231, 232, 233, 234, 235],
       [241, 242, 243, 244, 245]])

In [37]:
np.multiply?

In [39]:
np.dot(a,b.T)


Out[39]:
array([[120355, 126005, 131655, 137305],
       [131005, 137155, 143305, 149455],
       [141655, 148305, 154955, 161605],
       [152305, 159455, 166605, 173755]])

In [45]:
a = np.array([[1, 2,],[3, 4]])

In [46]:
a


Out[46]:
array([[1, 2],
       [3, 4]])

In [44]:
b = np.array([[10, 20,],[30, 40]])

In [47]:
b


Out[47]:
array([[10, 20],
       [30, 40]])

In [48]:
np.dot(a,b)


Out[48]:
array([[ 70, 100],
       [150, 220]])

In [52]:
np.dot(a[0,:], b[:,1])


Out[52]:
100

In [ ]: