Curriculum
Course: NumPy
Login

Curriculum

NumPy

Text lesson

Creating Filter Directly From Array

The previous example represents a common task in NumPy, which provides an efficient way to handle it.

We can directly use the array in place of the iterable variable in our condition, and it will function as expected.

Example

Generate a filter array that returns only the values greater than 42:

import numpy as np

arr = np.array([41424344])

filter_arr = arr 42

newarr = arr[filter_arr]

print(filter_arr)
print(newarr)

Example

Generate a filter array that returns only the even elements from the original array:

import numpy as np

arr = np.array([1234567])

filter_arr = arr 2 == 0

newarr = arr[filter_arr]

print(filter_arr)
print(newarr)