w3resource

NumPy: Remove the first dimension from a given array of shape (1,3,4)

NumPy: Array Object Exercise-195 with Solution

Write a NumPy program to remove the first dimension from a given array of shape (1,3,4).

Sample array:
[[[1, 2, 3, 4],
[0, 1, 3, 4],
[5, 0, 3, 2]]])

Sample Solution:

Python Code:

import numpy as np
nums = np.array([[[1, 2, 3, 4],
               [0, 1, 3, 4],
               [5, 0, 3, 2]]])
print('Shape of the said array:')
print(nums.shape) 
print("\nAfter removing the first dimension of the shape of the said array:")

Sample Output:

Shape of the said array:
(1, 3, 4)

After removing the first dimension of the shape of the said array:

Python Code Editor:

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

Previous: Write a NumPy program to create two arrays with shape (300,400, 5), fill values using unsigned integer (0 to 255). Insert a new axis that will appear at the beginning in the expanded array shape. Now combine the said two arrays into one.
Next: Write a NumPy program to create a 12x12x4 array with random values and extract any array of shape(6,6,3) from the said array.

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}