Tuesday, December 1, 2020

Python Pool: Numpy Squeeze in Python With Examples

Hello programmers, in this article, we will discuss the Numpy squeeze function in Python. The squeeze () function is used when we want to remove single-dimensional entries from the shape of an array. Whenever we want to change the shape of a three-dimensional array to a two-dimensional array, we make use of the squeeze() function in NumPy. Hence, the squeeze() function returns the input array with the subset of the dimension having a length equal to one removed from the array. Before we cite examples to show the working of numpy.squeeze() function, let me just brief you about the syntax, parameters, and return type of the same.

Syntax of Numpy squeeze

numpy.squeeze(a, axis=None)

Parameters of Numpy Squeeze:

  • a: Represents the Input data.
  • axis: Signifies an int or tuple of int values. It selects a subset of the single-dimensional entries in the shape. An error is raised, If an axis is selected with a shape entry greater than one.

Return type of Squeeze

This squeeze() function returns an output array similar to input array input, but with all or a subset of the dimensions of length 1 removed.

Example of Squeeze Function in Python

import numpy as np
a = np.array([[[0], [2], [4]]])
print(a.shape)

b = np.squeeze(a).shape
print(b)

OUTPUT:

(1, 3, 1)
(3)

EXPLANATION:

The above example is a very basic implementation of the squeeze function. An array ‘a’ is created returning the shape as (1, 3, 1). On passing ‘a’ to the squeeze() function, its shape is returned as (3) i.e., all the dimensions of length 1 is removed.

Numpy Squeeze for axis = 0

import numpy as np
a = np.arange(9).reshape(1, 3, 3)  
  
print ("Input array : ", a)   
b = np.squeeze(a , axis = 0)  
  
print ("output array : ", b)   
print("The shapes of Input and Output array : ")  
  
print(a.shape, b.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)

EXPLANATION:

In the above example, an array s defined using numpy.arrange() function of shape specified as (1, 3, 3). When this array is passed to the squeeze() function, the input array is returned having the same dimension and number of elements. Just the dimension of length 1 is removed. Hence the shape is retuned as (3,3). The axis parameter is not any, it is set to 0, which means the axis being squeezed is not of length 1.

Matrix squeeze

import numpy as np 
             
# make matrix with numpy 
a = np.matrix('[[4], [8]]') 
             
# applying matrix.squeeze() method 
b = a.squeeze() 
   
print(b) 

OUTPUT:

[[ 4 8]]

EXPLANATION:

With the help of Numpy matrix.squeeze() method, we are able to squeeze the size of a matrix. In this method, the Nx1 size of the input matrix is given out as a 1xN size of the output matrix. In the above example, a numpy matrix is defined using the np.matrix function. And then the numpy squeeze function is used to squeeze the matrix and give the output as [[ 4 8 ]] from the originally created matrix i.e., [ [4], [8] ].

Conclusion

In this article, we have seen the example and implementation of Numpy Squeeze in Python. We have also seen the use of Squeeze in a matrix as well. Refer to this article for any queries related to the Squeeze function.

The post Numpy Squeeze in Python With Examples appeared first on Python Pool.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...