numpy.squeeze() in Python
The numpy.squeeze() function removes single-dimensional entries from the shape of an array.
Syntax
numpy.squeeze(a, axis=None)
Parameter
The numpy.squeeze() function has three parameters which are as follows:
a: It represents the Input data.
axis: This parameter signifies an int or tuple of int values. It selects a subset of the single-dimensional entries in the shape. If an axis is selected with shape entry greater than one, an error is raised.
Return
This function returns an input array, but with all or a subset of the dimensions of length 1 removed.
Example 1
#Python Program explaining
#numpy.squeeze() function
import numpy as np
in_array = np.array([[[12, 42, 22], [9, 12, 2]]])
print ("Input array : ", in_array)
out_array = np.squeeze(in_array)
print ("output squeezed array : ", out_array)
Output
Input array : [[[12 42 22]
[ 9 12 2]]]
output squeezed array : [[12 42 22]
[ 9 12 2]]
Example 2
#Python Program explaining
#numpy.squeeze() function
import numpy as np
in_array = np.arange(8).reshape(1, 4, 2)
print ("Input array : ", in_array)
out_array = np.squeeze(in_array, axis = 0)
print ("output array : ", out_array)
Output
Input array : [[[0 1]
[2 3]
[4 5]
[6 7]]]
output array : [[0 1]
[2 3]
[4 5]
[6 7]]