Remove elements from Array Greater than a Value in Python

This tutorial will discuss about unique ways to remove elements from numpy array that are greater than a value.

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 greater 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 < operator. It will return a boolean array or mask, where each True value denotes set the corresponding value in the original array is smaller than the threshold value i.e. 40.

```# Define the threshold value
threshold = 40

# Create a boolean mask for the elements that
# are smaller than or equal to the threshold value
```

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

Basically it will remove all the values from numpy array which are Greater than the threshold value that is 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

# Create a boolean mask for the elements that
# are smaller than or equal to the threshold value

# Use the mask to select elements for which the
# corresponding element in mask contans True

# Print the NumPy array
print(numbers)
```

Output

```[22 34 39]

```

Using numpy.where() function

In this solution, we will fetch the index position of all the elements which are greater 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 greater 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 greater 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 greater than the threshold value.

In the below example we are going to delete all the elements from the numpy array which are greater 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

```[22 34 39]

```

Summary

We learned two ways to remove elements from NumPy Array that are greater than a threshold value.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top