12.3.2.5. 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))

Out:

array([[5., 9.],
       [1., 9.],
       [3., 4.],
       [8., 4.]])
np.hstack((a, b))

Out:

array([[5., 9., 3., 4.],
       [1., 9., 8., 4.]])
np.column_stack((a, b))

Out:

array([[5., 9., 3., 4.],
       [1., 9., 8., 4.]])
a[:, np.newaxis]

Out:

array([[[5., 9.]],

       [[1., 9.]]])
np.column_stack((a[:, np.newaxis], b[:, np.newaxis]))

Out:

array([[[5., 9.],
        [3., 4.]],

       [[1., 9.],
        [8., 4.]]])
np.column_stack is np.hstack

Out:

False
np.row_stack is np.vstack

Out:

True

Total running time of the script: ( 0 minutes 0.004 seconds)

Gallery generated by Sphinx-Gallery