简体   繁体   English

将数组的值从nan更改为零

[英]change the values of an array from nan to zero

I have an array A on python that has some nan values created by numpy.nan. 我在python上有一个数组A,它有一些由numpy.nan创建的nan值。 I want to set all the nan values to zero using A[A==numpy.nan] = 0 . 我想使用A[A==numpy.nan] = 0将所有nan值设置为零。 It doesn't change the array at all. 它根本不会改变阵列。 Why is that? 这是为什么?

You want np.isnan : 你想要np.isnan

A[np.isnan(A)] = 0

The problem with your code is that (according to IEEE), nan doesn't equal anything -- even itself. 您的代码的问题在于(根据IEEE), nan并不等于任何东西 - 甚至本身。

As a side note, there's also 作为旁注,还有

  • np.isinf -> (+/- infinity) np.isinf - >(+/-无穷大)
  • np.isfinite -> (not infinity or NaN) np.isfinite - >(不是无穷大或NaN)
  • np.isposinf -> (+ infinity) np.isposinf - >(+ infinity)
  • np.isneginf -> (- infinity) np.isneginf - >( - 无穷大)

您可以使用numpy函数将矩阵x处的所有nan值更改为零。

numpy.nan_to_num(x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM