How to Create a Filter Array That Will Return Only Even Elements


In this example, we will learn how to create a filter array that will return only even elements from the original array in Python NumPy.

Source Code

import numpy as np

n = np.array([5, 6, 7, 8, 9, 10, 11, 12])

filter_boolean = n % 2 == 0

filter_n = n[filter_boolean]

print(filter_boolean)
print(filter_n)

Output:

[False  True False  True False  True False  True]
[ 6  8 10 12]
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement