Numpy snippets

Jérémie Decock (www.jdhp.org)

This notebook is inspired by the following page: https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html

Import directives

In [2]:
import numpy as np

Create arrays

In [3]:
np.array([1, 2, 3])
Out[3]:
array([1, 2, 3])
In [4]:
np.array([[1, 2, 3],[4, 5, 6]])
Out[4]:
array([[1, 2, 3],
       [4, 5, 6]])

Special matrices

In [5]:
np.zeros(3)
Out[5]:
array([ 0.,  0.,  0.])
In [6]:
np.zeros((3, 4))
Out[6]:
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])
In [7]:
np.ones(3)
Out[7]:
array([ 1.,  1.,  1.])
In [8]:
np.ones((3, 4))
Out[8]:
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]])
In [9]:
np.eye(3)
Out[9]:
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])

Arange

In [10]:
np.arange(10)
Out[10]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [11]:
np.arange(10, 20)
Out[11]:
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
In [12]:
np.arange(10, 20, 2)
Out[12]:
array([10, 12, 14, 16, 18])

Linspace

In [13]:
np.linspace(0., 2., 5)
Out[13]:
array([ 0. ,  0.5,  1. ,  1.5,  2. ])

Meshgrid

In [14]:
xx, yy = np.meshgrid([1, 2, 3], [4, 5, 6])

print(xx)
print()
print(yy)
[[1 2 3]
 [1 2 3]
 [1 2 3]]

[[4 4 4]
 [5 5 5]
 [6 6 6]]

Random

Uniform distribution in [0, 1]

In [15]:
np.random.rand(3)
Out[15]:
array([ 0.49846881,  0.08922033,  0.64974019])
In [16]:
np.random.rand(3, 4)
Out[16]:
array([[ 0.91095588,  0.75443185,  0.29548162,  0.18609036],
       [ 0.59484319,  0.4377653 ,  0.34282951,  0.33219161],
       [ 0.21380037,  0.14987448,  0.46479221,  0.01749553]])

Poisson distribution

In [17]:
np.random.poisson(10, size=[3, 4])
Out[17]:
array([[10,  6,  9, 11],
       [11,  8, 12, 13],
       [11,  9,  6, 13]])

Multivariate normal distribution

In [18]:
mu = np.array([0., 0.])
cov = np.array([[1., 0.],
                [1., 1.]])
num_points = 10

np.random.multivariate_normal(mu, cov, num_points)
Out[18]:
array([[ 1.03169321, -0.59277011],
       [ 0.58525271, -0.45136561],
       [ 0.18753815,  1.59337572],
       [-1.30256849, -1.42144759],
       [-0.59328423, -1.94807392],
       [-0.87318203,  2.54109818],
       [ 0.17166882,  0.08995027],
       [-0.33527784, -1.1170611 ],
       [-0.88001965, -0.79824342],
       [-0.17083551,  1.38811894]])
In [19]:
np.get_printoptions()
Out[19]:
{'edgeitems': 3,
 'formatter': None,
 'infstr': 'inf',
 'linewidth': 75,
 'nanstr': 'nan',
 'precision': 8,
 'suppress': False,
 'threshold': 1000}
In [20]:
default_threshold = np.get_printoptions()["threshold"]
default_threshold
Out[20]:
1000

Arrays with more than default_threshold elements are truncated.

In [21]:
max_size = math.ceil(math.sqrt(default_threshold))
max_size
Out[21]:
32
In [22]:
a = np.random.randint(1, size=[max_size + 1, max_size + 1])
a
Out[22]:
array([[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]])

Print the full array (set threshold to infinity):

