Python 使用 np.where np.errstate 和警告 'error' 的 numpy 运行时警告
Python numpy Runtime warning using np.where np.errstate and warnings 'error'
我不断收到关于除法的运行时警告。
在下面的代码中,我使用了 from this forum and even imported warnings error from .
的答案
def alpha_n(V):
with np.errstate(divide='ignore'):
alph = np.where(V!= -55, 0.01*(V+55)/(1-np.exp(-0.1*(V+55))), 0.1)
return alph
RuntimeWarning: invalid value encountered in true_divide
如何正确定义函数以避免警告?
In [26]: def alpha_n(V):
...: with np.errstate(invalid='ignore'):
...: alph = np.where(V!= -55, 0.01*(V+55)/(1-np.exp(-0.1*(V+55))), 0
...: .1)
...: return alph
...:
In [27]: alpha_n(np.array([1,2,3,-55]))
Out[27]: array([0.56207849, 0.5719136 , 0.58176131, 0.1 ])
除以 0 不同于无效值:
In [28]: 1/(1-np.exp(-0.1*(np.array([-55])+55)))
<ipython-input-28-ed83c58d75bb>:1: RuntimeWarning: divide by zero encountered in true_divide
1/(1-np.exp(-0.1*(np.array([-55])+55)))
Out[28]: array([inf])
In [29]: 0/(1-np.exp(-0.1*(np.array([-55])+55)))
<ipython-input-29-0d642b423038>:1: RuntimeWarning: invalid value encountered in true_divide
0/(1-np.exp(-0.1*(np.array([-55])+55)))
Out[29]: array([nan])
我不断收到关于除法的运行时警告。
在下面的代码中,我使用了
def alpha_n(V):
with np.errstate(divide='ignore'):
alph = np.where(V!= -55, 0.01*(V+55)/(1-np.exp(-0.1*(V+55))), 0.1)
return alph
RuntimeWarning: invalid value encountered in true_divide
如何正确定义函数以避免警告?
In [26]: def alpha_n(V):
...: with np.errstate(invalid='ignore'):
...: alph = np.where(V!= -55, 0.01*(V+55)/(1-np.exp(-0.1*(V+55))), 0
...: .1)
...: return alph
...:
In [27]: alpha_n(np.array([1,2,3,-55]))
Out[27]: array([0.56207849, 0.5719136 , 0.58176131, 0.1 ])
除以 0 不同于无效值:
In [28]: 1/(1-np.exp(-0.1*(np.array([-55])+55)))
<ipython-input-28-ed83c58d75bb>:1: RuntimeWarning: divide by zero encountered in true_divide
1/(1-np.exp(-0.1*(np.array([-55])+55)))
Out[28]: array([inf])
In [29]: 0/(1-np.exp(-0.1*(np.array([-55])+55)))
<ipython-input-29-0d642b423038>:1: RuntimeWarning: invalid value encountered in true_divide
0/(1-np.exp(-0.1*(np.array([-55])+55)))
Out[29]: array([nan])