scikit-learn/examples/missing_values.py

67 lines
2.6 KiB
Python
Raw Normal View History

2013-07-09 17:12:17 +08:00
"""
======================================================
Imputing missing values before building an estimator
======================================================
This example shows that imputing the missing values can give better results
than discarding the samples containing any missing value.
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.
2013-07-09 17:12:17 +08:00
Script output:
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
"""
import numpy as np
from sklearn.datasets import load_boston
from sklearn.ensemble import RandomForestRegressor
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import Imputer
from sklearn.cross_validation import cross_val_score
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()
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
2013-07-09 17:12:17 +08:00
n_missing_samples = np.floor(n_samples * missing_rate)
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()
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()
2013-07-26 06:36:18 +08:00
estimator = Pipeline([("imputer", Imputer(missing_values=0,
2013-07-09 17:12:17 +08:00
strategy="mean",
axis=0)),
("forest", RandomForestRegressor(random_state=0,
n_estimators=100))])
score = cross_val_score(estimator, X_missing, y_missing).mean()
print("Score after imputation of the missing values = %.2f" % score)