In [23]:
np.set_printoptions(threshold=np.inf)
a
Out[23]:
array([[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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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],
       [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, 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, 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, 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, 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, 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, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

Go back to the default threshold:

In [24]:
np.set_printoptions(threshold=default_threshold)
a
Out[24]:
array([[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]])

Dimension and shape

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

Number of dimensions:

In [26]:
a.ndim
Out[26]:
2

Number of elements:

In [27]:
a.size
Out[27]:
6

Number of elements per dimension:

In [28]:
a.shape
Out[28]:
(2, 3)

Convert

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

Python list to Numpy array

In [30]:
np.array(l)
Out[30]:
array([[1, 2, 3],
       [4, 5, 6]])

Numpy array to Python list

In [31]:
a.tolist()
Out[31]:
[[1, 2, 3], [4, 5, 6]]

Copy

np.copy()

In [32]:
a = np.array([[1, 2, 3],[4, 5, 6]])
a
Out[32]:
array([[1, 2, 3],
       [4, 5, 6]])
In [33]:
b = a.copy()
b
Out[33]:
array([[1, 2, 3],
       [4, 5, 6]])
In [34]:
a[0,0] = 10
print(a)
print(b)
[[10  2  3]
 [ 4  5  6]]
[[1 2 3]
 [4 5 6]]

np.astype()

In [35]:
a = np.array([[1, 2, 3],[4, 5, 6]])
a
Out[35]:
array([[1, 2, 3],
       [4, 5, 6]])
In [36]:
b = a.astype('float64', copy=True)
b
Out[36]:
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])
In [37]:
a[0,0] = 10
print(a)
print(b)
[[10  2  3]
 [ 4  5  6]]
[[ 1.  2.  3.]
 [ 4.  5.  6.]]

Access elements

In [38]:
a = np.arange(6)
In [39]:
a
Out[39]:
array([0, 1, 2, 3, 4, 5])
In [40]:
a[0]
Out[40]:
0
In [41]:
a[-1]
Out[41]:
5

Slices

In [42]:
a[1:4]
Out[42]:
array([1, 2, 3])
In [43]:
a = np.array([[1, 2, 3, 4, 5, 6],
              [10, 20, 30, 40, 50, 60],
              [100, 200, 300, 400, 500, 600]])
a
Out[43]:
array([[  1,   2,   3,   4,   5,   6],
       [ 10,  20,  30,  40,  50,  60],
       [100, 200, 300, 400, 500, 600]])
In [44]:
a[0,1]
Out[44]:
2
In [45]:
a[1, :]
Out[45]:
array([10, 20, 30, 40, 50, 60])
In [46]:
a[1, ::2]
Out[46]:
array([10, 30, 50])
In [47]:
a[:, 1]
Out[47]:
array([  2,  20, 200])
In [48]:
a[0:2, 2:4]
Out[48]:
array([[ 3,  4],
       [30, 40]])
In [49]:
a[1:, 1:]
Out[49]:
array([[ 20,  30,  40,  50,  60],
       [200, 300, 400, 500, 600]])
In [50]:
a[:-1, :-1]
Out[50]:
array([[ 1,  2,  3,  4,  5],
       [10, 20, 30, 40, 50]])

Ellipsis

"The ellipsis is used to slice high-dimensional data structures.

It's designed to mean at this point, insert as many full slices (:) to extend the multi-dimensional slice to all dimensions."

https://stackoverflow.com/questions/118370/how-do-you-use-the-ellipsis-slicing-syntax-in-python

In [51]:
a = np.arange(2**3).reshape(2, 2, 2)
a
Out[51]:
array([[[0, 1],
        [2, 3]],

       [[4, 5],
        [6, 7]]])

To select all first elements in the last (3rd) dimension

In [52]:
a[..., 0]
Out[52]:
array([[0, 2],
       [4, 6]])

is equivalent to

In [53]:
a[:, :, 0]
Out[53]:
array([[0, 2],
       [4, 6]])

To select all first elements in the first (1st) dimension

In [54]:
a[0, ...]
Out[54]:
array([[0, 1],
       [2, 3]])

is equivalent to

In [55]:
a[0, :, :]
Out[55]:
array([[0, 1],
       [2, 3]])

Filter

In [56]:
a = np.array([[1, 2, 3, 4, 5, 6],
              [10, 20, 30, 40, 50, 60],
              [100, 200, 300, 400, 500, 600]])
a
Out[56]:
array([[  1,   2,   3,   4,   5,   6],
       [ 10,  20,  30,  40,  50,  60],
       [100, 200, 300, 400, 500, 600]])

Boolean matrix whose i,jth element is (a_ij > 5)

In [57]:
(a>5)
Out[57]:
array([[False, False, False, False, False,  True],
       [ True,  True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True,  True]], dtype=bool)

Find the indices where (a > 5)

In [58]:
np.nonzero(a>5)
Out[58]:
(array([0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2]),
 array([5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5]))

Set or copy a with elements greater than 5 zeroed out

In [59]:
a * (a<=5)
Out[59]:
array([[1, 2, 3, 4, 5, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0]])
In [60]:
a[a>5] = 0
a
Out[60]:
array([[1, 2, 3, 4, 5, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0]])

Select indices satisfying multiple conditions

Short version

In [61]:
a = np.array([[-1, 7, 3], [-11, -5, 20]])
a
Out[61]:
array([[ -1,   7,   3],
       [-11,  -5,  20]])
In [62]:
a[(a > -10) & (a < 10)] = 0
a
Out[62]:
array([[  0,   0,   0],
       [-11,   0,  20]])
In [63]:
a[(a < -10) | (a > 10)] = 1
a
Out[63]:
array([[0, 0, 0],
       [1, 0, 1]])

Detailed version

In [64]:
a = np.array([[-1, 7, 3], [-11, -5, 20]])
a
Out[64]:
array([[ -1,   7,   3],
       [-11,  -5,  20]])
In [65]:
m1 = (a > -10)
m2 = (a < 10)

print(m1)
print(m2)
print(m1 & m2)

a[m1 & m2] = 0
a
[[ True  True  True]
 [False  True  True]]
[[ True  True  True]
 [ True  True False]]
[[ True  True  True]
 [False  True False]]
Out[65]:
array([[  0,   0,   0],
       [-11,   0,  20]])

Concatenate

Concatenate vectors

In [66]:
a = np.zeros(3)
b = np.ones(3)
print("a:", a)
print("b:", b)
a: [ 0.  0.  0.]
b: [ 1.  1.  1.]
In [67]:
np.concatenate([a, b])
Out[67]:
array([ 0.,  0.,  0.,  1.,  1.,  1.])
In [68]:
np.hstack([a, b])
Out[68]:
array([ 0.,  0.,  0.,  1.,  1.,  1.])

Concatenate matrices

In [69]:
a = np.zeros([2, 3])
b = np.ones([2, 3])
In [70]:
a
Out[70]:
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])
In [71]:
b
Out[71]:
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])

