Reputation: 391
I have the following reproducible script:
import numpy as np
c0, c1 = 0.000032, 3
S_test = np.ones((3,3), dtype=float)*300000
S_test[1,1] = 2000
np.where(S_test > 2000, 1, 1 - 1 / np.exp((c0 * S_test) ** c1) )
RuntimeWarning: overflow encountered in exp
array([[1.00000000e+00, 1.00000000e+00, 1.00000000e+00],
[1.00000000e+00, 2.62109643e-04, 1.00000000e+00],
[1.00000000e+00, 1.00000000e+00, 1.00000000e+00]])
I am trying to figure out 1) Why I am getting this overflow error and 2) how can it be dealt with in a way that doesn't just ignore it because i'm not entirely certain what the consequences of ignoring it are in the first place.
The np.where()
should first recognize where S_test > 2000
and then return 1 for those values, alternatively, where this condition does not hold np.where()
should return 1 - 1 / np.exp((c0 * S_test) ** c1)
I have tried to separately compute 1 - 1 / np.exp((c0 * 2000) ** c1)
and this evaluates just fine and as I expected.
Any insight would be great.
Upvotes: 1
Views: 242
Reputation: 3254
You get the error because all arguments of np.where
are fully evaluated. You can do this:
S_test_capped = np.where(S_test > 2000, 2000, S_test)
np.where(S_test > 2000, 1, 1 - 1 / np.exp((c0 * S_test_capped) ** c1) )
or just silence the warnings:
old_settings = np.seterr(over='ignore')
np.where(S_test > 2000, 1, 1 - 1 / np.exp((c0 * S_test) ** c1) )
np.seterr(**old_settings) # restore warning settings
Upvotes: 0
Reputation: 26050
np.where evaluates both operands.
If you really want to avoid the warning, you can do e.g. something like this: https://github.com/scipy/scipy/blob/master/scipy/_lib/_util.py#L31
Upvotes: 4