
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/release_highlights/plot_release_highlights_0_22_0.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_auto_examples_release_highlights_plot_release_highlights_0_22_0.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_release_highlights_plot_release_highlights_0_22_0.py:


========================================
Release Highlights for scikit-learn 0.22
========================================

.. currentmodule:: sklearn

We are pleased to announce the release of scikit-learn 0.22, which comes
with many bug fixes and new features! We detail below a few of the major
features of this release. For an exhaustive list of all the changes, please
refer to the :ref:`release notes <changes_0_22>`.

To install the latest version (with pip)::

    pip install --upgrade scikit-learn

or with conda::

    conda install scikit-learn

.. GENERATED FROM PYTHON SOURCE LINES 23-35

New plotting API
----------------

A new plotting API is available for creating visualizations. This new API
allows for quickly adjusting the visuals of a plot without involving any
recomputation. It is also possible to add different plots to the same
figure. The following example illustrates :class:`~metrics.plot_roc_curve`,
but other plots utilities are supported like
:class:`~inspection.plot_partial_dependence`,
:class:`~metrics.plot_precision_recall_curve`, and
:class:`~metrics.plot_confusion_matrix`. Read more about this new API in the
:ref:`User Guide <visualizations>`.

.. GENERATED FROM PYTHON SOURCE LINES 35-57

.. code-block:: default


    from sklearn.model_selection import train_test_split
    from sklearn.svm import SVC
    from sklearn.metrics import plot_roc_curve
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.datasets import make_classification
    import matplotlib.pyplot as plt

    X, y = make_classification(random_state=0)
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

    svc = SVC(random_state=42)
    svc.fit(X_train, y_train)
    rfc = RandomForestClassifier(random_state=42)
    rfc.fit(X_train, y_train)

    svc_disp = plot_roc_curve(svc, X_test, y_test)
    rfc_disp = plot_roc_curve(rfc, X_test, y_test, ax=svc_disp.ax_)
    rfc_disp.figure_.suptitle("ROC curve comparison")

    plt.show()




.. image-sg:: /auto_examples/release_highlights/images/sphx_glr_plot_release_highlights_0_22_0_001.png
   :alt: ROC curve comparison
   :srcset: /auto_examples/release_highlights/images/sphx_glr_plot_release_highlights_0_22_0_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 58-73

Stacking Classifier and Regressor
---------------------------------
:class:`~ensemble.StackingClassifier` and
:class:`~ensemble.StackingRegressor`
allow you to have a stack of estimators with a final classifier or
a regressor.
Stacked generalization consists in stacking the output of individual
estimators and use a classifier to compute the final prediction. Stacking
allows to use the strength of each individual estimator by using their output
as input of a final estimator.
Base estimators are fitted on the full ``X`` while
the final estimator is trained using cross-validated predictions of the
base estimators using ``cross_val_predict``.

Read more in the :ref:`User Guide <stacking>`.

.. GENERATED FROM PYTHON SOURCE LINES 73-96

.. code-block:: default


    from sklearn.datasets import load_iris
    from sklearn.svm import LinearSVC
    from sklearn.linear_model import LogisticRegression
    from sklearn.preprocessing import StandardScaler
    from sklearn.pipeline import make_pipeline
    from sklearn.ensemble import StackingClassifier
    from sklearn.model_selection import train_test_split

    X, y = load_iris(return_X_y=True)
    estimators = [
        ('rf', RandomForestClassifier(n_estimators=10, random_state=42)),
        ('svr', make_pipeline(StandardScaler(),
                              LinearSVC(random_state=42)))
    ]
    clf = StackingClassifier(
        estimators=estimators, final_estimator=LogisticRegression()
    )
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, stratify=y, random_state=42
    )
    clf.fit(X_train, y_train).score(X_test, y_test)





.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none


    0.9473684210526315



.. GENERATED FROM PYTHON SOURCE LINES 97-102

Permutation-based feature importance
------------------------------------

The :func:`inspection.permutation_importance` can be used to get an
estimate of the importance of each feature, for any fitted estimator:

.. GENERATED FROM PYTHON SOURCE LINES 102-120

.. code-block:: default


    from sklearn.ensemble import RandomForestClassifier
    from sklearn.inspection import permutation_importance

    X, y = make_classification(random_state=0, n_features=5, n_informative=3)
    rf = RandomForestClassifier(random_state=0).fit(X, y)
    result = permutation_importance(rf, X, y, n_repeats=10, random_state=0,
                                    n_jobs=-1)

    fig, ax = plt.subplots()
    sorted_idx = result.importances_mean.argsort()
    ax.boxplot(result.importances[sorted_idx].T,
               vert=False, labels=range(X.shape[1]))
    ax.set_title("Permutation Importance of each feature")
    ax.set_ylabel("Features")
    fig.tight_layout()
    plt.show()




.. image-sg:: /auto_examples/release_highlights/images/sphx_glr_plot_release_highlights_0_22_0_002.png
   :alt: Permutation Importance of each feature
   :srcset: /auto_examples/release_highlights/images/sphx_glr_plot_release_highlights_0_22_0_002.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 121-128

