1
$\begingroup$

The only argument inside the fit function of SimpleImputer is: 'Age'. Yet the returned output worked on the 'Salary' column as well. That is what I am unable to understand.

Here is my code (considering all the necessary libraries imported):

from sklearn.impute import SimpleImputer
imputer = SimpleImputer(missing_values = np.nan)
imputer = imputer.fit(df[['Age']])    
X[:, 1:3] = imputer.fit_transform(X[:, 1:3])
print(X)

Dataset:

   Country   Age   Salary Purchased
0   France  44.0  72000.0        No
1    Spain  27.0  48000.0       Yes
2  Germany  30.0  54000.0        No
3    Spain  38.0  61000.0        No
4  Germany  40.0      NaN       Yes
5   France  35.0  58000.0       Yes
6    Spain   NaN  52000.0        No
7   France  48.0  79000.0       Yes
8  Germany  50.0  83000.0        No
9   France  37.0  67000.0       Yes

Output:

[['France' 44.0 72000.0]
 ['Spain' 27.0 48000.0]
 ['Germany' 30.0 54000.0]
 ['Spain' 38.0 61000.0]
 ['Germany' 40.0 63777.77777777778]
 ['France' 35.0 58000.0]
 ['Spain' 38.77777777777778 52000.0]
 ['France' 48.0 79000.0]
 ['Germany' 50.0 83000.0]
 ['France' 37.0 67000.0]]
$\endgroup$
0
$\begingroup$
imputer = imputer.fit(df[['Age']])    
X[:, 1:3] = imputer.fit_transform(X[:, 1:3])

You are again applying "fit" with fit_trasnform. Try with "transform" only

You will get a value error

ValueError: X has 3 features per sample, expected 2

$\endgroup$
5
  • $\begingroup$ So it is because of the X[:, 1:3] that the missing value for Salary was found and applied? $\endgroup$ – IcudBA-Hak-Cur Jul 13 '20 at 15:24
  • 1
    $\begingroup$ Your first fit is over written by the fit in the fit_transform. $\endgroup$ – 10xAI Jul 13 '20 at 16:09
  • $\begingroup$ Please mark the question as "Accepted" if you found so $\endgroup$ – 10xAI Jul 14 '20 at 1:08
  • $\begingroup$ Okay. I have one last thing to ask. imputer = imputer.fit(df[['Salary']]) I put in the above line as well, so it makes sense to me, though it hardly makes any difference. But I don't know what the correct way of getting the final result is. What could have been done differently? $\endgroup$ – IcudBA-Hak-Cur Jul 14 '20 at 2:31
  • $\begingroup$ Keep the columns same, with every function be it fit, transform, or fit transform. X[ :, 1:3] line is doing what you are trying. Line above it is not needed $\endgroup$ – 10xAI Jul 14 '20 at 3:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.