Python Practice › NumPy Programs › Program 62
Remove single-dimensional entries
Write a NumPy program to remove dimensions of size 1 from a shape.
Input
shape (2, 1, 3)
Output
(2, 1, 3) (2, 3)
Approach
np.squeeze.
Solution
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.