Python Practice › NumPy Programs › Program 89
Create and reshape
Write a NumPy program to create an array and reshape it.
Output
[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] (2, 6)
Approach
reshape(-1, n)lets NumPy work out one dimension.
Solution
import numpy as npa = np.arange(12)print(a.reshape(3, 4))print(a.reshape(-1, 6).shape)Output
[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] (2, 6)
Learn the concept
New to this topic? Read the lesson, then come back.