77.3k views
1 vote
Write a function which: is named not_in_between takes three parameters: a NumPy array, a lower threshold (float), and an upper threshold (float) returns a NumPy array You should use a boolean mask to return only the values in the NumPy array that are NOT in between the two specified threshold values, lower and upper. No loops are allowed, or built-in functions, or NumPy functions. For example, not_in_between([1, 2, 3, 4], 1, 3) should return a NumPy array of [4].

1 Answer

1 vote

This function uses boolean indexing to create a mask based on the conditions provided and then applies the mask to the input array to filter out the values not in between the specified thresholds.

import numpy as np

def not_in_between(array, lower, upper):

# Create a boolean mask for values not in between lower and upper thresholds

mask = (array <= lower) | (array >= upper)

# Use the boolean mask to filter the values

result = np.array(array)[mask]

return result

# Example usage:

input_array = np.array([1, 2, 3, 4])

lower_threshold = 1

upper_threshold = 3

result_array = not_in_between(input_array, lower_threshold, upper_threshold)

print(result_array)

User Dmitri Goldring
by
7.6k points