numpy.extract() in Python

This article is about using extract() function of NumPy module in Python. NumPy is a module for high dimensional array and matrices. Which also includes mathematical operations. numpy.extract() in python is used to return an array that satisfies a certain condition.

Syntax of numpy.extract() function

numpy.extract(condition, arr)

It returns a numpy array that satisfies the condition. To use it, we first need to install the Pandas library. You can find detailed instructions to do that here. To check the successful installation of NumPy, execute the following line of code:

import numpy as np

So, moving on let’s define our condition for which we want to use numpy.extract() function. Suppose, we have to extract all the multiples of 5 from a given array.

Let’s start by initializing the array:

arr = np.arange(10,20).reshape(5,2)


array([[10, 11], [12, 13], [14, 15], [16, 17], [18, 19]])

Our condition would be:

condition = np.mod(arr, 5) == 0

Now finally, using both the parameters, condition and array for numpy.extract(), we segregate integers divisible by 5.

np.extract(condition, arr)


array([10, 15])

Further Reading:

Leave a Reply

Your email address will not be published. Required fields are marked *