Numpy functions on multi-dimensional arrays#
# initialization
import numpy as np
Array creation and reshaping#
Functions such as np.zeros() and np.full() can be used to create multi-dimensional arrays. The only change is that instead of supplying the size as a scalar, we specify the shape of the output. For example:
np.zeros((3,4,2)) # 3 sheets, 4 rows, 2 columns
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.]]])
np.full((3, 4), 1) # 3 rows, 4 columns, value = 1
array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]])
More generally, we can create a 1D array, and then reshape it into the desired shape. This can be achieved using the .reshape() method. An example:
np.arange(12).reshape((3, 4))
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
Note that when we reshape, values increment first in the last dimensions, then work its way up.
The reverse of .reshape() is .flatten(), e.g.,
x = np.array([[1, 3, 5, 7], [2, 4, 6, 8]])
x.flatten()
array([1, 3, 5, 7, 2, 4, 6, 8])
Finally, sometimes we want to exchange the rows and columns of a numpy array. Such operation is known as “transposition” and can be achieved using the .transpose() method
x.transpose()
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
Array combining#
Arrays whose shape are compatible can be joined using np.concatenate(). For example:
x = np.array([[1, 3, 5], [2, 4, 6]]) # 2 rows, 3 columns
y = np.array([[1, 4, 9], [2, 5, 8], [7, 7, 7]]) # 3 rows, 3 columns
# the number of columns match, so we can merge rows (axis = 0)
np.concatenate([x, y], axis=0)
array([[1, 3, 5],
[2, 4, 6],
[1, 4, 9],
[2, 5, 8],
[7, 7, 7]])
For 2-dimensional arrays there are two shortcuts: np.hstack() for stacking columns and np.vstack() for stacking rows:
x = np.array([[1, 3, 5], [2, 4, 6]]) # 2 rows, 3 columns
y = np.array([[1, 4, 9], [2, 5, 8], [7, 7, 7]]) # 3 rows, 3 columns
z = np.array([[-1, -2], [1, 2]]) # 2 rows, 2 columns
# stacking rows
np.vstack([x, y])
array([[1, 3, 5],
[2, 4, 6],
[1, 4, 9],
[2, 5, 8],
[7, 7, 7]])
# stacking rows
np.hstack([x, z])
array([[ 1, 3, 5, -1, -2],
[ 2, 4, 6, 1, 2]])
Mapping functions#
The mapping functions in numpy can also be applied to multi-dimensional arrays. The result is somewhat expected: the mapping is applied element-wise and the output array has the same dimension as the input array. Example:
angles = np.array([
[0, np.pi / 6, np.pi/3, np.pi / 2],
[-np.pi, -4 * np.pi /3, -5 * np.pi / 3, -2 * np.pi]
])
np.round(np.sin(angles), 3)
array([[ 0. , 0.5 , 0.866, 1. ],
[-0. , 0.866, 0.866, 0. ]])
Reduction functions#
By default, reduction function will reduce a multi-dimensional array to a scalar, e.g.,
x = np.array([[1, 3, 5, 7], [2, 4, 6, 8]])
np.max(x)
8
However, more interesting (and useful) behavior can be obtained by supplying the axis argument, which tells numpy the dimension(s) that the reduction should be applied on.
For example, setting axis=0 for a 2D array will apply the reduction across the first dimension, i.e., row:
np.max(x, axis=0)
array([2, 4, 6, 8])
Whereas setting axis=1 for a 1D array will apply the reduction across the second dimension, i.e., column:
np.max(x, axis=1)
array([7, 8])
The idea also applies to 3D arrays, where axis=0 corresponds to sheets, axis=1 corresponds to rows, and axis=2 corresponds to column:
y = np.array([
[
[1, 2, 3, 4],
[5, 6, 7, 8]
],[
[-2, -1, 1, 2],
[-4, -3, 3, 4]
],[
[0, 1, 0, 1],
[1, 1, 0, 0]
]
])
np.mean(y, axis=2)
array([[2.5, 6.5],
[0. , 0. ],
[0.5, 0.5]])
You can also apply reduction across multiple axes at once. For example, we can compute the average over both rows and columns of each sheet:
np.mean(y, axis=(1,2))
array([4.5, 0. , 0.5])