Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

place random numbers for nan values numpy array

I have a numpy array X that has nan values in it.

X = np.array([[  1.,   2.,   3.],
              [  4.,  nan,  54.],
              [ 90.,  32.,  nan],
              [ 55.,  42.,  86.]])

I'd like to replace all nan values with a different random number. I can generate the random number easily with np.random.randn(). I can use a mask to locate and count the nans.

mx = ma.masked_array(X,mask=np.isnan(X)) //locate nans
mx.mask.sum()      // count nans so I know how many random values to generate

My issue is I don't know how to input them in quickly and efficiently. The example I gave above is a very small dataset, but I have one that is much much larger. Therefore efficiency is key.

If I try

X[mx.mask] = np.random.randn() //or 
X[mx.mask]=np.random.randn(mx.mask.sum())

I replace each nan with the same random number which is not what I want or I get a broadcast error in the second example.

Any suggestions?

like image 371
Terence Chow Avatar asked Aug 15 '26 02:08

Terence Chow


1 Answers

X[np.isnan(X)] = np.random.randn(len(X[np.isnan(X)]))

The above works perfectly for me. Numpy version 1.8.0.

like image 140
U2EF1 Avatar answered Aug 16 '26 17:08

U2EF1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!