Articles → Numpy → Conditionally Selecting The Elements In An Array In Numpy
Conditionally Selecting The Elements In An Array In Numpy
- An array is created using the arange function with values from 1 to 9
- Check the value of the array greater than 5 using a single expression arr > 5 and put the result of the expression in an array ‘result’
- The result will be a boolean array with true and false valuesClick to Enlarge
- The value from position 0 to 4 is false because the value at these positions is less than 5. The value from 5 to 8 is true because the value at these positions is greater than 5.
- Finally, print the value of arr[result], which returns the value from position 5 to 8 (all true values).Click to Enlarge