Updated June 15, 2023

## Introduction to NumPy squeeze

Whenever we want to remove one dimension from the given multi-dimensional array, we use a function called squeeze() in NumPy.For example, we will change the shape of a three-dimensional array to a two-dimensional one. In that case, we use the squeeze() function in NumPy, which takes the arrayname and axis parameters. The squeeze() function returns the input array with the subset of the dimension having a length equal to one removed from the array.

**Syntax:**

`numpy.squeeze(arrayname, axis=None)`

where arrayname is the name of the array that represents the array whose dimensions are to change an axis represents the shape of single dimensional entries subset, and its value cannot be more than one.

### Working of NumPy squeeze

- Whenever we want to remove one dimension from the given multi-dimensional array, we make use of a function called squeeze() in NumPy; for example, if we’re going to change the shape of the three-dimensional array to a two-dimensional array, we make use of squeeze() function in NumPy.
- The squeeze() function in NumPy takes the two parameters, arrayname, and axis.
- To specify the geometry of a portion of the array along a single dimension, use the axis option. Its value must be a single integer and cannot exceed the number of dimensions in the array.
- The squeeze() function returns the input array with the subset of the dimension having a length equal to one removed from the array.

**Code:**

```
#importing the package called numpy to enable us to use array function, shape function and squeeze function
import numpy as nump
#Creating a three dimensional array by making use of array function in NumPy and storing it in a variable called namearray
namearray = nump.array([[[ 0, 9, 8, 7],[ 6, 5, 4, 3],[ 2, 1, 0, 9]]])
#Displaying the elements of namearray followed by one line space by making use of \n
print 'The elements of the given array are:'
print namearray
print '\n'
#displaying the shape of the newly created array using shape function
print 'The shape of the given array is:'
print namearray.shape
print '\n'
#using squeeze function in NumPy to squeeze the newly created three dimensional array to a two dimensional array
squeezedarray = nump.squeeze(namearray,axis=0)
#displaying the elements of the squeezed array
print 'The elements of the squeezed array are:'
print squeezedarray
print '\n'
#Displaying the shape of the squeezed array using shape function
print 'The shape of the squeezed array is:'
print squeezedarray.shape
```

**Output:**

The program above can use NumPy’s array function, shape function, and squeeze function by importing the numpy package. The NumPy library creates a three-dimensional array by using the array function, and the resulting array is stored in the array variable. After displaying the array’s name and its elements, an empty line is printed. The shape function is then used to display the shape of the newly created array. Then squeeze function in NumPyis used to squeeze the freshly created three-dimensional array into a two-dimensional array.

#### Example #2

Python program to demonstrate the function to create a three-dimensional array and change the shape of the created three-dimensional array to a two-dimensional array:

**Code:**

```
#importing the package called numpy to enable us to use array function, shape function and squeeze function
import numpy as nump
#Creating a three dimensional array by making use of array function in NumPy and storing it in a variable called namearray
namearray = nump.array ([[[ 5, 4, 3, 2],[ 1, 6, 7, 8],[ 9, 0, 5, 4]]])
#Displaying the elements of namearray followed by one line space by making use of \n
print 'The elements of the given array are:'
print namearray
print '\n'
#displaying the shape of the newly created array using shape function
print 'The shape of the given array is:'
print namearray.shape
print '\n'
#using squeeze function in NumPy to squeeze the newly created three dimensional array to a two dimensional array
squeezedarray = nump.squeeze(namearray,axis=0)
#displaying the elements of the squeezed array
print 'The elements of the squeezed array are:'
print squeezedarray
print '\n'
#Displaying the shape of the squeezed array using shape function
print 'The shape of the squeezed array is:'
print squeezedarray.shape
```

**Output:**

The NumPy library is loaded to enable the use of functions such as array, shape, and squeeze. The array function from NumPy is used to create a three-dimensional array, which is stored in the variable name_array. The items of name_array are then displayed, followed by an empty line denoted by \n. Then squeeze function in NumPyis used to squeeze the newly created three-dimensional array into a two-dimensional array.

### Recommend ed Articles

We hope that this EDUCBA information on “NumPy squeeze” was beneficial to you. You can view EDUCBA’s recommended articles for more information.