# How does imputation work? I'm struggling to understand it

I have a short question. I am implementing Scikit-Learn in Typescript and currently blocked at understanding & implementing imputer (mean and regression strategies).

Based on the example given on Scikit-Learn page, http://scikit-learn.org/stable/modules/preprocessing.html#imputation

>>> import numpy as np
>>> from sklearn.preprocessing import Imputer
>>> imp = Imputer(missing_values='NaN', strategy='mean', axis=0)
>>> imp.fit([[1, 2], [np.nan, 3], [7, 6]])
Imputer(axis=0, copy=True, missing_values='NaN', strategy='mean', verbose=0)
>>> X = [[np.nan, 2], [6, np.nan], [7, 6]]
>>> print(imp.transform(X))
[[ 4.          2.        ]
[ 6.          3.666...]
[ 7.          6.        ]]


How does imputation against np.nan in [[np.nan, 2], [6, np.nan], [7, 6]] work?

• Could you please explain it to me how it works? I would appreciate any equations and background knowledge that I need to understand this.
• If you are asking about Scikit then your question is off topic here. If you are asking something more general about imputation, then please edit your question to reflect that. May 3, 2018 at 14:26
• Is it fine to use Scikit Learn code as a reference? May 4, 2018 at 10:10
• -reference- -> example* May 4, 2018 at 10:28
• You can use code as a reference, but if you are trying to debug code, this is the wrong forum. May 4, 2018 at 10:45
• Question: Don't you think it still belongs to this place because it's not really asking about implementation detail but asking about how it works? May 7, 2018 at 5:18

## 1 Answer

The Imputer is just calculating the mean for each column when fit is called. So column 1 has mean (1+7)/2 = 4 and column 2 has mean (2+3+6)/3 = 3.666....

The transform function just fills in the NaN fields with the column's mean value.

• How about regression strategy? Could you explain that to me as well? Thanks in advance again May 2, 2018 at 23:58
• There is no regression strategy in sklearn, but you could do this yourself by training a model using the other columns as the dataset and the column to be imputed as the target, nearest neighbor is a simple and fast way to do this (use the value of your closest neighbor(s) for each missing field). May 3, 2018 at 0:10