【问题标题】:Input other than negative numbers that may produce warning "invalid value" with numpy.sqrt?使用 numpy.sqrt 可能会产生警告“无效值”的负数以外的输入?
【发布时间】:2018-11-13 21:13:38
【问题描述】:

我有一个np.sqrt()sn-p,它没有检查负值,所以我收到了警告,这是可以理解的。我以为我已经修复了它,但我又得到了一个:

RuntimeWarning: invalid value encountered in sqrt
  data_std = np.sqrt(np.absolute(data_std))

np.sqrt 是否有除负数以外的无效数字?

【问题讨论】:

标签: python numpy absolute


【解决方案1】:

没有为负实数定义平方根,numpy 将为“实”dtype intfloat-np.inf(和 nan 本身)的负输入生成 nan

然而,平方根是为所有复数数定义的:

dtype example x np.sqrt(x) RuntimeWarning
Positive float 1. 1.
Positive int 1 1
Positive complex 1+0J 1
Negative float -1. nan ⚠️
Negative int -1 nan ⚠️
Negative complex -1+0j 1j
np.inf np.inf
np.inf nan ⚠️
np.nan nan

【讨论】:

    猜你喜欢
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    相关资源
    最近更新 更多