On the first dimension

Using vstack:

In [72]:
np.vstack([a, b])
Out[72]:
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])

Using concatenate:

In [74]:
np.concatenate([a, b], axis=0)
Out[74]:
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])

On the second dimension

Using hstack:

In [76]:
np.hstack([a, b])
Out[76]:
array([[ 0.,  0.,  0.,  1.,  1.,  1.],
       [ 0.,  0.,  0.,  1.,  1.,  1.]])

Using concatenate:

In [78]:
np.concatenate([a, b], axis=1)
Out[78]:
array([[ 0.,  0.,  0.,  1.,  1.,  1.],
       [ 0.,  0.,  0.,  1.,  1.,  1.]])

Join a sequence of arrays along a new axis

The axis parameter specifies the index of the new axis in the dimensions of the result.

In [80]:
a = np.zeros([2, 3])
b = np.ones([2, 3])
In [81]:
a
Out[81]:
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])
In [82]:
b
Out[82]:
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])

Along axis 0

In [83]:
np.stack([a, b], axis=0)
Out[83]:
array([[[ 0.,  0.,  0.],
        [ 0.,  0.,  0.]],

       [[ 1.,  1.,  1.],
        [ 1.,  1.,  1.]]])
In [84]:
np.stack([a, b], axis=0).shape
Out[84]:
(2, 2, 3)

Along axis 1

In [85]:
np.stack([a, b], axis=1)
Out[85]:
array([[[ 0.,  0.,  0.],
        [ 1.,  1.,  1.]],

       [[ 0.,  0.,  0.],
        [ 1.,  1.,  1.]]])
In [86]:
np.stack([a, b], axis=1).shape
Out[86]:
(2, 2, 3)

Along axis 2

In [87]:
np.stack([a, b], axis=2)
Out[87]:
array([[[ 0.,  1.],
        [ 0.,  1.],
        [ 0.,  1.]],

       [[ 0.,  1.],
        [ 0.,  1.],
        [ 0.,  1.]]])
In [88]:
np.stack([a, b], axis=2).shape
Out[88]:
(2, 3, 2)

Tile

In [89]:
a = np.array([[1, 2, 3], [4, 5, 6]])
np.tile(a, (2, 3))
Out[89]:
array([[1, 2, 3, 1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6, 4, 5, 6],
       [1, 2, 3, 1, 2, 3, 1, 2, 3],
       [4, 5, 6, 4, 5, 6, 4, 5, 6]])

