|
| 1 | +# ruff: noqa: CPY001, E501 |
| 2 | +""" |
| 3 | +======================================= |
| 4 | +Release Highlights for scikit-learn 1.9 |
| 5 | +======================================= |
| 6 | +
|
| 7 | +.. currentmodule:: sklearn |
| 8 | +
|
| 9 | +We are pleased to announce the release of scikit-learn 1.9! Many bug fixes |
| 10 | +and improvements were added, as well as some key new features. Below we |
| 11 | +detail the highlights of this release. **For an exhaustive list of |
| 12 | +all the changes**, please refer to the :ref:`release notes <release_notes_1_9>`. |
| 13 | +
|
| 14 | +To install the latest version (with pip):: |
| 15 | +
|
| 16 | + pip install --upgrade scikit-learn |
| 17 | +
|
| 18 | +or with conda:: |
| 19 | +
|
| 20 | + conda install -c conda-forge scikit-learn |
| 21 | +
|
| 22 | +""" |
| 23 | + |
| 24 | +# %% |
| 25 | +# Callbacks |
| 26 | +# --------- |
| 27 | +# This release introduces **experimental** support for callbacks in scikit-learn. They |
| 28 | +# are objects that can be registered on estimators, through the `set_callbacks` method, |
| 29 | +# to be invoked at the beginning and end of key steps during fit. See the |
| 30 | +# :ref:`user guide <callbacks_user>` for more details. **Only a few estimators support |
| 31 | +# callbacks for now**, see the |
| 32 | +# :ref:`list of supported estimators <callback_compatible_estimators>`. |
| 33 | +# |
| 34 | +# Two built-in callbacks are provided in this release: |
| 35 | +# |
| 36 | +# - :class:`~sklearn.callback.ProgressBar`, to display progress bars. |
| 37 | +# - :class:`~sklearn.callback.ScoringMonitor`, to compute and log scoring metrics. |
| 38 | + |
| 39 | +from sklearn.callback import ProgressBar, ScoringMonitor |
| 40 | +from sklearn.datasets import make_classification |
| 41 | +from sklearn.linear_model import LogisticRegression |
| 42 | + |
| 43 | +X, y = make_classification( |
| 44 | + n_samples=1000, n_features=50, n_classes=10, n_informative=20, random_state=0 |
| 45 | +) |
| 46 | + |
| 47 | +scoring_monitor = ScoringMonitor(scoring="d2_log_loss_score") |
| 48 | +logreg = LogisticRegression(solver="lbfgs") |
| 49 | +logreg.set_callbacks(scoring_monitor, ProgressBar()) |
| 50 | +logreg.fit(X, y) |
| 51 | + |
| 52 | +log = scoring_monitor.get_logs().data_as_pandas |
| 53 | +log[["task_name", "task_id", "d2_log_loss_score"]] |
| 54 | + |
| 55 | +# %% |
| 56 | +# Progress bars can also be displayed for compositions of estimators. |
| 57 | +# |
| 58 | +# .. code-block:: python |
| 59 | +# |
| 60 | +# from sklearn.callback import ProgressBar |
| 61 | +# from sklearn.datasets import load_iris |
| 62 | +# from sklearn.linear_model import LogisticRegression |
| 63 | +# from sklearn.model_selection import GridSearchCV |
| 64 | +# |
| 65 | +# X, y = load_iris(return_X_y=True) |
| 66 | +# logreg = LogisticRegression(solver="lbfgs") |
| 67 | +# grid_search = GridSearchCV(logreg, {"C": [10, 1, 0.1]}, n_jobs=2) |
| 68 | +# grid_search.set_callbacks(ProgressBar()) |
| 69 | +# grid_search.fit(X, y) |
| 70 | +# |
| 71 | +# Intermediate output. Note that two sub-tasks progress concurrently because we |
| 72 | +# set `n_jobs=2`:: |
| 73 | +# |
| 74 | +# GridSearchCV - fit ━━━━━━╸ 17% 0:00:02 |
| 75 | +# GridSearchCV - search #0 ━━━━━━━━━━━━━╸ 34% 0:00:01 |
| 76 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #1 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 77 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #0 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 78 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #2 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 79 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #3 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 80 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #4 ━━━━━━━━━━━━━━━━━━━━━╸ 54% 0:00:01 |
| 81 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #5 ━━━━━━━━━━━━━━━━━ 44% 0:00:01 |
| 82 | +# |
| 83 | +# Final output displaying all the completed nested subtasks:: |
| 84 | +# |
| 85 | +# GridSearchCV - fit ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 86 | +# GridSearchCV - search #0 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 87 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #1 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 88 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #0 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 89 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #2 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 90 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #3 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 91 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #4 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 92 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #5 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 93 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #6 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 94 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #7 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 95 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #8 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 96 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #9 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 97 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #10 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 98 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #11 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 99 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #12 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 100 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #13 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 101 | +# GridSearchCV - candidate-split-evaluation | LogisticRegression - fit #14 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 102 | +# GridSearchCV - refit-with-best-params | LogisticRegression - fit #1 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00 |
| 103 | + |
| 104 | +# %% |
| 105 | +# There is also a public API to implement callback support in third-party estimators and |
| 106 | +# to implement custom callbacks. See the :ref:`developer's guide <callbacks>` for more |
| 107 | +# details. |
| 108 | +# |
| 109 | +# New callbacks and callback support in more estimators will be added in future |
| 110 | +# releases. The callback API is experimental and may evolve without deprecation. |
| 111 | + |
| 112 | +# %% |
| 113 | +# Improvements to the HTML representation of estimators |
| 114 | +# ----------------------------------------------------- |
| 115 | +# The HTML representation of estimators now includes information made available after |
| 116 | +# fit. There is a new "Fitted attributes" table that lists the fitted attributes and |
| 117 | +# their type and values. In addition, the HTML representation of transformers includes |
| 118 | +# new visual blocks showing the number and names of the output features. |
| 119 | +# |
| 120 | +# Expand the diagram below by clicking on the different visual blocks to see the new |
| 121 | +# features. |
| 122 | + |
| 123 | +import pandas as pd |
| 124 | + |
| 125 | +from sklearn.compose import make_column_transformer |
| 126 | +from sklearn.linear_model import LogisticRegression |
| 127 | +from sklearn.pipeline import make_pipeline |
| 128 | +from sklearn.preprocessing import OneHotEncoder, StandardScaler |
| 129 | + |
| 130 | +X = pd.DataFrame({"num": [0.1, 0.2, 0.3, 0.4], "cat": ["A", "C", "B", "C"]}) |
| 131 | +y = [1, 3, 1, 2] |
| 132 | + |
| 133 | +pipe = make_pipeline( |
| 134 | + make_column_transformer((StandardScaler(), ["num"]), (OneHotEncoder(), ["cat"])), |
| 135 | + LogisticRegression(), |
| 136 | +) |
| 137 | +pipe.fit(X, y) |
| 138 | + |
| 139 | +# %% |
| 140 | +# Computing metrics across thresholds |
| 141 | +# ----------------------------------- |
| 142 | +# A new function :func:`~sklearn.metrics.metric_at_thresholds` has been added to compute |
| 143 | +# an arbitrary binary classification metric across all possible decision thresholds. |
| 144 | + |
| 145 | +import matplotlib.pyplot as plt |
| 146 | + |
| 147 | +from sklearn.datasets import make_classification |
| 148 | +from sklearn.linear_model import LogisticRegression |
| 149 | +from sklearn.metrics import accuracy_score, f1_score, metric_at_thresholds |
| 150 | + |
| 151 | +X, y = make_classification(weights=[0.9, 0.1], random_state=0) |
| 152 | +lr = LogisticRegression(random_state=0).fit(X, y) |
| 153 | +y_score = lr.predict_proba(X)[:, 1] |
| 154 | + |
| 155 | +accuracy, thresholds = metric_at_thresholds(y, y_score, accuracy_score) |
| 156 | +f1, _ = metric_at_thresholds(y, y_score, f1_score) |
| 157 | + |
| 158 | +_, ax = plt.subplots() |
| 159 | +ax.plot(thresholds, accuracy, label="Accuracy") |
| 160 | +ax.plot(thresholds, f1, label="F1") |
| 161 | +ax.set_xlabel("threshold") |
| 162 | +ax.set_ylabel("metric value") |
| 163 | +ax.legend() |
| 164 | +plt.show() |
| 165 | + |
| 166 | +# %% |
| 167 | +# Sparse array configuration |
| 168 | +# -------------------------- |
| 169 | +# A new configuration key `"sparse_interface"` has been added to control the type of |
| 170 | +# sparse objects produced by functions and estimators. It is now possible to produce |
| 171 | +# sparse arrays instead of sparse matrices (default). |
| 172 | +# This continues the effort to prepare for |
| 173 | +# `SciPy's migration from sparse matrices to sparse arrays <https://docs.scipy.org/doc/scipy/reference/sparse.migration_to_sparray.html>`_. |
| 174 | + |
| 175 | +import sklearn |
| 176 | +from sklearn.preprocessing import OneHotEncoder |
| 177 | + |
| 178 | +X = [["fox", "dog", "cat"]] |
| 179 | +ohe = OneHotEncoder() |
| 180 | + |
| 181 | +with sklearn.config_context(sparse_interface="sparray"): |
| 182 | + Xt = ohe.fit_transform(X) |
| 183 | +Xt |
0 commit comments