Simple imputer delete nan instead of imputation

Viewed 905

I'm trying to impute NaN values with SimpleImputer mean strategy but instead of imputing it, it deletes the NaN values, I read how to use it here and the documentation, it simply does not work with numpy array or python lists, what's wrong? and what is the solution?

import numpy as np
from sklearn.impute import SimpleImputer
from sklearn.model_selection import train_test_split

X = np.array([[2,3,6,5,4, np.nan],[2,3,6,15,4, np.nan]])

SI = SimpleImputer(strategy='mean')
X = SI.fit_transform(X)
print(X)

output

runfile('D:/python projects/untitled0.py', wdir='D:/python projects')
[[ 2.  3.  6.  5.  4.]
 [ 2.  3.  6. 15.  4.]]
2 Answers
In [239]: SI=SimpleImputer(verbose=1)                                                          
In [240]: SI.fit_transform(X)                                                                  
/usr/local/lib/python3.6/dist-packages/sklearn/impute/_base.py:403: UserWarning: Deleting features without observed values: [5]
  "observed values: %s" % missing)
Out[240]: 
array([[ 2.,  3.,  6.,  5.,  4.],
       [ 2.,  3.,  6., 15.,  4.]])

tweak X:

In [241]: X = np.array([[2,3,6,5,4, np.nan],[2,3,6,15,np.nan, 4]])                             
In [242]: SI.fit_transform(X)                                                                  
Out[242]: 
array([[ 2.,  3.,  6.,  5.,  4.,  4.],
       [ 2.,  3.,  6., 15.,  4.,  4.]])

All the values in the last column are Nan in your data. Hence, the imputer removes that column, because it does not know what value needs to be imputed. Please make sure you have at least one non- Nan value in your data for allowing imputer to work.

X = np.array([[2,3,6,5,4, np.nan],
              [2,3,6,15,4, np.nan], 
              [1,2,6,2,4, 1] ])

SI = SimpleImputer(strategy='mean')
SI.fit_transform(X)


# Output:
[[ 2.  3.  6.  5.  4.  1.]
 [ 2.  3.  6. 15.  4.  1.]
 [ 1.  2.  6.  2.  4.  1.]]
Related