Python | Numpy np.digitize() method (original) (raw)

Last Updated : 02 Sep, 2024

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

**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.

Python3 1=1 `

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 :

Python3 1=1 `

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]]

Similar Reads