Native support for missing values for gradient boosting
-------------------------------------------------------

The :class:`ensemble.HistGradientBoostingClassifier`
and :class:`ensemble.HistGradientBoostingRegressor` now have native
support for missing values (NaNs). This means that there is no need for
imputing data when training or predicting.

.. GENERATED FROM PYTHON SOURCE LINES 128-139

.. code-block:: default


    from sklearn.experimental import enable_hist_gradient_boosting  # noqa
    from sklearn.ensemble import HistGradientBoostingClassifier
    import numpy as np

    X = np.array([0, 1, 2, np.nan]).reshape(-1, 1)
    y = [0, 0, 1, 1]

    gbdt = HistGradientBoostingClassifier(min_samples_leaf=1).fit(X, y)
    print(gbdt.predict(X))





.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    [0 0 1 1]




.. GENERATED FROM PYTHON SOURCE LINES 140-151

Precomputed sparse nearest neighbors graph
------------------------------------------
Most estimators based on nearest neighbors graphs now accept precomputed
sparse graphs as input, to reuse the same graph for multiple estimator fits.
To use this feature in a pipeline, one can use the `memory` parameter, along
with one of the two new transformers,
:class:`neighbors.KNeighborsTransformer` and
:class:`neighbors.RadiusNeighborsTransformer`. The precomputation
can also be performed by custom estimators to use alternative
implementations, such as approximate nearest neighbors methods.
See more details in the :ref:`User Guide <neighbors_transformer>`.

.. GENERATED FROM PYTHON SOURCE LINES 151-171

.. code-block:: default


    from tempfile import TemporaryDirectory
    from sklearn.neighbors import KNeighborsTransformer
    from sklearn.manifold import Isomap
    from sklearn.pipeline import make_pipeline

    X, y = make_classification(random_state=0)

    with TemporaryDirectory(prefix="sklearn_cache_") as tmpdir:
        estimator = make_pipeline(
            KNeighborsTransformer(n_neighbors=10, mode='distance'),
            Isomap(n_neighbors=10, metric='precomputed'),
            memory=tmpdir)
        estimator.fit(X)

        # We can decrease the number of neighbors and the graph will not be
        # recomputed.
        estimator.set_params(isomap__n_neighbors=5)
        estimator.fit(X)








.. GENERATED FROM PYTHON SOURCE LINES 172-186

KNN Based Imputation
------------------------------------
We now support imputation for completing missing values using k-Nearest
Neighbors.

Each sample's missing values are imputed using the mean value from
``n_neighbors`` nearest neighbors found in the training set. Two samples are
close if the features that neither is missing are close.
By default, a euclidean distance metric
that supports missing values,
:func:`~metrics.nan_euclidean_distances`, is used to find the nearest
neighbors.

Read more in the :ref:`User Guide <knnimpute>`.

.. GENERATED FROM PYTHON SOURCE LINES 186-194

.. code-block:: default


    import numpy as np
    from sklearn.impute import KNNImputer

    X = [[1, 2, np.nan], [3, 4, 3], [np.nan, 6, 5], [8, 8, 7]]
    imputer = KNNImputer(n_neighbors=2)
    print(imputer.fit_transform(X))





.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    [[1.  2.  4. ]
     [3.  4.  3. ]
     [5.5 6.  5. ]
     [8.  8.  7. ]]




.. GENERATED FROM PYTHON SOURCE LINES 195-201

Tree pruning
------------

It is now possible to prune most tree-based estimators once the trees are
built. The pruning is based on minimal cost-complexity. Read more in the
:ref:`User Guide <minimal_cost_complexity_pruning>` for details.

.. GENERATED FROM PYTHON SOURCE LINES 201-212

.. code-block:: default


    X, y = make_classification(random_state=0)

    rf = RandomForestClassifier(random_state=0, ccp_alpha=0).fit(X, y)
    print("Average number of nodes without pruning {:.1f}".format(
        np.mean([e.tree_.node_count for e in rf.estimators_])))

    rf = RandomForestClassifier(random_state=0, ccp_alpha=0.05).fit(X, y)
    print("Average number of nodes with pruning {:.1f}".format(
        np.mean([e.tree_.node_count for e in rf.estimators_])))





.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    Average number of nodes without pruning 22.3
    Average number of nodes with pruning 6.4




.. GENERATED FROM PYTHON SOURCE LINES 213-217

Retrieve dataframes from OpenML
-------------------------------
:func:`datasets.fetch_openml` can now return pandas dataframe and thus
properly handle datasets with heterogeneous data:

.. GENERATED FROM PYTHON SOURCE LINES 217-223

.. code-block:: default


    from sklearn.datasets import fetch_openml

    titanic = fetch_openml('titanic', version=1, as_frame=True)
    print(titanic.data.head()[['pclass', 'embarked']])



.. rst-class:: sphx-glr-script-out

