Skip to content
Justice Tech School

Flatten and Transpose

ravel and flatten make 1D arrays. T swaps rows and columns. swapaxes and transpose handle higher dimensions.

Example
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.T)
print(a.flatten())
print(a.T.shape)

Result

[[1 4]
 [2 5]
 [3 6]]
[1 2 3 4 5 6]
(3, 2)

Exercise

Swap rows and columns.

a.

Quiz Test

What shape is the transpose of 2 by 3?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.