I want to construct a np.array from another np.array using a conditional. For each value, if the condition is met, one operation has to be applied, otherwise another. The calculation I have written is ugly due to conversion to and back a list. Can it be improved in terms of speed, by not converting to a list?
THR = 1.0
THR_REZ = 1.0 / THR**2
def thresholded_function(x):
if x < THR:
return THR_REZ
else:
return 1.0 / x**2
rad2 = .....some_np_array.....
rez = np.array([threshold(r2) for r2 in rad2])