Search⌘ K
AI Features

Solution Review: Using Conditions on Arrays

Explore how to filter elements in NumPy arrays by applying specific conditions using logical operators like and and or. This lesson helps you understand how to select array values based on boolean criteria, enhancing your data processing skills for scientific computing tasks.

We'll cover the following...

Solution #

C++
import numpy as np
x = np.linspace(0, 2 * np.pi, 20)
y = np.sin(x)
arr1 = y[(y > 0.7) | (y < -0.5)]
arr2 = y[(y > -0.5) & (y < 0.7)]
print(arr1)
print('-----')
print(arr2)

Explanation

  • In line 3, we have created an equally spaced array with 20 values ranging from 00 to 2π2\ ...