You can select elements in an array based on conditions. Let's begin with 2 arrays.
np.where lets you select elements from an array based on conditions. It returns the index of the elements that meets the conditions.
In this example, using the 2 arrays from above, we can use
np.where to find which:
xthat are bigger than or equal to its corresponding element in
xmultiplied by 2 that are smaller than its corresponding element in
ythat are bigger than 5
Conditional operators like
> can be used to compare arrays. Using them will return Boolean values.
In addition, you can use this syntax to return the index of elements that meets the condition:
y [y > 3]
Similarly, you can also use
Use these arrays for the following practice:
myArray_1 = np.array([2,2,3,4]) myArray_2 = np.array([3,2,3,4])
1. Find the elements that are the same in both arrays
2. Find elements in
myArray_1 that are smaller than or equal to its corresponding element in
3. Find the elements in
myArray_1 that is bigger than 2