search
Search
Unlock 100+ guides
search toc
close
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
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

# NumPy | digitize method

schedule Aug 10, 2023
Last updated
local_offer
PythonNumPy
Tags
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

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 = [5]bins = [5, 6]np.digitize(a, bins) # or right=False array([1]) ```

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 = [5]bins = [5, 6]np.digitize(a, bins, right=True) array([0]) ```

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

Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!