Reshape or transpose

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

Transpose

In [91]:
a.T
Out[91]:
array([[1, 4],
       [2, 5],
       [3, 6]])

Flatten

In [92]:
a.flatten()
Out[92]:
array([1, 2, 3, 4, 5, 6])

Reshape

In [93]:
a = np.arange(6)
a
Out[93]:
array([0, 1, 2, 3, 4, 5])

Row vector to column vector

In [94]:
a.reshape([-1, 1])
Out[94]:
array([[0],
       [1],
       [2],
       [3],
       [4],
       [5]])

Vector to matrix

In [95]:
a.reshape([2, 3])
Out[95]:
array([[0, 1, 2],
       [3, 4, 5]])
In [96]:
a.reshape([3, 2])
Out[96]:
array([[0, 1],
       [2, 3],
       [4, 5]])

Aggregation / reduction

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

Change the axis value in the following functions to aggregate along a given axis.

In [98]:
np.sum(a, axis=None)
Out[98]:
21
In [99]:
np.cumsum(a, axis=None)
Out[99]:
array([ 1,  3,  6, 10, 15, 21])
In [100]:
np.diff(a.ravel())
Out[100]:
array([1, 1, 1, 1, 1])
In [101]:
np.mean(a, axis=None)
Out[101]:
3.5
In [102]:
np.var(a, axis=None)
Out[102]:
2.9166666666666665
In [103]:
np.std(a, axis=None)
Out[103]:
1.707825127659933
In [104]:
np.median(a, axis=None)
Out[104]:
3.5
In [105]:
np.min(a, axis=None)
Out[105]:
1
In [106]:
np.max(a, axis=None)
Out[106]:
6
In [107]:
np.prod(a, axis=None)
Out[107]:
720
In [108]:
np.cumprod(a, axis=None)
Out[108]:
array([  1,   2,   6,  24, 120, 720])

Compute the histogram of a set of data (with a specific binning)

In [109]:
a = np.array([1, 1, 3, 2, 2, 2])
a
Out[109]:
array([1, 1, 3, 2, 2, 2])

All but the last (righthand-most) bin is half-open. In other words, if bins is:

[1, 2, 3, 4]

then the first bin is [1, 2) (including 1, but excluding 2) and the second [2, 3). The last bin, however, is [3, 4], which includes 4.

In [110]:
bins = np.array([1, 2, 3, 4])
bins
Out[110]:
array([1, 2, 3, 4])
In [111]:
hist, bins_ = np.histogram(a, bins=bins)
hist
Out[111]:
array([2, 3, 1])

Linear algebra

Dot product of two arrays

In [112]:
a = np.array([1, 2, 3])
b = np.array([10, 20, 30])
In [113]:
np.dot(a, b)
Out[113]:
140
In [114]:
a.dot(b)
Out[114]:
140

Compute the (multiplicative) inverse of a matrix

In [115]:
a = np.random.normal(size=(3, 3))
a
Out[115]:
array([[-0.87995168,  0.31309307, -0.17371675],
       [-0.02608205, -0.15537251, -0.39861845],
       [-0.53996303,  0.52858947,  0.43896486]])
In [116]:
np.linalg.inv(a)
Out[116]:
array([[ -3.80496602,   6.12153001,   4.05310004],
       [ -6.05281836,  12.81833664,   9.24481654],
       [  2.6082157 ,  -7.9055026 ,  -3.86862052]])

Compute the eigenvalues and right eigenvectors of a square array

In [117]:
a = np.random.normal(size=(3, 3))
a
Out[117]:
array([[ 1.19117901, -0.07578913, -0.84708534],
       [-1.09233479, -0.95774735,  1.22852515],
       [ 0.34123376,  1.46160672, -0.3344782 ]])
In [118]:
np.linalg.eig(a)
Out[118]:
(array([-1.83505709,  1.48179771,  0.25221285]),
 array([[ 0.1687629 , -0.83430493,  0.66961718],
        [-0.7249802 ,  0.49539317,  0.13669826],
        [ 0.66777451,  0.24191092,  0.7300181 ]]))

Singular Value Decomposition

In [119]:
a = np.random.normal(size=(3, 3))
a
Out[119]:
array([[-1.4626098 , -0.42874071,  0.76704942],
       [-1.47206938,  0.11652844,  0.78948959],
       [-0.03221191, -0.08992432, -0.15300779]])
