This tutorial will discuss about unique ways to remove elements from numpy array that are less than a value.
Table of Contents
Using [] Operator with NumPy Array
Suppose we have an NumPy Array of numbers,
numbers = np.array([22, 45, 34, 78, 39, 47])
Now, we want to delete all the values from this array which are less than 40 i.e. a threshold value.
For this, first we will create a boolean mask/array by comparing the NumPy array and threshold value using the greater than or equal to operator. It will return a boolean array or mask, where each True value denotes set the corresponding value in the original array is greater than the threshold value i.e. 40.
# Define the threshold value threshold = 40 # Create a boolean mask for the elements that # are greater than or equal to the threshold value mask = numbers >= threshold
Then we will pass this boolean array into the subscript operator of numpy array. It will return a copy of the numpy array after removing all the values for which provided boolean mask had value False.
# Use the mask to create a new array with # the specified elements removed numbers = numbers[mask]
Basically it will remove all the values from numpy array which are less than the threshold value that is 40.
Let’s see the complete example,
Frequently Asked:
- Delete elements from a Numpy Array by value or conditions in Python
- Remove All Occurrences of a Value from NumPy Array
- Remove Array elements that are in Another Array – Python
- Remove NaN or Infinite Values from a NumPy Array
import numpy as np # Create a NumPy Array numbers = np.array([22, 45, 34, 78, 39, 47]) # Define the threshold value threshold = 40 # Create a boolean mask for the elements that # are greater than or equal to the threshold value mask = numbers >= threshold # Use the mask to create a new array with # the specified elements removed numbers = numbers[mask] # Print the NumPy array print(numbers)
Output
[45 78 47]
Using numpy.where() function
In this solution, we will fetch the index position of all the elements which are less than threshold value in the number array.
For these we will compare the number array with the threshold value using <
operator. It will return in boolean sequence ,where each true value denotes set the corresponding value in array is less than the threshold value.
# Define the threshold value threshold = 40 numbers < threshold
Then we will pass this boolean sequence into the where() function of numpy module. Like this,
# Use the `where` function to identify the indices # of the elements to remove indicesToRemove = np.where(numbers < threshold)
It will return an array of index positions of elements which were true in the boolean sequence, basically the index position of elements which are less than the threshold value in the number array.
Then we will use the delete() function from numpy module to delete elements from this index positions.
# Use the delete() function to remove the elements # at the specified indices numbers = np.delete(numbers, indicesToRemove)
It will delete all the elements from array which are less than the threshold value.
In the below example we are going to delete all the elements from the numpy array which are less than 40.
Let’s see the complete example,
import numpy as np # Create a NumPy Array numbers = np.array([22, 45, 34, 78, 39, 47]) # Define the threshold value threshold = 40 # Use the `where` function to identify the indices # of the elements to remove indicesToRemove = np.where(numbers < threshold) # Use the delete() function to remove the elements # at the specified indices numbers = np.delete(numbers, indicesToRemove) # Print the numpy array print(numbers)
Output
[45 78 47]
Summary
We learned two ways to remove elements from NumPy Array that are less than a threshold value.