python
data_error
ai_generated
true
RuntimeWarning:在 sqrt 中遇到无效值
RuntimeWarning: invalid value encountered in sqrt
ID: python/numpy-invalid-value-encountered
80%修复率
88%置信度
0证据数
2024-11-11首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
对负数应用 np.sqrt,产生 NaN。
English
np.sqrt applied to negative numbers, producing NaN.
解决方案
-
95% 成功率
np.sqrt(np.clip(arr, 0, None))
-
90% 成功率
np.where(arr >= 0, np.sqrt(arr), np.nan)
无效尝试
常见但无效的做法:
-
80% 失败
Hides NaN; downstream results wrong
-
70% 失败
Changes math silently; loses sign info