Python Practice › NumPy Programs › Program 89

Create and reshape

EasyNumPy Programs · 89 of 100

Write a NumPy program to create an array and reshape it.

Advertisement
Output
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
(2, 6)

Approach

  1. reshape(-1, n) lets NumPy work out one dimension.

Solution

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

More numpy programs

← All 100 NumPy Programs

Leave a Comment