Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
831 views
in Technique[技术] by (71.8m points)

python - 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. I want to set all the nan values to zero using A[A==numpy.nan] = 0. It doesn't change the array at all. Why is that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You want np.isnan:

A[np.isnan(A)] = 0

The problem with your code is that (according to IEEE), nan doesn't equal anything -- even itself.

As a side note, there's also

  • np.isinf -> (+/- infinity)
  • np.isfinite -> (not infinity or NaN)
  • np.isposinf -> (+ infinity)
  • np.isneginf -> (- infinity)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...