Python Practice › NumPy Programs › Program 62

Remove single-dimensional entries

EasyNumPy Programs · 62 of 100

Write a NumPy program to remove dimensions of size 1 from a shape.

Advertisement
Input
shape (2, 1, 3)
Output
(2, 1, 3) (2, 3)

Approach

  1. np.squeeze.

Solution

Python
import numpy as npa = np.zeros((2, 1, 3))print(a.shape, np.squeeze(a).shape)
Output
(2, 1, 3) (2, 3)

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Leave a Comment