Back to questions
JuniorTheoryCommon
What does this program print?
import numpy as np
from sklearn.impute import SimpleImputer
X_train = np.array([[2.0], [np.nan], [4.0], [9.0]])
X_test = np.array([[np.nan], [100.0]])
imputer = SimpleImputer()
imputer.fit(X_train)
print(imputer.transform(X_test)[0, 0])5.0
- Expecting
100.0:transformfills a gap with the number thatfitfound onX_train; the known test value100.0is not used. - Expecting
4.0, the median: without astrategy,SimpleImputeruses the mean, and the mean of 2, 4 and 9 is5.0.
- What would the program print with
SimpleImputer(strategy="median"), and when is that the better choice? - Why is the test gap filled with a training statistic rather than one computed on the test set?