Python numpy digitize

The Python numpy digitize function returns the indices of the bins to which the array elements belong. The syntax of this array digitize method is

numpy.digitize(a, bins, right = False)

If a value(s) is beyond the bins boundary, the Python numpy array digitize method returns 0 or len(bins).

  • a – Array of Values
  • bins – Array of bins
  • right – It decides whether the bins are in increasing or decreasing.
rightBins OrderOutput
TrueIncreasingbins[i – 1] < a <= bins[i]
FalseIncreasingbins[i – 1] <= a < bins[i]
TrueDecreasingbins[i – 1] >= a > bins[i]
FalseDecreasingbins[i – 1] > a >= bins[i]

Python numpy digitize array example

We declared an array of nine random numbers in this program to test the digitize function.

np.digitize(a, bins = [15]) – It means if the element in the a array is less than 15, it returns 0, and if it is greater than or equals to 15, it returns 1.

np.digitize(a, bins = [9, 20])

  • If the element is less than 9, it returns zero.
  • If a[n] is between 9 and 20, i.e., 9 ≤ a[n] < 20, it returns 1.
  • If a[n] is greater than or equals 20, it returns 2.

np.digitize(a, bins = [9, 20], right = True)

  • If the element is less than or equals 9, it returns zero.
  • If a[n] is greater than 9 and less than or equals 20, i.e., 9 < a[n] ≤ 20, it returns 1.
  • If a[n] is greater than 20, it returns 2.
import numpy as np

a  = np.array([10, 20, 7, 30, 11, 1, 19, 5, 40])

b = np.digitize(a, bins = [15])
print(b)

c = np.digitize(a, bins = [9, 20])
print(c)

d = np.digitize(a, bins = [9, 20], right = True)
print(d)
[0 1 0 1 0 0 1 0 1]
[1 2 0 2 1 0 1 0 2]
[1 1 0 2 1 0 1 0 2]

Python numpy digitize array example 2

If multiple bin values exist, the start and end conditions will be the same as the above. However, the middle one will increment the buckets. For instance, b =  np.digitize(a, bins)

  • If a[n] is less than 3, it returns 0.
  • If a[n] is greater than or equals 3 and less than 6, it returns 1.
  • If a[n] is greater than or equals 6 and less than 8, it returns 2.
  • If a[n] is greater than or equals 8, it returns 3.
import numpy as np

a  = np.array([1, 12, 5, 2, 7, 9, 4, 8, 10, 2])
bins = np.array([3, 6, 8])

b = np.digitize(a, bins)
print(b)

c = np.digitize(a, bins = [2, 4, 6,  8])
print(c)
[0 3 1 0 2 3 1 3 3 0]
[0 4 2 1 3 4 2 4 4 1]

Count bin Frequency

The numpy modules a bincount() function to count the frequency of each bin, and this example uses the same. 

b = 4 zeros, 2 ones, and 4 twos.

c = 1 zero,  2 ones, 2 twos, 1 three, and 4 fours.

import numpy as np

a  = np.array([1, 12, 5, 2, 7, 9, 4, 8, 10, 2])

b = np.digitize(a, bins = [5, 8])
print(b)
print(np.bincount(b))

c = np.digitize(a, bins = [2, 4, 6,  8])
print(c)
print(np.bincount(c))
[0 2 1 0 1 2 0 2 2 0]
[4 2 4]
[0 4 2 1 3 4 2 4 4 1]
[1 2 2 1 4]