- 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 specific axis 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.
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([[[57, 78], [54, 69]]])
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([[[57, 78], [54, 69]]]) # 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... [[[57 78] [54 69]]] Dimensions of our Array... 3 Datatype of our Array object... int64 Array Shape... (1, 2, 2) Squeeze the shape of Array... (2, 2)
- Related Articles
- Remove axes of length one from an array over axis 0 in Numpy
- Remove axes of length one with specific axis in Numpy
- Remove axes of length one from an array in Numpy
- Remove axes of length one from the masked array in Numpy
- Expand the shape of an array over specific axis in Numpy
- Expand the shape of an array over axis 1 in Numpy
- Expand the shape of an array over axis 0 in Numpy
- Expand the shape of an array over tuple of axis in Numpy
- Unpack elements of a uint8 array into a binary-valued output array over specific axis in Numpy
- Pack the elements of a binary-valued array into bits in a uint8 array over specific axis in Numpy
- Join a sequence of arrays with stack() over specific axis in Numpy
- Return a view of the masked array with axes transposed along given axis in NumPy
- Return the average of the masked array elements over axis 0 in Numpy
- Compute the maximum of the masked array elements over axis 0 in Numpy
- Compute the maximum of the masked array elements over axis 1 in Numpy