.. code-block:: pytb

    Traceback (most recent call last):
      File "/build/scikit-learn-ZSX7SD/scikit-learn-0.23.2/examples/release_highlights/plot_release_highlights_0_22_0.py", line 220, in <module>
        titanic = fetch_openml('titanic', version=1, as_frame=True)
      File "/build/scikit-learn-ZSX7SD/scikit-learn-0.23.2/.pybuild/cpython3_3.10/build/sklearn/utils/validation.py", line 72, in inner_f
        return f(**kwargs)
      File "/build/scikit-learn-ZSX7SD/scikit-learn-0.23.2/.pybuild/cpython3_3.10/build/sklearn/datasets/_openml.py", line 738, in fetch_openml
        data_info = _get_data_info_by_name(name, version, data_home)
      File "/build/scikit-learn-ZSX7SD/scikit-learn-0.23.2/.pybuild/cpython3_3.10/build/sklearn/datasets/_openml.py", line 381, in _get_data_info_by_name
        json_data = _get_json_content_from_openml_api(url, None, False,
      File "/build/scikit-learn-ZSX7SD/scikit-learn-0.23.2/.pybuild/cpython3_3.10/build/sklearn/datasets/_openml.py", line 161, in _get_json_content_from_openml_api
        return _load_json()
      File "/build/scikit-learn-ZSX7SD/scikit-learn-0.23.2/.pybuild/cpython3_3.10/build/sklearn/datasets/_openml.py", line 61, in wrapper
        return f(*args, **kw)
      File "/build/scikit-learn-ZSX7SD/scikit-learn-0.23.2/.pybuild/cpython3_3.10/build/sklearn/datasets/_openml.py", line 157, in _load_json
        with closing(_open_openml_url(url, data_home)) as response:
      File "/build/scikit-learn-ZSX7SD/scikit-learn-0.23.2/.pybuild/cpython3_3.10/build/sklearn/datasets/_openml.py", line 106, in _open_openml_url
        with closing(urlopen(req)) as fsrc:
      File "/usr/lib/python3.10/urllib/request.py", line 216, in urlopen
        return opener.open(url, data, timeout)
      File "/usr/lib/python3.10/urllib/request.py", line 519, in open
        response = self._open(req, data)
      File "/usr/lib/python3.10/urllib/request.py", line 536, in _open
        result = self._call_chain(self.handle_open, protocol, protocol +
      File "/usr/lib/python3.10/urllib/request.py", line 496, in _call_chain
        result = func(*args)
      File "/usr/lib/python3.10/urllib/request.py", line 1391, in https_open
        return self.do_open(http.client.HTTPSConnection, req,
      File "/usr/lib/python3.10/urllib/request.py", line 1351, in do_open
        raise URLError(err)
    urllib.error.URLError: <urlopen error [Errno -2] Name or service not known>




.. GENERATED FROM PYTHON SOURCE LINES 224-232

Checking scikit-learn compatibility of an estimator
---------------------------------------------------
Developers can check the compatibility of their scikit-learn compatible
estimators using :func:`~utils.estimator_checks.check_estimator`. For
instance, the ``check_estimator(LinearSVC)`` passes.

We now provide a ``pytest`` specific decorator which allows ``pytest``
to run all checks independently and report the checks that are failing.

.. GENERATED FROM PYTHON SOURCE LINES 232-242

.. code-block:: default


    from sklearn.linear_model import LogisticRegression
    from sklearn.tree import DecisionTreeRegressor
    from sklearn.utils.estimator_checks import parametrize_with_checks


    @parametrize_with_checks([LogisticRegression, DecisionTreeRegressor])
    def test_sklearn_compatible_estimator(estimator, check):
        check(estimator)


.. GENERATED FROM PYTHON SOURCE LINES 243-256

ROC AUC now supports multiclass classification
----------------------------------------------
The :func:`roc_auc_score` function can also be used in multi-class
classification. Two averaging strategies are currently supported: the
one-vs-one algorithm computes the average of the pairwise ROC AUC scores, and
the one-vs-rest algorithm computes the average of the ROC AUC scores for each
class against all other classes. In both cases, the multiclass ROC AUC scores
are computed from the probability estimates that a sample belongs to a
particular class according to the model. The OvO and OvR algorithms support
weighting uniformly (``average='macro'``) and weighting by the prevalence
(``average='weighted'``).

Read more in the :ref:`User Guide <roc_metrics>`.

.. GENERATED FROM PYTHON SOURCE LINES 256-265

.. code-block:: default



    from sklearn.datasets import make_classification
    from sklearn.svm import SVC
    from sklearn.metrics import roc_auc_score

    X, y = make_classification(n_classes=4, n_informative=16)
    clf = SVC(decision_function_shape='ovo', probability=True).fit(X, y)
    print(roc_auc_score(y, clf.predict_proba(X), multi_class='ovo'))


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  1.236 seconds)


.. _sphx_glr_download_auto_examples_release_highlights_plot_release_highlights_0_22_0.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download sphx-glr-download-python

     :download:`Download Python source code: plot_release_highlights_0_22_0.py <plot_release_highlights_0_22_0.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: plot_release_highlights_0_22_0.ipynb <plot_release_highlights_0_22_0.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
