Tips and Traps¶
The Pythonic way of checking whether a collection (string, list, set, dict, etc.)
coll
is non-empty is to useif coll
. However, do NOT useif arr
to check whether a numpy array is non-empty or not. Instead, you shoule usearr.size >0
to check whether a numpy array is non-empty or not.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]:
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]:
In [13]:
arr[1:4]
Out[13]:
In [14]:
itemgetter(2, 3, 5)(arr)
Out[14]:
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]:
In [42]:
np.where(arr >= 30)
Out[42]:
In [43]:
np.where(arr >= 30)[0].min()
Out[43]:
In [44]:
np.min(np.where(arr >= 30))
Out[44]:
numpy.ndarray.sum¶
In [15]:
arr.sum()
Out[15]:
numpy.ndarray.prod¶
In [16]:
arr.prod()
Out[16]:
Missing Values¶
np.NaN
, np.NAN
and np.nan
are the same.
- bottleneck is a collection of fast, NaN-aware NumPy array functions written in C.
In [20]:
np.NaN is np.NAN
Out[20]:
In [21]:
np.NAN is np.nan
Out[21]:
Matrix¶
In [33]:
mat = np.matrix(
[
[1, 2, 3],
[4, 5, 6],
]
)
In [34]:
mat.diagonal()
Out[34]:
In [35]:
np.diag(mat)
Out[35]:
In [ ]: