Skip to content

Commit b34096e

Browse files
committed
API Make IterativeImputer experimental (scikit-learn#13824)
1 parent 10b8fba commit b34096e

File tree

15 files changed

+1452
-1342
lines changed

15 files changed

+1452
-1342
lines changed

doc/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,9 @@
263263
'sphx_glr_plot_compare_methods_001.png': 349}
264264

265265

266-
# enable experimental module so that the new GBDTs estimators can be
266+
# enable experimental module so that experimental estimators can be
267267
# discovered properly by sphinx
268-
from sklearn.experimental import enable_hist_gradient_boosting # noqa
268+
from sklearn.experimental import * # noqa
269269

270270

271271
def make_carousel_thumbs(app, exception):

doc/modules/classes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ Samples generator
471471
:toctree: generated/
472472

473473
experimental.enable_hist_gradient_boosting
474+
experimental.enable_iterative_imputer
474475

475476

476477
.. _feature_extraction_ref:

doc/modules/impute.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,16 @@ of ``y``. This is done for each feature in an iterative fashion, and then is
105105
repeated for ``max_iter`` imputation rounds. The results of the final
106106
imputation round are returned.
107107

108+
.. note::
109+
110+
This estimator is still **experimental** for now: the predictions
111+
and the API might change without any deprecation cycle. To use it,
112+
you need to explicitly import ``enable_iterative_imputer``.
113+
114+
::
115+
108116
>>> import numpy as np
117+
>>> from sklearn.experimental import enable_iterative_imputer
109118
>>> from sklearn.impute import IterativeImputer
110119
>>> imp = IterativeImputer(max_iter=10, random_state=0)
111120
>>> imp.fit([[1, 2], [3, 6], [4, 8], [np.nan, 3], [7, np.nan]]) # doctest: +NORMALIZE_WHITESPACE

doc/whats_new/v0.21.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ Support for Python 3.4 and below has been officially dropped.
216216

217217
>>> # explicitly require this experimental feature
218218
>>> from sklearn.experimental import enable_hist_gradient_boosting # noqa
219-
>>> # now you can import normally from ensemble
219+
>>> # now you can import normally from sklearn.ensemble
220220
>>> from sklearn.ensemble import HistGradientBoostingClassifier
221221

222222
:pr:`12807` by :user:`Nicolas Hug<NicolasHug>`.
@@ -358,6 +358,15 @@ Support for Python 3.4 and below has been officially dropped.
358358
:pr:`12177` by :user:`Sergey Feldman <sergeyf>` and :user:`Ben Lawson
359359
<benlawson>`.
360360

361+
The API of IterativeImputer is experimental and subject to change without any
362+
deprecation cycle. To use them, you need to explicitly import
363+
``enable_iterative_imputer``::
364+
365+
>>> from sklearn.experimental import enable_iterative_imputer # noqa
366+
>>> # now you can import normally from sklearn.impute
367+
>>> from sklearn.impute import IterativeImputer
368+
369+
361370
- |Feature| The :class:`impute.SimpleImputer` and
362371
:class:`impute.IterativeImputer` have a new parameter ``'add_indicator'``,
363372
which simply stacks a :class:`impute.MissingIndicator` transform into the

examples/impute/plot_iterative_imputer_variants_comparison.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import matplotlib.pyplot as plt
4343
import pandas as pd
4444

45+
# To use this experimental feature, we need to explicitly ask for it:
46+
from sklearn.experimental import enable_iterative_imputer # noqa
4547
from sklearn.datasets import fetch_california_housing
4648
from sklearn.impute import SimpleImputer
4749
from sklearn.impute import IterativeImputer

examples/impute/plot_missing_values.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import numpy as np
2424
import matplotlib.pyplot as plt
2525

26+
# To use the experimental IterativeImputer, we need to explicitly ask for it:
27+
from sklearn.experimental import enable_iterative_imputer # noqa
2628
from sklearn.datasets import load_diabetes
2729
from sklearn.datasets import load_boston
2830
from sklearn.ensemble import RandomForestRegressor
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Enables IterativeImputer
2+
3+
The API and results of this estimators might change without any deprecation
4+
cycle.
5+
6+
Importing this file dynamically sets :class:`sklearn.impute.IterativeImputer`
7+
as an attribute of the impute module::
8+
9+
>>> # explicitly require this experimental feature
10+
>>> from sklearn.experimental import enable_iterative_imputer # noqa
11+
>>> # now you can import normally from impute
12+
>>> from sklearn.impute import IterativeImputer
13+
"""
14+
15+
from ..impute._iterative import IterativeImputer
16+
from .. import impute
17+
18+
impute.IterativeImputer = IterativeImputer
19+
impute.__all__ += ['IterativeImputer']
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""Tests for making sure experimental imports work as expected."""
2+
3+
import textwrap
4+
5+
from sklearn.utils.testing import assert_run_python_script
6+
7+
8+
def test_imports_strategies():
9+
# Make sure different import strategies work or fail as expected.
10+
11+
# Since Python caches the imported modules, we need to run a child process
12+
# for every test case. Else, the tests would not be independent
13+
# (manually removing the imports from the cache (sys.modules) is not
14+
# recommended and can lead to many complications).
15+
16+
good_import = """
17+
from sklearn.experimental import enable_iterative_imputer
18+
from sklearn.impute import IterativeImputer
19+
"""
20+
assert_run_python_script(textwrap.dedent(good_import))
21+
22+
good_import_with_ensemble_first = """
23+
import sklearn.ensemble
24+
from sklearn.experimental import enable_iterative_imputer
25+
from sklearn.impute import IterativeImputer
26+
"""
27+
assert_run_python_script(textwrap.dedent(good_import_with_ensemble_first))
28+
29+
bad_imports = """
30+
import pytest
31+
32+
with pytest.raises(ImportError):
33+
from sklearn.impute import IterativeImputer
34+
35+
import sklearn.experimental
36+
with pytest.raises(ImportError):
37+
from sklearn.impute import IterativeImputer
38+
"""
39+
assert_run_python_script(textwrap.dedent(bad_imports))

0 commit comments

Comments
 (0)