pseudocode for np.where
:
def np_where(chooser, true_opt, false_opt):
out = np.empty(chooser.shape, dtype = true_opt.dtype)
out[~chooser] = false_opt
return out
Importantly, true_opt
is generated before calling the function. So if anything in it raises an error, the interpreter never gets to call np.where
- even if np.where
would never use the parts of true_opt
that raise the error.
You can get rid of the divide by zero
errors, but don't use np.seterr
as recommended in the other answer - that will shut it off for the whole session and may cause problems with other bits of code. You can do it like this:
with np.errstate(divide='ignore'):
mo = np.where(p_arr > 0.5, -6.93/(p_arr - 0.5), 10)
To find out where your error was coming from, just use:
np.where(p_arr == 0.5)
Which should give you the coordinates where you were getting the divide by zero
error
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…