Python Practice › NumPy Programs › Program 33
Reshape 2 × 3 into 3 × 2
Write a NumPy program to reshape a 2 × 3 matrix into a 3 × 2 matrix.
Input
[[5, 6, 7], [8, 9, 10]]
Output
[[ 5 6] [ 7 8] [ 9 10]]
Approach
reshape(3, 2); the number of items must stay the same.
Solution
import numpy as npm = np.array([[5, 6, 7], [8, 9, 10]])print(m.reshape(3, 2))Output
[[ 5 6] [ 7 8] [ 9 10]]
Note
The original task said “2 × 2”, but six values cannot fit in a 2 × 2 matrix; its sample output was 3 × 2.
Learn the concept
New to this topic? Read the lesson, then come back.