【问题标题】:Python numpy Runtime warning using np.where np.errstate and warnings 'error'使用 np.where np.errstate 和警告“错误”的 Python numpy 运行时警告
【发布时间】:2021-01-19 16:59:13
【问题描述】:

我不断收到有关除法的运行时警告。 在下面的代码中,我使用了这个论坛的a question 的答案,甚至从this 导入了警告错误。

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:在 true_divide 中遇到无效值

如何正确定义函数以避免出现警告?

【问题讨论】:

  • 你用的是什么版本的python?另外,您能否提供一个通过函数alpha_n 发送的数据示例
  • 使用np.where 来避免错误值并不是一个好主意。他们仍在评估中。另一种方法是使用带有自己的where(和out)参数的np.divide ufunc。

标签: python numpy division


【解决方案1】:
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])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 2013-04-24
    • 1970-01-01
    相关资源
    最近更新 更多