Python Practice › NumPy Programs › Program 33

Reshape 2 × 3 into 3 × 2

EasyNumPy Programs · 33 of 100

Write a NumPy program to reshape a 2 × 3 matrix into a 3 × 2 matrix.

Advertisement
Input
[[5, 6, 7], [8, 9, 10]]
Output
[[ 5  6]
 [ 7  8]
 [ 9 10]]

Approach

  1. reshape(3, 2); the number of items must stay the same.

Solution

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs