Here are some tricks of the Python scientific ecosystem that you may not know about yet.
Here is a boring 1D Numpy array:
Indexing with None
adds empty dimensions on the fly.
Need x
as column vector? x[:, None]
Need x
as row vector? x[None, :]
Outer product? x[:, None] @ x[None, :] # or np.outer(x, x)
Need x
as 5-dimensional array? x[None, None, :, None, None]
np.newaxis
is defined as None
. This may be slightly more readable: x[np.newaxis, :]
Here is the 5D Numpy array from the example above:
Want to select some elements along the 3rd dimention?
Use the ellipsis (...
) operator to avoid having to type so many :
's!
Also useful when writing functions that take an array of arbitrary many dimensions as input:
You may be familiar with the super useful zip
function:
Did you know that you can feed the result back into zip
to achieve the opposite?
Here is a common use case for it: selecting specific cells from a matrix.
NumPy broadcasting behavior is truly epic. Here is how to remove the column-wise mean from a matrix:
So clean and readable! How to remove the row-wise mean?
The default broadcasting behavior doesn't work for us here. One solution is to use the [None]
indexing trick discussed above. Broadcasting behavior becomes a lot more intuitive when you make sure that both arrays have the same number of dimensions. Any dimensions that have a length of 1 will get broadcasted:
Recognizing this, many NumPy functions have a keepdims
parameter. When set, any dimensions that would normally be removed will be set to length 1 instead. This makes broadcasting super easy: