## Introduction to 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 want to change the shape of three dimensional array to two-dimensional array, we make use of the squeeze() function in NumPy which takes the two parameters arrayname and axis. Where arrayname represents the array whose dimensions are to be changed and the axis represents the shape of single-dimensional entries subset and its value cannot be more than one and if it is more than one, in any case, an error is thrown and 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 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 and if it is more than one, in any case, an error is thrown.

### Working of NumPy squeeze

Working of NumPy squeeze is as follows:

- 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 want to change the shape of three dimensional array to two dimensional array, we make use of squeeze() function in NumPy.
- The squeeze() function in NumPy takes the two parameters arrayname and axis.
- arrayname represents the array whose dimensions are to be changed.
- axis represents the shape of a single dimensional entries subset and its value cannot be more than one and if it is more than one in any case, an error is thrown.
- The squeeze() function returns the input array with the subset of the dimension having a length equal to one removed from the array.

### Examples of NumPy squeeze

Following are the examples as given below:

#### Example #1

Python program to demonstrate NumPysqueeze 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([[[ 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:**

In the above program, a package called numpy is imported which allows us to make use of array function, shape function and squeeze function in NumPy. Then a three dimensional array is created by making use of the array function in NumPy and storing it in a variable called namearray. Then the elements of the array namearray are displayed followed by one line space by making use of \n. Then the shape of the newly created array is displayed using shape function. Then squeeze function in NumPyis used to squeeze the newly created three dimensional array to a two dimensional array. Then the elements of the squeezed array are displayed. Then the changed shape of the squeezed array is displayed using shape function.

#### Example #2

Python program to demonstrate NumPy squeeze 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:**

In the above program, a package called numpy is imported which allows us to make use of array function, shape function, and squeeze function in NumPy. Then a three dimensional array is created by making use of the array function in NumPy and storing it in a variable called namearray. Then the elements of the array namearray is displayed followed by one line space by making use of \n. Then the shape of the newly created array is displayed using shape function. Then squeeze function in NumPyis used to squeeze the newly created three dimensional array to a two dimensional array. Then the elements of the squeezed array are displayed. Then the changed shape of the squeezed array is displayed using shape function.

### Recommend ed Articles

This is a guide to NumPy squeeze. Here we also discuss the introduction and working of numpy squeeze max along with different examples and its code implementation. You may also have a look at the following articles to learn more –

4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion

4.5

View Course

Related Courses