search
Search
Map of Data Science
Guest 0reps
exit_to_appLog out
Map of data science
Thanks for the thanks!
close
account_circle
Profile
exit_to_app
Sign out
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview Doc Search Code Search Beta SORRY NOTHING FOUND!
mic
Start speaking... Voice search is only supported in Safari and Chrome.
Shrink
Navigate to
A
A
brightness_medium
share
arrow_backShare Twitter Facebook

# NumPy | digitize method

NumPy
chevron_right
Documentation
schedule Jul 1, 2022
Last updated
local_offer PythonNumPy
Tags
expand_more
map
Check out the interactive map of data science

Numpy's `digitize(~)` method returns a Numpy array of indices of the bins to which the values in the input array belongs to. To explain in plain words is difficult, so please look at the examples for clarification.

# Parameters

1. `a` | `array-like`

The array of values.

2. `bins` | `array-like`

The array of bins, which must be one-dimensional and sorted in ascending order.

3. `right` | `boolean` | `optional`

If True, then value will be placed in the next bin at the endpoints. If False, then value will be placed in the previous bin. By default, `right=False`.

# Return value

A Numpy array of integer indices.

# Examples

## Basic usage

Consider the following code snippet:

``` # Our array of valuesa = [3, 6.5, 9]# Our binsbins = [5, 6, 7, 8]np.digitize(a, bins) array([0, 2, 4]) ```

Let's understand the output here.

• The first value 3 is between 3 <= 5 (the first bin), so the returned integer index is 0.

• The second value 6.5 is between 6 and 7 (2nd and 3rd bin), so the returned integer index is 2.

• The third value 9 is larger than 8 (the 4th bin), so the returned integer index is 4.

A nice way of wrapping your head around this is to think of the index of the value if it were to be inserted into the bins array. For instance, the value 3 will be inserted into index 0, so 0 is returned. 6.5 will be inserted into index 2, so 2 is returned, and so on.

## Handling the endpoints

By default, when checking for which bin to place a value in, Numpy will use the < comparison. For instance,

``` a = bins = [5, 6]np.digitize(a, bins) # or right=False array() ```

The reason we get an integer index of 1 is that the first comparison we perform is 5 < 5, which evaluates to `False`.

Instead of a < comparison, we can perform a <= comparison, like follows:

``` a = bins = [5, 6]np.digitize(a, bins, right=True) array() ```

Here, we get a integer index of 0 because the first comparison 5 <=5 evaluates to `True`.