Numpy squeeze – Python NumPy squeeze() Function

NumPy squeeze() Function:

Numpy squeeze: The squeeze() function in NumPy module remove axes of length one from an array

It removes single-dimensional items from the shape of an array.

Syntax:

numpy.squeeze(a, axis=None)

Parameters

a: This is required. It is the input array.

axis: This is optional. None, int, or a tuple of ints can be used. Selects a subset of the entries of length one in the shape. An error is raised if an axis is specified with a shape entry greater than one.

Return Value: 

NP squeeze: The input array with all or a subset of the dimensions of length 1 removed is returned.

NumPy squeeze() Function in Python

Example1

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list(multi-dimensional) as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Print the shape(dimensions) of the given array using the shape function.
  • Pass the given array as an argument to the squeeze() function of numpy module to remove the axes of length one from the above given array.
  • Store it in another variable.
  • Print the above-squeezed array(here axis=None).
  • Print the shape of the squeezed array using the shape function.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list(multi-dimensional) as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[[4, 12, 1],
                 [6, 9, 2],
                 [7, 5, 3]]])
                  
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Print the shape(dimensions) of the given array using the shape function
print("Given array shape is:", gvn_arry.shape)
# Pass the given array as an argument to the squeeze() function of numpy module 
# to remove the axes of length one from the above given array
# Store it in another variable.
rslt_arry = np.squeeze(gvn_arry)
# Print the above squeezed array(here axis=None).
print("The above squeezed array is:")
print(rslt_arry)
# Print the shape of the squeezed array using the shape function
print("Squeezed array shape is:", rslt_arry.shape)

Output:

The above given array is:
[[[ 4 12 1]
 [ 6 9 2]
 [ 7 5 3]]]
Given array shape is: (1, 3, 3)
The above squeezed array is:
[[ 4 12 1]
 [ 6 9 2]
 [ 7 5 3]]
Squeezed array shape is: (3, 3)

Example2

Numpy squeeze: On an axis with a shape greater than one, an array cannot be squeezed.

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list (here with shape (1,3,1)) as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Print the shape of the given array using the shape function.
  • Squeezing an array at an axis with a shape greater than one is impossible. For example, Array has shape = 3 at axis=1, therefore squeezing it at axis=1 will result in an exception.
  • Pass the given array, axis = 0 as the arguments to the squeeze() function of numpy module to squeeze the given array at axis=0.
  • Store it in another variable.
  • Pass the given array, axis = 2 as the arguments to the squeeze() function of numpy module to squeeze the given array at axis=2.
  • Store it in another variable.
  • Print the above obtained squeezed array at axis = 0
  • Print the shape of the squeezed array at axis = 0 using the shape function.
  • Print the above obtained squeezed array at axis = 2
  • Print the shape of the squeezed array at axis = 2 using the shape function.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list(here with shape (1,3,1)) as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[[5],[6],[7]]])
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Print the shape(dimensions) of the given array using the shape function
print("Given array shape is:", gvn_arry.shape)
print()
# Squeezing an array at an axis with a shape greater than one is impossible. 
# For example, Array has shape = 3 at axis=1, therefore squeezing it at
# axis=1 will result in an exception.

# Pass the given array, axis = 0 as the arguments to the squeeze() function of numpy module 
# to squeeze the given array at axis=0
# Store it in another variable.
rslt_arry1 = np.squeeze(gvn_arry, axis=0)
# Pass the given array, axis = 2 as the arguments to the squeeze() function of numpy module 
# to squeeze the given array at axis=2
# Store it in another variable.
rslt_arry2 = np.squeeze(gvn_arry, axis=2)
# Print the above obtained squeezed array at axis = 0 
print("The above obtained squeezed array at axis = 0:")
print(rslt_arry1)
# Print the shape of the squeezed array at axis = 0 using the shape function
print("The shape of the squeezed array at axis = 0 is:", rslt_arry1.shape)
print()
# Print the above obtained squeezed array at axis = 2 
print("The above obtained squeezed array at axis = 2:")
print(rslt_arry2)
# Print the shape of the squeezed array at axis = 2 using the shape function
print("The shape of the squeezed array at axis = 2 is:", rslt_arry2.shape)

Output:

The above given array is:
[[[5]
 [6]
 [7]]]
Given array shape is: (1, 3, 1)

The above obtained squeezed array at axis = 0:
[[5]
 [6]
 [7]]
The shape of the squeezed array at axis = 0 is: (3, 1)

The above obtained squeezed array at axis = 2:
[[5 6 7]]
The shape of the squeezed array at axis = 2 is: (1, 3)