Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing columns which has only "nan" values from a NumPy array

I have a NumPy matrix like the one below:

[[182 93 107 ..., nan nan -1]
 [182 93 107 ..., nan nan -1]
 [182 93 110 ..., nan nan -1]
 ..., 
 [188 95 112 ..., nan nan -1]
 [188 97 115 ..., nan nan -1]
 [188 95 112 ..., nan nan -1]]

I want to remove the columns which only involve nan values from the above matrix.

How can I do this? Thanks.

like image 936
yusuf Avatar asked Feb 02 '16 13:02

yusuf


People also ask

How can I remove columns in NumPy array that contains non numeric values?

Many times we have non-numeric values in NumPy array. These values need to be removed, so that array will be free from all these unnecessary values and look more decent. It is possible to remove all columns containing Nan values using the Bitwise NOT operator and np. isnan() function.

How do I delete NaN columns?

dropna() is used to drop/remove columns with NaN / None values.

How do I remove all NaN values from an array?

How to drop all missing values from a numpy array? Droping the missing values or nan values can be done by using the function "numpy. isnan()" it will give us the indexes which are having nan values and when combined with other function which is "numpy. logical_not()" where the boolean values will be reversed.


1 Answers

Assuming your array is of floats now, you can identify all the columns which are NaN and use fancy indexing to retrieve the others:

d
array([[ 182.,   93.,  107.,   nan,   nan,   -1.],
       [ 182.,   93.,  107.,    4.,   nan,   -1.],
       [ 182.,   93.,  110.,   nan,   nan,   -1.],
       [ 188.,   95.,  112.,   nan,   nan,   -1.],
       [ 188.,   97.,  115.,   nan,   nan,   -1.],
       [ 188.,   95.,  112.,   nan,   nan,   -1.]])


d[:,~np.all(np.isnan(d), axis=0)]

array([[ 182.,   93.,  107.,   nan,   -1.],
       [ 182.,   93.,  107.,    4.,   -1.],
       [ 182.,   93.,  110.,   nan,   -1.],
       [ 188.,   95.,  112.,   nan,   -1.],
       [ 188.,   97.,  115.,   nan,   -1.],
       [ 188.,   95.,  112.,   nan,   -1.]])
like image 151
xnx Avatar answered Oct 11 '22 00:10

xnx