Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

numpy
python
communitycreator

How to extract numbers within a range from a NumPy array

Harsh Jain

Introduction

NumPy was created to perform scientific computing in Python. The NumPy library enables the user to create N-dimensional arrays, and perform linear algebra operations on NumPy objects.

In this shot, we will explore different ways in which we can extract the required elements from a NumPy array.

Using the where() method

The where() method accepts an expression to extract the required elements from the array. This expression should return a boolean value. Let’s see how the where() method works in the code.

import numpy as np

# Create a NumPy array
numpy_array = np.array([2, 3, 6, 9, 11, 23, 34, 66])

# Using where() method
indices = np.where((numpy_array >= 7) & (numpy_array < 11))
print("Indices: ", indices)
print("Elements satisfied the condition: ",numpy_array[indices])
Using the where() method in NumPy

Explanation

  • In line 1, we import the numpy package.

  • In line 4, we create an array and specify the number of elements to be 10, starting from 11 to 1010.

  • In line 7, we use the where() method and specify a condition to extract the elements which are greater than or equal to 7 and less than 11. This function returns the indices of the elements which satisfy the condition.

  • In line 8, we print the indices of the elements that satisfied the condition.

  • We use these indices and extract and print the elements in line 9.

Using the logical_and() method

The logical_and() method from the numpy package accepts multiple conditions or expressions as a parameter. Each of the conditions or the expressions should return a boolean value. These boolean values are used to extract the required elements from the array. The logical_and() function will return a list of boolean elements.

Each boolean element represents whether the element in the array, at the same index of the boolean value, satisfies all the conditions or expressions that were passed to the logical_and() method. The boolean element is True only when all the conditions provided to this function are satisfied. Let’s see how the logical_and() method works in the code.

import numpy as np

# Create a NumPy array
numpy_array = np.array([2, 3, 6, 9, 11, 23, 34, 66])

# using logical_end() method
indices = np.where(np.logical_and(numpy_array >= 7, numpy_array < 11))
print("Indices: ", indices)
print("Elements satisfied the condition: ",numpy_array[indices])
Using the logical_end() method

Explanation

  • In line 1, we import the numpy package.

  • In line 4, we create an array and specify the number of elements to be 10, ranging from 1 to 10.

  • In line 7, we use the logical_and() method and specify two conditions to extract the elements, which are greater than or equal to 7 and less than 11.

    • As discussed, the logical_and() method returns a list of boolean values.
    • These boolean values represents the index of the element from the array that satisfies the conditions.
    • This list of boolean values is then passed to the where() method, where it will be determined which element to consider.
  • In line 8, we print the indices of the elements that satisfied the condition.

  • We use these indices and extract and print the elements in line 9.

Applying conditions on the NumPy arrays

The third way to extract the elements from the NumPy array is to provide all the conditions to the array itself. Let’s see how this will work in the code.

import numpy as np

# Create a NumPy array
numpy_array = np.array([2, 3, 6, 9, 11, 23, 34, 66])

# Using condition on NumPy array
condition_array =  numpy_array[(numpy_array >= 7) & (numpy_array <= 14)]
print("Elements satisfied the condition: ",condition_array)
Using conditions on the NumPy array

Explanation

  • In line 1, we import the numpy package.

  • In line 4, we create an array and specify the number of elements to be 10, ranging from 1 to 10.

  • In line 7, we specify two conditions directly to the array.

    • Each condition is in the form of a tuple . We are using these conditions to extract the elements which are greater than or equal to 7 and less than or equal to 14.
    • The array will directly return the elements that satisfy all the conditions.
  • In line 8, we print the extracted elements.

RELATED TAGS

numpy
python
communitycreator
RELATED COURSES

View all Courses

Keep Exploring