|
| 1 | +""" |
| 2 | +========================================================= |
| 3 | +Imputing missing values with variants of IterativeImputer |
| 4 | +========================================================= |
| 5 | +
|
| 6 | +The :class:`sklearn.impute.IterativeImputer` class is very flexible - it can be |
| 7 | +used with a variety of estimators to do round-robin regression, treating every |
| 8 | +variable as an output in turn. |
| 9 | +
|
| 10 | +In this example we compare some estimators for the purpose of missing feature |
| 11 | +imputation with :class:`sklearn.imputeIterativeImputer`:: |
| 12 | +
|
| 13 | + :class:`~sklearn.linear_model.BayesianRidge`: regularized linear regression |
| 14 | + :class:`~sklearn.tree.DecisionTreeRegressor`: non-linear regression |
| 15 | + :class:`~sklearn.ensemble.ExtraTreesRegressor`: similar to missForest in R |
| 16 | + :class:`~sklearn.neighbors.KNeighborsRegressor`: comparable to other KNN |
| 17 | + imputation approaches |
| 18 | +
|
| 19 | +Of particular interest is the ability of |
| 20 | +:class:`sklearn.impute.IterativeImputer` to mimic the behavior of missForest, a |
| 21 | +popular imputation package for R. In this example, we have chosen to use |
| 22 | +:class:`sklearn.ensemble.ExtraTreesRegressor` instead of |
| 23 | +:class:`sklearn.ensemble.RandomForestRegressor` (as in missForest) due to its |
| 24 | +increased speed. |
| 25 | +
|
| 26 | +Note that :class:`sklearn.neighbors.KNeighborsRegressor` is different from KNN |
| 27 | +imputation, which learns from samples with missing values by using a distance |
| 28 | +metric that accounts for missing values, rather than imputing them. |
| 29 | +
|
| 30 | +The goal is to compare different estimators to see which one is best for the |
| 31 | +:class:`sklearn.impute.IterativeImputer` when using a |
| 32 | +:class:`sklearn.linear_model.BayesianRidge` estimator on the California housing |
| 33 | +dataset with a single value randomly removed from each row. |
| 34 | +
|
| 35 | +For this particular pattern of missing values we see that |
| 36 | +:class:`sklearn.ensemble.ExtraTreesRegressor` and |
| 37 | +:class:`sklearn.linear_model.BayesianRidge` give the best results. |
| 38 | +""" |
| 39 | +print(__doc__) |
| 40 | + |
| 41 | +import numpy as np |
| 42 | +import matplotlib.pyplot as plt |
| 43 | +import pandas as pd |
| 44 | + |
| 45 | +from sklearn.datasets import fetch_california_housing |
| 46 | +from sklearn.impute import SimpleImputer |
| 47 | +from sklearn.impute import IterativeImputer |
| 48 | +from sklearn.linear_model import BayesianRidge |
| 49 | +from sklearn.tree import DecisionTreeRegressor |
| 50 | +from sklearn.ensemble import ExtraTreesRegressor |
| 51 | +from sklearn.neighbors import KNeighborsRegressor |
| 52 | +from sklearn.pipeline import make_pipeline |
| 53 | +from sklearn.model_selection import cross_val_score |
| 54 | + |
| 55 | +N_SPLITS = 5 |
| 56 | + |
| 57 | +rng = np.random.RandomState(0) |
| 58 | + |
| 59 | +X_full, y_full = fetch_california_housing(return_X_y=True) |
| 60 | +n_samples, n_features = X_full.shape |
| 61 | + |
| 62 | +# Estimate the score on the entire dataset, with no missing values |
| 63 | +br_estimator = BayesianRidge() |
| 64 | +score_full_data = pd.DataFrame( |
| 65 | + cross_val_score( |
| 66 | + br_estimator, X_full, y_full, scoring='neg_mean_squared_error', |
| 67 | + cv=N_SPLITS |
| 68 | + ), |
| 69 | + columns=['Full Data'] |
| 70 | +) |
| 71 | + |
| 72 | +# Add a single missing value to each row |
| 73 | +X_missing = X_full.copy() |
| 74 | +y_missing = y_full |
| 75 | +missing_samples = np.arange(n_samples) |
| 76 | +missing_features = rng.choice(n_features, n_samples, replace=True) |
| 77 | +X_missing[missing_samples, missing_features] = np.nan |
| 78 | + |
| 79 | +# Estimate the score after imputation (mean and median strategies) |
| 80 | +score_simple_imputer = pd.DataFrame() |
| 81 | +for strategy in ('mean', 'median'): |
| 82 | + estimator = make_pipeline( |
| 83 | + SimpleImputer(missing_values=np.nan, strategy=strategy), |
| 84 | + br_estimator |
| 85 | + ) |
| 86 | + score_simple_imputer[strategy] = cross_val_score( |
| 87 | + estimator, X_missing, y_missing, scoring='neg_mean_squared_error', |
| 88 | + cv=N_SPLITS |
| 89 | + ) |
| 90 | + |
| 91 | +# Estimate the score after iterative imputation of the missing values |
| 92 | +# with different estimators |
| 93 | +estimators = [ |
| 94 | + BayesianRidge(), |
| 95 | + DecisionTreeRegressor(max_features='sqrt', random_state=0), |
| 96 | + ExtraTreesRegressor(n_estimators=10, n_jobs=-1, random_state=0), |
| 97 | + KNeighborsRegressor(n_neighbors=15) |
| 98 | +] |
| 99 | +score_iterative_imputer = pd.DataFrame() |
| 100 | +for estimator in estimators: |
| 101 | + estimator = make_pipeline( |
| 102 | + IterativeImputer(random_state=0, estimator=estimator), |
| 103 | + br_estimator |
| 104 | + ) |
| 105 | + score_iterative_imputer[estimator.__class__.__name__] = \ |
| 106 | + cross_val_score( |
| 107 | + estimator, X_missing, y_missing, scoring='neg_mean_squared_error', |
| 108 | + cv=N_SPLITS |
| 109 | + ) |
| 110 | + |
| 111 | +scores = pd.concat( |
| 112 | + [score_full_data, score_simple_imputer, score_iterative_imputer], |
| 113 | + keys=['Original', 'SimpleImputer', 'IterativeImputer'], axis=1 |
| 114 | +) |
| 115 | + |
| 116 | +# plot boston results |
| 117 | +fig, ax = plt.subplots(figsize=(13, 6)) |
| 118 | +means = -scores.mean() |
| 119 | +errors = scores.std() |
| 120 | +means.plot.barh(xerr=errors, ax=ax) |
| 121 | +ax.set_title('California Housing Regression with Different Imputation Methods') |
| 122 | +ax.set_xlabel('MSE (smaller is better)') |
| 123 | +ax.set_yticks(np.arange(means.shape[0])) |
| 124 | +ax.set_yticklabels([" w/ ".join(label) for label in means.index.get_values()]) |
| 125 | +plt.tight_layout(pad=1) |
| 126 | +plt.show() |
0 commit comments