Note
Go to the end to download the full example code.
12.3.10.2.7. Shape manipulation#
import numpy as np
rg = np.random.default_rng(1)
a = np.floor(10 * rg.random((3, 4)))
a
array([[5., 9., 1., 9.],
[3., 4., 8., 4.],
[5., 0., 7., 5.]])
return the array, flattened
a.ravel()
array([5., 9., 1., 9., 3., 4., 8., 4., 5., 0., 7., 5.])
modified shape
a.reshape(6, 2)
array([[5., 9.],
[1., 9.],
[3., 4.],
[8., 4.],
[5., 0.],
[7., 5.]])
transpose
a.T
array([[5., 3., 5.],
[9., 4., 0.],
[1., 8., 7.],
[9., 4., 5.]])
a.resize(2, 6)