开发者

Python Numpy nan_to_num help

开发者 https://www.devze.com 2023-01-25 16:40 出处:网络
I\'m having some trouble with Numpy\'s nan_to_num function: I\'ve got an array where the last column contains fewer elements than the other columns, and when I import it into Python, it places \'nan\'

I'm having some trouble with Numpy's nan_to_num function: I've got an array where the last column contains fewer elements than the other columns, and when I import it into Python, it places 'nan's to fill out the array. That's just fine until I need to do some other things that get tripped-up by the 'nan's.

I'm trying to use 'nan_to_num' with no success. It's likely a small thing I'm missing, but I can't figure it out.

Here's some simple input and output:

input:

a = numpy.array([[1, nan, 3]])
print a
numpy.n开发者_开发知识库an_to_num(a)
print a

output

[[  1.  nan   3.]]
[[  1.  nan   3.]]

The second 'nan' should be a zero... http://docs.scipy.org/doc/numpy/reference/generated/numpy.nan_to_num.html

Thanks in advance.


You haven't changed the value of a. Try this:

a = numpy.array([[1, nan, 3]])
a = numpy.nan_to_num(a)
print a
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号