Python Numpy nan_to_num help

I am having problems with the Numpy nan_to_num function: I have an array in which the last column contains fewer elements than other columns, and when I import it into Python, it puts "nan" to fill the array. This is just fine, as long as I don’t need to do any other things that deal with Nan.

I try to use "nan_to_num" without success. Most likely, this is a small thing that I am missing, but I can not understand.

Here are some simple inputs and outputs:

input:

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

Output

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

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

Thanks in advance.

+4
source share
1 answer

You have not changed the value of a . Try the following:

 a = numpy.array([[1, nan, 3]]) a = numpy.nan_to_num(a) print a 
+4
source

All Articles