Ben Chuanlong Du's Blog

It is never too late to learn.

Numpy Arrays in Python

Tips and Traps

  1. The Pythonic way of checking whether a collection (string, list, set, dict, etc.) coll is non-empty is to use if coll. However, do NOT use if arr to check whether a numpy array is non-empty or not. Instead, you shoule use arr.size >0 to check whether a numpy array is non-empty or not.

  2. bottleneck is a collection of fast, NaN-aware NumPy array functions written in C.

In [8]:
from operator import itemgetter
import numpy as np

Construct Numpy Arrays

Construct a numpy array from a list.

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

Join a sequence of arrays along a new axis using numpy.stack.

In [ ]:
np.stack(
    [
        [1, 2, 3],
        [4, 5, 6],
    ],
    axis=0,
)
In [ ]:
np.stack(
    [
        [1, 2, 3],
        [4, 5, 6],
    ],
    axis=1,
)

Slicing

In [12]:
arr[0]
Out[12]:
0
In [13]:
arr[1:4]
Out[13]:
array([1, 2, 3])
In [14]:
itemgetter(2, 3, 5)(arr)
Out[14]:
(2, 3, 5)
In [ ]:
 

numpy.flip

Reverse the order of elements in an array along the given axis. The shape of the array is preserved, but the elements are reordered.

In [ ]:
 

numpy.where

Note: numpy.where works on both numpy.ndarray and list, however, it doesn't work on a generator/iterator.

In [41]:
arr = np.array([9, 80, 1, 2, 7, 1000])
arr >= 30
Out[41]:
array([False,  True, False, False, False,  True])
In [42]:
np.where(arr >= 30)
Out[42]:
(array([1, 5]),)
In [43]:
np.where(arr >= 30)[0].min()
Out[43]:
1
In [44]:
np.min(np.where(arr >= 30))
Out[44]:
1

numpy.ndarray.sum

In [15]:
arr.sum()
Out[15]:
21

numpy.ndarray.prod

In [16]:
arr.prod()
Out[16]:
0

Missing Values

np.NaN, np.NAN and np.nan are the same.

  1. bottleneck is a collection of fast, NaN-aware NumPy array functions written in C.
In [20]:
np.NaN is np.NAN
Out[20]:
True
In [21]:
np.NAN is np.nan
Out[21]:
True

Matrix

In [33]:
mat = np.matrix(
    [
        [1, 2, 3],
        [4, 5, 6],
    ]
)
In [34]:
mat.diagonal()
Out[34]:
matrix([[1, 5]])
In [35]:
np.diag(mat)
Out[35]:
array([1, 5])
In [ ]:
 

Comments