0
votes

My Code:

### Working with NaN using sklearn
import numpy as np
from sklearn.preprocessing import Imputer
### Mean strategy
imp = Imputer(missing_values='NaN', strategy='mean', axis=1) 
imp.fit([1,5,9,np.NaN])
X = [1,5,9,np.NaN]
y = imp.transform(X)
print (y)

After running I am getting below warning message: C:\Users\Admin\Anaconda3\lib\site-packages\sklearn\utils\validation.py:386: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample. DeprecationWarning)

How to solve it? I tried the reshape but it is giving error message saying: 'list' object has no attribute 'reshape'

Please help.

1
you should create an array here imp.fit([1,5,9,np.NaN]) change to imp.fit(np.array([1,5,9,np.NaN])) and X = [1,5,9,np.NaN] should be X = np.array([1,5,9,np.NaN]) not sure if you still need to reshape but the type at least will be compatible with sklearn - EdChum
Stii getting below error: --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-1-b707e4784d60> in <module>() 3 from sklearn.preprocessing import Imputer 4 imp = Imputer(missing_values='NaN', strategy='mean', axis=1) ### Mean strategy ----> 5 imp.fit(np.array[1,5,9,np.NaN]) 6 #X.reshape(-1, 1) 7 X =np.array([1,5,9,np.NaN]) TypeError: 'builtin_function_or_method' object is not subscriptable - VaishaliLambe

1 Answers

0
votes

so i ran your code and changed X do a 2d list... Turns out that because you were passing a 1D array to transform so it was throwing you the error... So i made it a 2D lisst

import numpy as np
from sklearn.preprocessing import Imputer
### Mean strategy
imp = Imputer(missing_values='NaN', strategy='mean', axis=1)
imp.fit([1,5,9,np.NaN])
X = [[1,5,9,np.NaN]]           # < =========== The change that I made 
y = imp.transform(X)
print(y)
enter code here