- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove axes of length one from an array over axis 0 in Numpy
Squeeze the Array shape using the numpy.squeeze() method. This removes axes of length one from an array over specific axis. The axis is set using the "axis" parameter. We have set axis 0 here.
The function returns the input array, but with all or a subset of the dimensions of length 1 removed. This is always a itself or a view into the input array. If all axes are squeezed, the result is a 0d array and not a scalar.
The axis selects a subset of the entries of length one in the shape. If an axis is selected with shape entry greater than one, an error is raised.
Steps
At first, import the required library −
import numpy as np
Creating a numpy array using the array() method. We have added elements of int type −
arr = np.array([[[20, 36, 57, 78], [32, 54, 69, 84]]])
Display the array −
print("Our Array...
",arr)
Check the Dimensions −
print("
Dimensions of our Array...
",arr.ndim)
Get the Datatype −
print("
Datatype of our Array object...
",arr.dtype)
Display the shape of array −
print("
Array Shape...
",arr.shape)
Squeeze the Array shape using the numpy.squeeze() method. The axis is set using the "axis" parameter −
print("
Squeeze the shape of Array...
",np.squeeze(arr, axis = 0).shape)
Example
import numpy as np # Creating a numpy array using the array() method # We have added elements of int type arr = np.array([[[20, 36, 57, 78], [32, 54, 69, 84]]]) # Display the array print("Our Array...
",arr) # Check the Dimensions print("
Dimensions of our Array...
",arr.ndim) # Get the Datatype print("
Datatype of our Array object...
",arr.dtype) # Display the shape of array print("
Array Shape...
",arr.shape) # Squeeze the Array shape using the numpy.squeeze() method # The axis is set using the "axis" parameter print("
Squeeze the shape of Array...
",np.squeeze(arr, axis = 0).shape)
Output
Our Array... [[[20 36 57 78] [32 54 69 84]]] Dimensions of our Array... 3 Datatype of our Array object... int64 Array Shape... (1, 2, 4) Squeeze the shape of Array... (2, 4)