# numpy.squeeze() in Python

In this tutorial, we are going to learn one of the important functions i.e squeeze() of the numpy module in Python.

## numpy.squeeze() function in Python

**numpy.squeeze()** function is used when we want to remove one dimension in the multidimensional array.

For example, if the shape of the array is 3-dimension and we want the 2-dimension array, then we use squeeze() function to remove one dimension in array.

**Syntax: numpy.squeeze**

numpy.squeeze(array, axis=None)

**Parameter:**

**array** = Like input array

**axis** = (* **None or int or tuple of ints, optional*). Axis parameter is to select the subset of the single dimension in the shape or multi-dimension.

**Let’s see the example of numpy.squeeze**

**Let’s see the example of numpy.squeeze**

**Step 1:** Import the numpy module as np

**Step 2:** Creating the one-dimensional array. np.arange(0,12) start from 0 to 12.

import numpy as np #one dimensional array one_dimen = np.arange(0,12) print("\nOne dimensional array:\n", one_dimen)

**Output:**

One dimensional array: [ 0 1 2 3 4 5 6 7 8 9 10 11]

**Step 3:** Now we convert the one-dimensional array into the two-dimensional array and three-dimensional array using np.reshape(3,4) which represents 3 rows and 4 columns and np.reshape(1,3,4) which represent 1 block, 3 rows and 4 columns.

import numpy as np #one dimensional array one_dimen = np.arange(0,12) print("\nOne dimensional array:\n", one_dimen) #Two Dimensional Array two_dimen = one_dimen.reshape(3,4) print("\ntwo dimensional array:\n", two_dimen) print("Shape of the two_dimen:", two_dimen.shape) # Three Dimensional Array three_dimen = one_dimen.reshape(1,3,4) print("\nThree dimensional array:\n", three_dimen) print("Shape of three_dimen:", three_dimen.shape)

**Output:**

One dimensional array: [ 0 1 2 3 4 5 6 7 8 9 10 11] two dimensional array: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] Shape of the two_dimen: (3, 4) Three dimensional array: [[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]] Shape of three_dimen: (1, 3, 4)

**Step 4:** If some situation came where we want the 2-dimensional array from a 3-dimensional array. So now squeeze function come into existence.

By using squeeze function we remove one-dimension in three-dimensional array i.e from three_dimen in program.

import numpy as np #one dimensional array one_dimen = np.arange(0,12) print("\nOne dimensional array:\n", one_dimen) #Two Dimensional Array two_dimen = one_dimen.reshape(3,4) print("\ntwo dimensional array:\n", two_dimen) print("Shape of the two_dimen:", two_dimen.shape) # Three Dimensional Array three_dimen = one_dimen.reshape(1,3,4) print("\nThree dimensional array:\n", three_dimen) print("Shape of three_dimen:", three_dimen.shape) # Removing the one dimension in the array squeeze_three_dimen = np.squeeze(three_dimen, axis=0) print("\nNew Squeezed Array of three_dimen:\n",squeeze_three_dimen) print("Squeeze Shape:", squeeze_three_dimen.shape)

Output:

One dimensional array: [ 0 1 2 3 4 5 6 7 8 9 10 11] two dimensional array: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] Shape of the two_dimen: (3, 4) Three dimensional array: [[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]] Shape of three_dimen: (1, 3, 4) New Squeezed Array of three_dimen: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] Squeeze Shape: (3, 4)

Here, in program line no. 17 show the shape of the three_dimen is (1, 3, 4) which is three dimensional. But after using squeeze function the new dimension is ( 3, 4) that is two dimensional which shown in line no 21.

**You can also learn:**

How to sort Numpy array in Python – Various Ways?

## Leave a Reply