NumPy – squeeze

NumPy - squeeze

In this chapter, we will discuss about NumPy – squeeze. This function removes the one-dimensional entry from the shape of the given array. Two parameters are required for this function.

numpy.squeeze(arr, axis)

Where,

Sr.No.Parameter & Description
1arrInput array
2axisint or tuple of int. selects a subset of single-dimensional entries in the shape

Example Of NumPy squeeze

import numpy as np  
x = np.arange(9).reshape(1,3,3) 

print 'Array X:' 
print x 
print '\n'  
y = np.squeeze(x) 

print 'Array Y:' 
print y 
print '\n'  

print 'The shapes of X and Y array:' 
print x.shape, y.shape

Its output is as follows −

Array X:
[[[0 1 2]
 [3 4 5]
 [6 7 8]]]

Array Y:
[[0 1 2]
 [3 4 5]
 [6 7 8]]

The shapes of X and Y array:
(1, 3, 3) (3, 3)

Next Topic – Click Here

This Post Has 2 Comments

Leave a Reply