Note
Go to the end to download the full example code.
12.3.10.2.9. Stacking arrays#
import numpy as np
rg = np.random.default_rng(1)
a = np.floor(10 * rg.random((2, 2)))
b = np.floor(10 * rg.random((2, 2)))
np.vstack((a, b))
array([[5., 9.],
[1., 9.],
[3., 4.],
[8., 4.]])
np.hstack((a, b))
array([[5., 9., 3., 4.],
[1., 9., 8., 4.]])
np.column_stack((a, b))
array([[5., 9., 3., 4.],
[1., 9., 8., 4.]])
a[:, np.newaxis]
array([[[5., 9.]],
[[1., 9.]]])
np.column_stack((a[:, np.newaxis], b[:, np.newaxis]))
array([[[5., 9.],
[3., 4.]],
[[1., 9.],
[8., 4.]]])
np.column_stack is np.hstack
False
np.row_stack is np.vstack
True