What does Imputer.fit with nan value do?

Viewed 751

I'm learning machine learning using the scikit-learn library and I was puzzled when I see the code from Scikit-learn tutorial

It has the following code:

import numpy as np
from sklearn.impute import SimpleImputer
imp = SimpleImputer(missing_values=np.nan, strategy='mean')
imp.fit([[1, 2], [np.nan, 3], [7, 6]])       

X = [[np.nan, 2], [6, np.nan], [7, 6]]
print(imp.transform(X)) 

From what I understand, imputer is used to fill in the missing values, and that it uses some strategy like mean, median or mode. But I don't understand what these 3 lines of code are doing

imp.fit([[1, 2], [np.nan, 3], [7, 6]])       
X = [[np.nan, 2], [6, np.nan], [7, 6]]
print(imp.transform(X)) 

Why does it fit a matrix with np.nan ? And how these fitting affect the imp.transform(X)?

1 Answers

When you run imp.fit, it calculates the value to replace in each column (in this case the mean).

You can access access what I keep in each column with: imp.statistics_

When you run imp.transform, it complete the NaN with the corresponding value (what is in imp.statistics_)

Related