numpy.squeeze() function is used when we want to remove single-dimensional entries from the shape of an array.
Syntax : numpy.squeeze(arr, axis=None )
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 shape entry greater than one, an error is raised.
squeezed [ndarray] The input array, but with all or a subset of the dimensions of length 1 removed. This is always a itself or a view into arr.
Code #1 :
Input array : [[[2 2 2] [2 2 2]]] Shape of input array : (1, 2, 3) output squeezed array : [[2 2 2] [2 2 2]] Shape of output array : (2, 3)
Code #2 :
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)
ValueError : If axis is not None, and an axis being squeezed is not of length 1.
Code #3 :
ValueError Traceback (most recent call last) in () 5 6 print ("Input array : ", in_arr) ----> 7 out_arr = geek.squeeze(in_arr, axis=1) 8 print ("output array : ", out_arr) 9 print("The shapes of Input and Output array : ") ~\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in squeeze(a, axis) 1196 try: 1197 # First try to use the new axis= parameter -> 1198 return squeeze(axis=axis) 1199 except TypeError: 1200 # For backwards compatibility ValueError: cannot select an axis to squeeze out which has size not equal to one
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- set add() in python
- try and except in Python
- chr() in Python
- Python | a += b is not always a = a + b
- max() and min() in Python
- SQL using Python | Set 1
- abs() in Python
- Any & All in Python
- zip() in Python
- bin() in Python
- pow() in Python
- Python Set | pop()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.