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:
x
that are bigger than or equal to its corresponding element in y
x
multiplied by 2 that are smaller than its corresponding element in y
y
that 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 np.where
:
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 myArray_2
3. Find the elements in myArray_1
that is bigger than 2
You are expected to comply with University policies and guidelines namely, Appropriate Use of Information Resources Policy, IT Usage Policy and Social Media Policy. Users will be personally liable for any infringement of Copyright and Licensing laws. Unless otherwise stated, all guide content is licensed by CC BY-NC 4.0.