w3resource

NumPy: Remove single-dimensional entries from a specified shape

NumPy: Array Object Exercise-57 with Solution

Write a NumPy program to remove single-dimensional entries from a specified shape.
Specified shape: (3, 1, 4).

Sample Solution:-

Python Code:

import numpy as np
x = np.zeros((3, 1, 4))
print(np.squeeze(x).shape)

Sample Output:

(3, 4)

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to insert a new axis within a 2-D array.
Next: Write a NumPy program to concatenate two 2-dimensional arrays.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Set comprehension:

>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}