Open In App

Python | Numpy np.digitize() method

With the help of np.digitize() method, we can get the indices of the bins to which the each value is belongs to an array by using np.digitize() method.

Syntax : np.digitize(Array, Bin, Right)
Return : Return an array of indices of the bins.



Example #1 :
In this example we can see that by using np.digitize() method, we are able to get the array of indices of the bin of each value which belongs to an array by using this method.




# import numpy
import numpy as np
  
a = np.array([1.2, 2.4, 3.6, 4.8])
bins = np.array([1.0, 1.3, 2.5, 4.0, 10.0])
  
# using np.digitize() method
gfg = np.digitize(a, bins)
  
print(gfg)

Output :

[1 2 3 4]



Example #2 :




# import numpy
import numpy as np
  
a = np.array([[10.2, 21.4, 3.6, 14.8], [1.0, 5.0, 10.0, 15.0]])
bins = np.array([1.0, 1.3, 2.5, 4.0, 10.0])
  
# using np.digitize() method
gfg = np.digitize(a, bins)
  
print(gfg)

Output :

[[5 5 3 5]
[1 4 5 5]]

Article Tags :