简体   繁体   中英

Fill an array with nan in numpy

I have a numpy vector my_indexes of size 1XN which contain boolean values of indexing and a 2D array my_array of size MxK where K << N. Actually, the boolean vector correspond to columns that I remove (or keep in the array my_array) and I want to add those columns back filled with zeros (or 'NaNs'). My code for removing the columns:

 my_array= np.array(my_array[:, ~np.all(np.isnan(my_array), axis=0)])
 cols = np.all(np.isnan(my_array), axis=0)
 my_array = some_process(my_array)
 # How can I add the removed columns

My array if of size MXN and then the size is MXK . How can I fill the removed columns again with nan or zeros?

An example could be:

0.1 nan 0.3 .04 nan 0.12 0.12
0.1 nan 0.3 .04 nan 0.12 0.12
0.1 nan 0.3 .04 nan 0.12 0.12

Firstly, I want to remove the nan columns using my_array= np.array(my_array[:, ~np.all(np.isnan(my_array), axis=0)]) .

0.1 0.3 .04 0.12 0.12
0.1 0.3 .04 0.12 0.12
0.1 0.3 .04 0.12 0.12

And the my_indexes vector is:

False True False False True False False

Then I want to process the matrix and then have the nan columns back (note that the preprocessing cannot happened with the nan columns). I guess that I need to use the np.insert function however how can I do so using my boolean vector

You can probably use masked arrays for that:

import numpy as np
import numpy.ma as ma

def some_process(x):
    return x**2

x = np.arange(9, dtype=float).reshape(3, 3)
x[:,1] = np.nan
print(x)
# [[  0.  nan   2.]
#  [  3.  nan   5.]
#  [  6.  nan   8.]]

# mask all np.nan and np.inf
masked_x = ma.masked_invalid(x)
# Compute the process only on the unmasked values and fill back np.nan
x = ma.filled(some_process(masked_x), np.nan)
print(x)
# [[  0.  nan   4.]
#  [  9.  nan  25.]
#  [ 36.  nan  64.]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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