2013-07-09 17:12:17 +08:00
|
|
|
"""
|
|
|
|
|
======================================================
|
|
|
|
|
Imputing missing values before building an estimator
|
|
|
|
|
======================================================
|
|
|
|
|
|
2017-05-12 05:42:57 +08:00
|
|
|
This example shows that imputing the missing values can give better
|
|
|
|
|
results than discarding the samples containing any missing value.
|
|
|
|
|
Imputing does not always improve the predictions, so please check via
|
|
|
|
|
cross-validation. Sometimes dropping rows or using marker values is
|
|
|
|
|
more effective.
|
2013-07-09 17:12:17 +08:00
|
|
|
|
2013-07-28 15:03:56 +08:00
|
|
|
Missing values can be replaced by the mean, the median or the most frequent
|
|
|
|
|
value using the ``strategy`` hyper-parameter.
|
2014-11-30 22:31:22 +08:00
|
|
|
The median is a more robust estimator for data with high magnitude variables
|
|
|
|
|
which could dominate results (otherwise known as a 'long tail').
|
2013-07-09 17:12:17 +08:00
|
|
|
|
2014-11-30 22:31:22 +08:00
|
|
|
Script output::
|
2013-07-09 17:12:17 +08:00
|
|
|
|
2016-08-30 19:28:01 +08:00
|
|
|
Score with the entire dataset = 0.56
|
2013-07-26 06:36:18 +08:00
|
|
|
Score without the samples containing missing values = 0.48
|
|
|
|
|
Score after imputation of the missing values = 0.55
|
2013-07-09 17:12:17 +08:00
|
|
|
|
2014-11-30 22:31:22 +08:00
|
|
|
In this case, imputing helps the classifier get close to the original score.
|
2017-05-12 05:42:57 +08:00
|
|
|
|
2013-07-09 17:12:17 +08:00
|
|
|
"""
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
from sklearn.datasets import load_boston
|
|
|
|
|
from sklearn.ensemble import RandomForestRegressor
|
|
|
|
|
from sklearn.pipeline import Pipeline
|
2018-02-15 03:47:06 +08:00
|
|
|
from sklearn.impute import SimpleImputer
|
2015-09-11 02:26:39 +08:00
|
|
|
from sklearn.model_selection import cross_val_score
|
2013-07-09 17:12:17 +08:00
|
|
|
|
|
|
|
|
rng = np.random.RandomState(0)
|
|
|
|
|
|
|
|
|
|
dataset = load_boston()
|
|
|
|
|
X_full, y_full = dataset.data, dataset.target
|
|
|
|
|
n_samples = X_full.shape[0]
|
|
|
|
|
n_features = X_full.shape[1]
|
|
|
|
|
|
|
|
|
|
# Estimate the score on the entire dataset, with no missing values
|
|
|
|
|
estimator = RandomForestRegressor(random_state=0, n_estimators=100)
|
|
|
|
|
score = cross_val_score(estimator, X_full, y_full).mean()
|
2016-08-30 19:28:01 +08:00
|
|
|
print("Score with the entire dataset = %.2f" % score)
|
2013-07-09 17:12:17 +08:00
|
|
|
|
2013-07-26 06:36:18 +08:00
|
|
|
# Add missing values in 75% of the lines
|
|
|
|
|
missing_rate = 0.75
|
2017-05-12 05:42:57 +08:00
|
|
|
n_missing_samples = int(np.floor(n_samples * missing_rate))
|
2013-07-09 17:12:17 +08:00
|
|
|
missing_samples = np.hstack((np.zeros(n_samples - n_missing_samples,
|
|
|
|
|
dtype=np.bool),
|
|
|
|
|
np.ones(n_missing_samples,
|
|
|
|
|
dtype=np.bool)))
|
|
|
|
|
rng.shuffle(missing_samples)
|
|
|
|
|
missing_features = rng.randint(0, n_features, n_missing_samples)
|
|
|
|
|
|
|
|
|
|
# Estimate the score without the lines containing missing values
|
|
|
|
|
X_filtered = X_full[~missing_samples, :]
|
2014-08-29 20:26:15 +08:00
|
|
|
y_filtered = y_full[~missing_samples]
|
2013-07-09 17:12:17 +08:00
|
|
|
estimator = RandomForestRegressor(random_state=0, n_estimators=100)
|
|
|
|
|
score = cross_val_score(estimator, X_filtered, y_filtered).mean()
|
2013-07-25 19:00:46 +08:00
|
|
|
print("Score without the samples containing missing values = %.2f" % score)
|
2013-07-09 17:12:17 +08:00
|
|
|
|
|
|
|
|
# Estimate the score after imputation of the missing values
|
|
|
|
|
X_missing = X_full.copy()
|
|
|
|
|
X_missing[np.where(missing_samples)[0], missing_features] = 0
|
|
|
|
|
y_missing = y_full.copy()
|
2018-02-15 03:47:06 +08:00
|
|
|
estimator = Pipeline([("imputer", SimpleImputer(missing_values=0,
|
2018-03-30 13:40:19 +08:00
|
|
|
strategy="mean")),
|
2013-07-09 17:12:17 +08:00
|
|
|
("forest", RandomForestRegressor(random_state=0,
|
|
|
|
|
n_estimators=100))])
|
|
|
|
|
score = cross_val_score(estimator, X_missing, y_missing).mean()
|
2013-07-25 19:00:46 +08:00
|
|
|
print("Score after imputation of the missing values = %.2f" % score)
|