-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
[MRG] Merge IterativeImputer into master branch #11977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
dc67ec0
FEA Reinstate ChainedImputer
jnothman cbf89ec
Fix import of time
jnothman e4fa514
Merge branch 'master' into iterativeimputer
jnothman a4f2a89
[MRG] ChainedImputer -> IterativeImputer, and documentation update (#…
sergeyf 09a9a21
[MRG] sample from a truncated normal instead of clipping samples from…
benlawson d854b45
Merge branch 'master' into iterativeimputer
jnothman caa089e
DOC Merge IterativeImputer what's news
jnothman 1550d65
Merge branch 'master' into iterativeimputer
jnothman f103c6b
Undo changes to v0.20.rst
jnothman 9e10658
Revert changes to v0.20.rst
jnothman 0aab6dc
DOC Normalize whitespace in doctest
jnothman d34f227
Fix for SciPy 0.17
jnothman b44dff8
Fix doctest
jnothman 0453c19
Create examples/impute gallery
jnothman 8758561
Add missing readme file
jnothman f4d970e
Undo change to circle build
jnothman 34b7a46
DOC Make IterativeImputer doctest more stable (#13026)
jnothman b58bd0b
TST IterativeImputer: Check predictor type (#13039)
jnothman cf4670c
EHN: Changing default model for IterativeImputer to BayesianRidge (#1…
sergeyf dc304a4
EXA Add IterativeImputer extended example (#12100)
sergeyf 3c2716a
Merge branch 'master' into iterativeimputer
jnothman 92e7316
ENH IterativeImputer: n_iter->max_iter (#13061)
sergeyf d409e5a
Merge branch 'master' into iterativeimputer
jnothman cb3ec84
pep8
jnothman c123440
API estimator is now first param of IterativeImputer (#13153)
sergeyf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -655,8 +655,9 @@ Kernels: | |
:template: class.rst | ||
|
||
impute.SimpleImputer | ||
impute.IterativeImputer | ||
impute.MissingIndicator | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you remove this |
||
.. _kernel_approximation_ref: | ||
|
||
:mod:`sklearn.kernel_approximation` Kernel Approximation | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.. _impute_examples: | ||
|
||
Missing Value Imputation | ||
------------------------ | ||
|
||
Examples concerning the :mod:`sklearn.impute` module. |
126 changes: 126 additions & 0 deletions
126
examples/impute/plot_iterative_imputer_variants_comparison.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
""" | ||
========================================================= | ||
Imputing missing values with variants of IterativeImputer | ||
========================================================= | ||
|
||
The :class:`sklearn.impute.IterativeImputer` class is very flexible - it can be | ||
used with a variety of estimators to do round-robin regression, treating every | ||
variable as an output in turn. | ||
|
||
In this example we compare some estimators for the purpose of missing feature | ||
imputation with :class:`sklearn.imputeIterativeImputer`:: | ||
|
||
:class:`~sklearn.linear_model.BayesianRidge`: regularized linear regression | ||
:class:`~sklearn.tree.DecisionTreeRegressor`: non-linear regression | ||
:class:`~sklearn.ensemble.ExtraTreesRegressor`: similar to missForest in R | ||
:class:`~sklearn.neighbors.KNeighborsRegressor`: comparable to other KNN | ||
imputation approaches | ||
|
||
Of particular interest is the ability of | ||
:class:`sklearn.impute.IterativeImputer` to mimic the behavior of missForest, a | ||
popular imputation package for R. In this example, we have chosen to use | ||
:class:`sklearn.ensemble.ExtraTreesRegressor` instead of | ||
:class:`sklearn.ensemble.RandomForestRegressor` (as in missForest) due to its | ||
increased speed. | ||
|
||
Note that :class:`sklearn.neighbors.KNeighborsRegressor` is different from KNN | ||
imputation, which learns from samples with missing values by using a distance | ||
metric that accounts for missing values, rather than imputing them. | ||
|
||
The goal is to compare different estimators to see which one is best for the | ||
:class:`sklearn.impute.IterativeImputer` when using a | ||
:class:`sklearn.linear_model.BayesianRidge` estimator on the California housing | ||
dataset with a single value randomly removed from each row. | ||
|
||
For this particular pattern of missing values we see that | ||
:class:`sklearn.ensemble.ExtraTreesRegressor` and | ||
:class:`sklearn.linear_model.BayesianRidge` give the best results. | ||
""" | ||
print(__doc__) | ||
|
||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
import pandas as pd | ||
|
||
from sklearn.datasets import fetch_california_housing | ||
from sklearn.impute import SimpleImputer | ||
from sklearn.impute import IterativeImputer | ||
from sklearn.linear_model import BayesianRidge | ||
from sklearn.tree import DecisionTreeRegressor | ||
from sklearn.ensemble import ExtraTreesRegressor | ||
from sklearn.neighbors import KNeighborsRegressor | ||
from sklearn.pipeline import make_pipeline | ||
from sklearn.model_selection import cross_val_score | ||
|
||
N_SPLITS = 5 | ||
|
||
rng = np.random.RandomState(0) | ||
|
||
X_full, y_full = fetch_california_housing(return_X_y=True) | ||
n_samples, n_features = X_full.shape | ||
|
||
# Estimate the score on the entire dataset, with no missing values | ||
br_estimator = BayesianRidge() | ||
score_full_data = pd.DataFrame( | ||
cross_val_score( | ||
br_estimator, X_full, y_full, scoring='neg_mean_squared_error', | ||
cv=N_SPLITS | ||
), | ||
columns=['Full Data'] | ||
) | ||
|
||
# Add a single missing value to each row | ||
X_missing = X_full.copy() | ||
y_missing = y_full | ||
missing_samples = np.arange(n_samples) | ||
missing_features = rng.choice(n_features, n_samples, replace=True) | ||
X_missing[missing_samples, missing_features] = np.nan | ||
|
||
# Estimate the score after imputation (mean and median strategies) | ||
score_simple_imputer = pd.DataFrame() | ||
for strategy in ('mean', 'median'): | ||
estimator = make_pipeline( | ||
SimpleImputer(missing_values=np.nan, strategy=strategy), | ||
br_estimator | ||
) | ||
score_simple_imputer[strategy] = cross_val_score( | ||
estimator, X_missing, y_missing, scoring='neg_mean_squared_error', | ||
cv=N_SPLITS | ||
) | ||
|
||
# Estimate the score after iterative imputation of the missing values | ||
# with different estimators | ||
estimators = [ | ||
BayesianRidge(), | ||
DecisionTreeRegressor(max_features='sqrt', random_state=0), | ||
ExtraTreesRegressor(n_estimators=10, n_jobs=-1, random_state=0), | ||
KNeighborsRegressor(n_neighbors=15) | ||
] | ||
score_iterative_imputer = pd.DataFrame() | ||
for estimator in estimators: | ||
estimator = make_pipeline( | ||
IterativeImputer(random_state=0, estimator=estimator), | ||
br_estimator | ||
) | ||
score_iterative_imputer[estimator.__class__.__name__] = \ | ||
cross_val_score( | ||
estimator, X_missing, y_missing, scoring='neg_mean_squared_error', | ||
cv=N_SPLITS | ||
) | ||
|
||
scores = pd.concat( | ||
[score_full_data, score_simple_imputer, score_iterative_imputer], | ||
keys=['Original', 'SimpleImputer', 'IterativeImputer'], axis=1 | ||
) | ||
|
||
# plot boston results | ||
fig, ax = plt.subplots(figsize=(13, 6)) | ||
means = -scores.mean() | ||
errors = scores.std() | ||
means.plot.barh(xerr=errors, ax=ax) | ||
ax.set_title('California Housing Regression with Different Imputation Methods') | ||
ax.set_xlabel('MSE (smaller is better)') | ||
ax.set_yticks(np.arange(means.shape[0])) | ||
ax.set_yticklabels([" w/ ".join(label) for label in means.index.get_values()]) | ||
plt.tight_layout(pad=1) | ||
plt.show() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remove this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jnothman I assume you don't want me to make a PR for this =)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sorry I did not see it was @jnothman PR. I'll fix it then :)