In [120]:
U, s, V = np.linalg.svd(a)
print(U, s, V)
[[-0.71394132 -0.674647    0.18745459]
 [-0.70005136  0.68211215 -0.2113081 ]
 [ 0.01469333 -0.28208942 -0.95927559]] [ 2.35967054  0.39938082  0.14232731] [[ 0.87905002  0.09458885 -0.46725155]
 [-0.02074326  0.98677949  0.16073565]
 [ 0.47627805 -0.13160235  0.86939061]]

Solve a linear matrix equation, or system of linear scalar equations

In [121]:
a = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])
np.linalg.solve(a, b)
Out[121]:
array([ 2.,  3.])

Diagonals

Extract the diagonal:

In [122]:
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
np.diag(a)
Out[122]:
array([1, 5, 9])

Make a diagonal matrix:

In [123]:
d = np.array([1, 2, 3])
np.diag(d)
Out[123]:
array([[1, 0, 0],
       [0, 2, 0],
       [0, 0, 3]])

Trace

In [124]:
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
np.trace(a)
Out[124]:
15

Upper and lower triangles of an array

In [125]:
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
In [126]:
np.triu(a)
Out[126]:
array([[1, 2, 3],
       [0, 5, 6],
       [0, 0, 9]])
In [127]:
np.tril(a)
Out[127]:
array([[1, 0, 0],
       [4, 5, 0],
       [7, 8, 9]])

Data types

Get type

In [128]:
a = np.arange(-2., 2., 0.5)
a.dtype
Out[128]:
dtype('float64')

Size in memory (in bytes)

In [129]:
a = np.arange(-2., 2., 0.5)

Per item:

In [130]:
a.itemsize
Out[130]:
8

Full array:

In [131]:
a.nbytes
Out[131]:
64

Init

In [132]:
a = np.zeros(3)
a.dtype
Out[132]:
dtype('float64')
In [133]:
a = np.zeros(3, dtype=np.bool)
a.dtype
Out[133]:
dtype('bool')
In [134]:
a = np.zeros(3, dtype=np.int)
a.dtype
Out[134]:
dtype('int64')
In [135]:
a = np.zeros(3, dtype=np.int8)
a.dtype
Out[135]:
dtype('int8')
In [136]:
a = np.zeros(3, dtype=np.uint8)
a.dtype
Out[136]:
dtype('uint8')

Conversions

In [137]:
a = np.arange(-2., 2., 0.5)
a
Out[137]:
array([-2. , -1.5, -1. , -0.5,  0. ,  0.5,  1. ,  1.5])
In [138]:
a.astype(np.bool)
Out[138]:
array([ True,  True,  True,  True, False,  True,  True,  True], dtype=bool)
In [139]:
a.astype(np.int)
Out[139]:
array([-2, -1, -1,  0,  0,  0,  1,  1])
In [140]:
a.astype(np.int8)
Out[140]:
array([-2, -1, -1,  0,  0,  0,  1,  1], dtype=int8)
In [141]:
a.astype(np.uint8)
Out[141]:
array([254, 255, 255,   0,   0,   0,   1,   1], dtype=uint8)

Masked arrays

Without masked array

In [142]:
a = np.array([[np.nan, 2, 3], [1, np.nan, 6]])
a
Out[142]:
array([[ nan,   2.,   3.],
       [  1.,  nan,   6.]])
In [143]:
a.min()
Out[143]:
nan
In [144]:
np.nanmin(a)
Out[144]:
1.0
In [145]:
a.max()
Out[145]:
nan
In [146]:
np.nanmax(a)
Out[146]:
6.0
In [147]:
a.mean()
Out[147]:
nan
In [148]:
np.nanmean(a)
Out[148]:
3.0
In [149]:
a.shape
Out[149]:
(2, 3)

With masked array

In [150]:
ma = np.ma.masked_where(np.isnan(a), a)
ma
Out[150]:
masked_array(data =
 [[-- 2.0 3.0]
 [1.0 -- 6.0]],
             mask =
 [[ True False False]
 [False  True False]],
       fill_value = 1e+20)
In [151]:
ma.min()
Out[151]:
1.0
In [152]:
ma.max()
Out[152]:
6.0
In [153]:
ma.mean()
Out[153]:
3.0
In [154]:
ma.shape
Out[154]:
(2, 3)