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

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 `extract(~)` method extracts values from the input array that pass the provided criteria.

# Parameters

1. `condition` | `array_like`

A boolean mask (i.e. an array whose values can be either `True` or `False`). Indices whose value is `True` will be extracted from the input array.

2. `arr` | `array_like`

The array to perform the method on.

WARNING

The ordering of the parameters is peculiar

Almost all Numpy functions have the input array as the first argument, followed by additional parameters. However, weirdly enough, for the `extract(~)` method, the input array comes AT THE END.

# Return value

A Numpy array with the desired values extracted from the input array.

# Examples

## Extracting values based on a criteria

Suppose we wanted to extract all values greater than 3 from a Numpy array. Our first step is to create a boolean mask based on this condition, like follows:

``` a = np.array([1,2,3,4,5])condition = (a > 3)condition array([False, False, False, True, True]) ```

The important code here is `(a>3)`, which iterates over the Numpy array and for each value, flag `True` if the condition is met and flag `False` if not met. In this example, only the values `4` and `5` fit our criteria, and so that's why we see the boolean `True` at their indices.

The next step is to use the `extract(~)` method to get the values whose indices are flagged as `True` in the condition array.

``` np.extract(condition, a) array([4, 5]) ```

Great, we've extracted all the values that met our criteria.

## Extracting values based on multiple criteria

In the previous example, we just had one condition of `> 3`. To add multiple conditions, use the & operand:

``` a = np.array([1,2,3,4,5])condition = (a > 3) & (a % 2 == 0)condition array([False, False, False, True, False]) ```

Here, we're trying to extract values that are both greater than 3 and even. Once again, supply the boolean mask to the `extract(~)` method like follows:

``` np.extract(condition, a) array([4]) ```
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!