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 | argwhere method

schedule Aug 12, 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 `argwhere(~)` method returns the indices of non-zero values.

# Parameters

1. `a` | `array_like`

The input array.

# Return value

A NumPy array of indices of non-zero values. If the input array is 1D or 2D, then the returned array will be 2D.

# Examples

## Basic usage

``` x = np.array([1,2,3])np.argwhere(x) array([[0],       [1],       [2]]) ```

Here, since all values in x are non-zero, all indices are returned. Notice the returned array is 2D.

In NumPy, `False` values are equivalent to 0, while `True` values are equivalent to 1. This means that the `argwhere(~)` can be used for masks.

Suppose we wanted to determine the indices of values larger than 2. We can do so like follows:

``` x = np.array([1,2,3,4])mask = x > 2 array([False, False, True, True]) ```

``` np.argwhere(mask) array([[2],       [3]]) ```

## Two-dimensional arrays

Suppose we wanted to determine the indices of values larger than 2 in a 2D Numpy array

``` x = np.array([[1,2],[3,4],[5,6]])mask = x > 2 array([[False, False],       [ True, True],       [ True, True]]) ```

``` np.argwhere(mask) array([[1, 0],       [1, 1],       [2, 0],       [2, 1]]) ```

The way to interpret this is as follows:

``` [1,0] -> row 1, column 0 is non-zero (i.e. greater than 2)[1,1] -> row 1, column 1 is non-zero[2,0] -> row 2, column 0 is non-zero[2,1] -> row 2, column 1 is non-zero ```
Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
1
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!