# Machine Learning: Missing Data in Dataset and Imputer [duplicate]

I am a newbie in ML, and I am learning how to fill missing data in a dataset using Imputer. These are the few lines of code that I came across

from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)
imputer = imputer.fit(X[:, 1:3])
X[:, 1:3] = imputer.transform(X[:, 1:3])


Now I am not able to understand what is the role of the fit and the transform function. It will be great if someone can help. Thank you.

• I have seen it already but it was not clear to me, so I asked again – Turing101 Apr 16 '20 at 21:07

## 2 Answers

fit is learning how make the transformation on array X(not fill missing values, only have knowledge how do it). All info is saved inside of Imputer.

If we want to use info from fit and transform data in the way which is saved in clf we use command transform.

• But why do we need to fit the data? we are only doing mean or median right? and that can be done by simple mathematics rather than "training" the data to fit. – Turing101 Apr 17 '20 at 5:57
• We fit to the data to get a more generic way to data imputation. We use not only mean or median(scikit-learn.org/stable/modules/impute.html). – fuwiak Apr 17 '20 at 6:57

In a very simple words, Imputer(), is the definition of how you want to fill in the missing values. For example, you define what will be the strategy, what will be the axis. This line doesn't do anything because you have just created an object.

Finally, you fit this imputer object for letting it learn your dataset and accordingly it will transform your missing values when you call transform method.

• But why do we need to fit the data? we are only doing mean or median right? and that can be done by simple mathematics rather than "training" the data to fit. – Turing101 Apr 17 '20 at 5:57