find all values in numpy array that lie within a range

Numpy – Find Array Values Within a Range

The Numpy library in Python comes with a number of useful functions and methods to work with and manipulate the data in arrays. In this tutorial, we will look at how to find all the values in a Numpy array that lie within a given range (between two specified values both inclusive).

Steps to get all the array values within a given range in Numpy

find all values in numpy array that lie within a range

You can use boolean indexing to filter the Numpy array such that the resulting array contains only the elements that specify a given condition. For example, values within the range [k1, k2] – values that are greater than or equal to k1 and also less than or equal to k2.

Step 1 – Create a Numpy array

First, we will create a Numpy array that we will be using throughout this tutorial.

import numpy as np

# create a numpy array
ar = np.array([1, 3, 4, 7, 6, 8, 2])
# display the array
print(ar)

Output:

[1 3 4 7 6 8 2]

Here, we used the numpy.array() function to create a one-dimensional Numpy array containing some numbers.

Step 2 – Filter the array using a boolean expression

To find all the values from a Numpy array within a given range, filter the array using boolean indexing.

First, we will specify our boolean expression, (ar >= k1) & (ar <= k2) and then use the boolean array resulting from this expression to filter our original array.

For example, let’s get all the values in the above array that are within the range of 3 to 6 (k1=3, k2=6).

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# values in the range [3, 6]
print(ar[(ar >= 3) & (ar <= 6)])

Output:

[3 4 6]

We get all the values in the array ar that are between 3 and 6 (both inclusive).

To understand what’s happening here, let’s look under the hood. Let’s see what we get from the expression (ar >= 3) & (ar <= 6).

(ar >= 3) & (ar <= 6)

Output:

array([False,  True,  True, False,  True, False, False])

Note that here we combine two boolean expressions here using the & operator. We get a boolean array as result. The boolean values in this array represent whether a value at a particular index satisfies the given condition or not (in our case whether the element is within the range [3, 6]).

When we do ar[(ar >= 3) & (ar <= 6)], we are essentially filtering the original array where the condition evaluates to True.

You can similarly filter a Numpy array for other conditions as well.

Summary – Get all values in Numpy array between two values

In this tutorial, we looked at how to get all the values in a Numpy array that are within a given range. The following is a short summary of the steps mentioned –

  1. Create a Numpy array (skip this step if you already have an array to operate on).
  2. Use boolean indexing to filter the array for only the values that lie within the range [k1, k2], (ar >= k1) & (ar <= k2).

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top