Appearance
Numpy 速查表
NumPy
Array manipulation mini-language. It can run up to one hundred times faster than the equivalent Python code. An even faster alternative that runs on a GPU is called CuPy.
python
# $ pip3 install numpy
import numpy as np
python
<array> = np.array(<list/list_of_lists/…>) # Returns a 1d/2d/… NumPy array.
<array> = np.zeros/ones/empty(<shape>) # Also np.full(<shape>, <el>).
<array> = np.arange(from_inc, to_exc, ±step) # Also np.linspace(start, stop, len).
<array> = np.random.randint(from_inc, to_exc, <shape>) # Also np.random.random(<shape>).
python
<view> = <array>.reshape(<shape>) # Also `<array>.shape = <shape>`.
<array> = <array>.flatten() # Also `<view> = <array>.ravel()`.
<view> = <array>.transpose() # Or: <array>.T
python
<array> = np.copy/abs/sqrt/log/int64(<array>) # Returns new array of the same shape.
<array> = <array>.sum/max/mean/argmax/all(axis) # Aggregates specified dimension.
<array> = np.apply_along_axis(<func>, axis, <array>) # Func can return a scalar or array.
python
<array> = np.concatenate(<list_of_arrays>, axis=0) # Links arrays along first axis (rows).
<array> = np.vstack/column_stack(<list_of_arrays>) # Treats 1d arrays as rows or columns.
<array> = np.tile/repeat(<array>, <int/list> [, axis]) # Tiles array or repeats its elements.
- Shape is a tuple of dimension sizes. A 100x50 RGB image has shape (50, 100, 3).
- Axis is an index of a dimension. Leftmost dimension has index 0. Summing the RGB image along axis 2 will return a greyscale image with shape (50, 100).
Indexing
perl
<el> = <2d>[row_index, col_index] # Or: <3d>[<int>, <int>, <int>]
<1d_view> = <2d>[row_index] # Or: <3d>[<int>, <int>, <slice>]
<1d_view> = <2d>[:, col_index] # Or: <3d>[<int>, <slice>, <int>]
<2d_view> = <2d>[from:to_row_i, from:to_col_i] # Or: <3d>[<int>, <slice>, <slice>]
perl
<1d_array> = <2d>[row_indices, col_indices] # Or: <3d>[<int/1d>, <1d>, <1d>]
<2d_array> = <2d>[row_indices] # Or: <3d>[<int/1d>, <1d>, <slice>]
<2d_array> = <2d>[:, col_indices] # Or: <3d>[<int/1d>, <slice>, <1d>]
<2d_array> = <2d>[np.ix_(row_indices, col_indices)] # Or: <3d>[<int/1d/2d>, <2d>, <2d>]
perl
<2d_bools> = <2d> > <el/1d/2d> # 1d object must have size of a row.
<1/2d_arr> = <2d>[<2d/1d_bools>] # 1d_bools must have size of a column.
':'
returns a slice of all dimension's indices. Omitted dimensions default to':'
.- Python converts
'obj[i, j]'
to'obj[(i, j)]'
. This makes'<2d>[row_i, col_i]'
and'<2d>[row_indices]'
indistinguishable to NumPy if tuple of indices is passed! - Indexing with a slice and 1d object works the same as when using two slices (lines 4, 6, 7).
'ix_([1, 2], [3, 4])'
returns'[[1], [2]]'
and'[[3, 4]]'
. Due to broadcasting rules, this is the same as using'[[1, 1], [2, 2]]'
and'[[3, 4], [3, 4]]'
.- Any value that is broadcastable to the indexed shape can be assigned to the selection.
Broadcasting
A set of rules by which NumPy functions operate on arrays of different shapes.
python
left = np.array([ 0.1, 0.6, 0.8 ]) # `left.shape == (3,)`
right = np.array([[0.1],[0.6],[0.8]]) # `right.shape == (3, 1)`
1. If array shapes differ in length, left-pad the shorter shape with ones:
python
left = np.array([[0.1, 0.6, 0.8]]) # `left.shape == (1, 3)`
right = np.array([[0.1],[0.6],[0.8]]) # `right.shape == (3, 1)`
2. If any dimensions differ in size, expand the ones that have size 1 by duplicating their elements:
python
left = np.array([[0.1, 0.6, 0.8], # `left.shape == (3, 3)`
[0.1, 0.6, 0.8],
[0.1, 0.6, 0.8]])
right = np.array([[0.1, 0.1, 0.1], # `right.shape == (3, 3)`
[0.6, 0.6, 0.6],
[0.8, 0.8, 0.8]])
Example
For each point returns index of its nearest point ([0.1, 0.6, 0.8] => [1, 2, 1]
):
python
>>> print(points := np.array([0.1, 0.6, 0.8]))
[0.1 0.6 0.8]
>>> print(wrapped_points := points.reshape(3, 1))
[[0.1]
[0.6]
[0.8]]
>>> print(deltas := points - wrapped_points)
[[ 0. 0.5 0.7]
[-0.5 0. 0.2]
[-0.7 -0.2 0. ]]
>>> deltas[range(3), range(3)] = np.inf
>>> print(distances := np.abs(deltas))
[[inf 0.5 0.7]
[0.5 inf 0.2]
[0.7 0.2 inf]]
>>> print(distances.argmin(axis=1))
[1 2 1]