NumPy Squeeze in Python

Introduction:

In this tutorial we are learning about the NumPy squeeze in Python. We want to remove the size of a certain number of variables, so we use a function called "squeeze()" in NumPy. In this case, we use NumPy's squeeze() function, which accepts an array name and axis parameter. The squeeze() function returns the input array whose length is a subset of the length equal to 1 subtracted from the array. When we want to remove a single-dimensional entry from the image of a directory, then we can use the numpy.squeeze() function.

Syntax:

The syntax of the NumPy squeeze in Python is given below -

Parameters:

The parameters of the NumPy squeeze in Python are given below -

  • arr: [array_like] It is an input array.
  • axis=None: The value of the axis is None or int or tuple of ints] It is an optional parameter. It selects a subset of one-dimensional inputs in an image. If the selected axis has an input value greater than 1, then an error occurs.

Return value:

The return value of the NumPy squeeze in Python is given below -

squeezed [ndarray] Input array with all or one of the Length 1 dimensions removed. This is always arr itself or the appearance of arr.

What is the working principle of the NumPy squeeze in Python?

The working principle of the NumPy squeeze in Python is given below -

  1. When we want to remove dimensions from a multi-dimensional array, we use the function called "squeeze()" in NumPy. For example, if we want to convert a three-dimensional array to a two-dimensional array, then we can use the squeeze() function in NumPy.
  2. The squeeze() function in NumPy takes two parameters, which are the arr and the axis.
  3. Use the axis option to specify the geometry of a portion of the array along one dimension. Its value must be a number and cannot exceed the number of dimensions in the array.
  4. The squeeze() function returns the input array whose length is a subset of the length equal to 1 subtracted from the array.

Program Code 1:

Here we give a program code of the NumPy squeeze in Python. The code is given below -

Output:

Now, we run the above code and find the input and output array and the shape of them. The output is given below -

The input array is: [[[3 3 3 3]
  [3 3 3 3]]]
The shape of the input array is: (1, 2, 4)
The output squeezed array is: [[3 3 3 3]
 [3 3 3 3]]
The shape of the output array is: (2, 4)

Program Code 2:

Here we give another program code of the NumPy squeeze in Python. The code is given below -

Output:

Now, we run the above code and find the input and the output array and the shape of them. The output is given below -

The input array is: [[[0 1 2]
  [3 4 5]
  [6 7 8]]]
The output array is: [[0 1 2]
 [3 4 5]
 [6 7 8]]
The shape of the input and the output array is:
(1, 3, 3) (3, 3)

Program Code 3:

Here we give another program code of the NumPy squeeze in Python. The code is given below -

Output:

Now, we run the above code, and a value error is occurring here. The output is given below -

The input array is: [[[0 1 2]
  [3 4 5]
  [6 7 8]]]
ERROR!
Traceback (most recent call last):
  File "<main.py>", line 8, in <module>
  File "/usr/local/lib/python3.11/site-packages/numpy/core/fromnumeric.py", line 1558, in squeeze
    return squeeze(axis=axis)
           ^^^^^^^^^^^^^^^^^^
ValueError: cannot select an axis to squeeze out which has size not equal to one

Program Code 4:

Here we give another program code of the NumPy squeeze in Python. The Python program demonstrates the process of creating a three-dimensional array and converting the image of the created three-dimensional array into a two-dimensional array. The code is given below -

Output:

Now, we run the above code and find the input and output array and the shape of them. Installs the NumPy library to support features such as arrays, shapes, and squeeze. The array function in NumPy is used to create a three-dimensional array stored in the arr variable. The arr elements are then displayed, followed by empty lines represented by \n. Then use the squeeze function in NumPy to compress the newly created three-dimensional array into a two-dimensional array. The output is given below -

The input array is:
[[[1 3 3 2]
  [1 5 6 8]
  [7 0 1 4]]]


The shape of the input array is:
(1, 3, 4)


The output squeezed array is:
[[1 3 3 2]
 [1 5 6 8]
 [7 0 1 4]]


The shape of the output array is:
(3, 4)

Conclusion:

In this tutorial, we learn about the NumPy squeeze in Python. We learn the syntax and parameters of the NumPy squeeze in Python. It is a method of removing an entry from the original shape of a series. We can understand the different uses of the NumPy squeeze() function with the help of some examples. We are looking at the special case where we will get a ValueError when the axis value is greater than or equal to 1.