# numpy.squeeze() – The NumPy squeeze Python Function

## Introduction to NumPy squeeze Function

When we wish to remove single-dimensional entries from the form of an array, we utilize the numpy.squeeze() function.

Syntax:

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

Parameters:

• arr : [array_like] Input array.
• axis : [None or int or tuple of ints, optional] Selects a subset of the single-dimensional entries in the shape. If an axis is selected with a shape entry greater than one, an error is raised.

Return: squeezed [ndarray] The input array, but with all or a subset of the dimensions of length 1 removed. This is always itself or a view into arr.

## Code Examples of NumPy squeeze Function

Example 01:

``````# welcome to softhunt.net
# Python program explaining
# numpy.squeeze function

import numpy as np

in_arr = np.array([[[1, 2, 3], [4, 5, 6]]])

print ("Input array : \n", in_arr)
print("Shape of input array : ", in_arr.shape)

out_arr = np.squeeze(in_arr)

print ("output squeezed array : \n", out_arr)
print("Shape of output array : ", out_arr.shape)``````

Output:

```Input array :
[[[1 2 3]
[4 5 6]]]
Shape of input array :  (1, 2, 3)
output squeezed array :
[[1 2 3]
[4 5 6]]
Shape of output array :  (2, 3)```

Example 02:

``````# welcome to softhunt.net
# Python program explaining
# numpy.squeeze function
import numpy as geek
in_arr = geek.arange(9).reshape(1, 3, 3)

print ("Input array : \n", in_arr)
out_arr = geek.squeeze(in_arr, axis = 0)

print ("output array : \n", out_arr)
print("The shapes of Input and Output array : ",in_arr.shape, out_arr.shape)``````

Output:

```Input array :
[[[0 1 2]
[3 4 5]
[6 7 8]]]
output array :
[[0 1 2]
[3 4 5]
[6 7 8]]
The shapes of Input and Output array :  (1, 3, 3) (3, 3)```

Note: A value error occurs If the axis is not None, and an axis being squeeze is not of length 1. As you can see in the below example

Example 03:

``````# welcome to softhunt.net
# Python program explaining
# numpy.squeeze function
import numpy as geek
in_arr = geek.arange(9).reshape(1, 3, 3)

print ("Input array : \n", in_arr)
out_arr = geek.squeeze(in_arr, axis = 1)

print ("output array : \n", out_arr)
print("The shapes of Input and Output array : ",in_arr.shape, out_arr.shape)``````

Output:

## FAQs

### How do I reduce the size of a NumPy array?

numpy.squeeze() may be use to remove all dimensions of size 1 from a NumPy array. And ndarray.squeeze() is also a function given by ndarray.

### What does NumPy atleast_2d do?

When we wish to convert inputs to arrays with at least two dimensions, we utilize the atleast_2d() function. Higher-dimensional inputs are maintain while scalar and 1-dimensional inputs are transform into 2-dimensional arrays.

### How do you sum an array in NumPy?

The NumPy module of Python includes the numpy.sum() function. The sum of all items, the sum of each row, and the sum of each column of a specified array are computed using this function.

``````import numpy as np
a=np.array([[1,4],[3,5]])
b=np.sum(a,axis=1)
print(b) ``````

Output:

`[5 8]`

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

## NumPy squeeze: Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

1. numpy.expand_dims() – The NumPy expand_dims Python Function