Skip to content

Commit 021bbdf

Browse files
authored
DOC make example works with latest scikit-learn (#951)
1 parent 66e3cf1 commit 021bbdf

15 files changed

+62
-34
lines changed

doc/conf.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,6 @@
8282
# The name of the Pygments (syntax highlighting) style to use.
8383
pygments_style = "sphinx"
8484

85-
# -- Options for math equations -----------------------------------------------
86-
87-
extensions.append("sphinx.ext.imgmath")
88-
imgmath_image_format = "svg"
89-
9085
# -- Options for HTML output ----------------------------------------------
9186

9287
# The theme to use for HTML and HTML Help pages. See the documentation for

doc/whats_new/v0.10.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ Version 0.10.0 (ongoing)
66
Changelog
77
---------
88

9+
Compatibility
10+
.............
11+
12+
- Maintenance release for be compatible with scikit-learn >= 1.0.2.
13+
:pr:`946`, :pr:`947`, :pr:`949` by :user:`Guillaume Lemaitre <glemaitre>`.
14+
915
Deprecation
1016
...........
1117

@@ -19,6 +25,14 @@ Deprecation
1925
estimator where `n_jobs` is set.
2026
:pr:`887` by :user:`Guillaume Lemaitre <glemaitre>`.
2127

28+
- The parameter `base_estimator` is deprecated and will be removed in version
29+
0.12. It is impacted the following classes:
30+
:class:`~imblearn.ensemble.BalancedBaggingClassifier`,
31+
:class:`~imblearn.ensemble.EasyEnsembleClassifier`,
32+
:class:`~imblearn.ensemble.RUSBoostClassifier`.
33+
:pr:`946` by :user:`Guillaume Lemaitre <glemaitre>`.
34+
35+
2236
Enhancements
2337
............
2438

examples/api/plot_sampling_strategy_usage.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
# --------------------------------
5454
#
5555
# `sampling_strategy` can be given a `float`. For **under-sampling
56-
# methods**, it corresponds to the ratio :math:`\\alpha_{us}` defined by
57-
# :math:`N_{rM} = \\alpha_{us} \\times N_{m}` where :math:`N_{rM}` and
56+
# methods**, it corresponds to the ratio :math:`\alpha_{us}` defined by
57+
# :math:`N_{rM} = \alpha_{us} \times N_{m}` where :math:`N_{rM}` and
5858
# :math:`N_{m}` are the number of samples in the majority class after
5959
# resampling and the number of samples in the minority class, respectively.
6060

@@ -77,7 +77,7 @@
7777

7878
# %% [markdown]
7979
# For **over-sampling methods**, it correspond to the ratio
80-
# :math:`\\alpha_{os}` defined by :math:`N_{rm} = \\alpha_{os} \\times N_{M}`
80+
# :math:`\alpha_{os}` defined by :math:`N_{rm} = \alpha_{os} \times N_{M}`
8181
# where :math:`N_{rm}` and :math:`N_{M}` are the number of samples in the
8282
# minority class after resampling and the number of samples in the majority
8383
# class, respectively.

examples/applications/plot_impact_imbalanced_classes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@
345345
bag_clf = make_pipeline(
346346
preprocessor_tree,
347347
BalancedBaggingClassifier(
348-
base_estimator=HistGradientBoostingClassifier(random_state=42),
348+
estimator=HistGradientBoostingClassifier(random_state=42),
349349
n_estimators=10,
350350
random_state=42,
351351
n_jobs=2,

examples/applications/plot_over_sampling_benchmark_lfw.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474

7575
from sklearn.neighbors import KNeighborsClassifier
7676

77-
# %%
7877
from imblearn import FunctionSampler
7978
from imblearn.over_sampling import ADASYN, SMOTE, RandomOverSampler
8079
from imblearn.pipeline import make_pipeline
@@ -145,6 +144,7 @@
145144
ax.set_xlim([0, 1])
146145
ax.set_ylim([0, 1])
147146
sns.despine(offset=10, ax=ax)
147+
plt.legend(loc="lower right", fontsize=16)
148148
plt.tight_layout()
149149
plt.show()
150150

examples/ensemble/plot_comparison_ensemble_classifier.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,21 @@
6767

6868
# %%
6969
import seaborn as sns
70-
from sklearn.metrics import plot_confusion_matrix
70+
from sklearn.metrics import ConfusionMatrixDisplay
7171

7272
sns.set_context("poster")
7373

74-
disp = plot_confusion_matrix(tree, X_test, y_test, colorbar=False)
74+
disp = ConfusionMatrixDisplay.from_estimator(tree, X_test, y_test, colorbar=False)
7575
_ = disp.ax_.set_title("Decision tree")
7676

7777
# %% [markdown]
7878
# Classification using bagging classifier with and without sampling
7979
# -----------------------------------------------------------------
8080
#
81-
# Instead of using a single tree, we will check if an ensemble of decsion tree
81+
# Instead of using a single tree, we will check if an ensemble of decision tree
8282
# can actually alleviate the issue induced by the class imbalancing. First, we
8383
# will use a bagging classifier and its counter part which internally uses a
84-
# random under-sampling to balanced each boostrap sample.
84+
# random under-sampling to balanced each bootstrap sample.
8585

8686
# %%
8787
from sklearn.ensemble import BaggingClassifier
@@ -117,10 +117,14 @@
117117
import matplotlib.pyplot as plt
118118

119119
fig, axs = plt.subplots(ncols=2, figsize=(10, 5))
120-
plot_confusion_matrix(bagging, X_test, y_test, ax=axs[0], colorbar=False)
120+
ConfusionMatrixDisplay.from_estimator(
121+
bagging, X_test, y_test, ax=axs[0], colorbar=False
122+
)
121123
axs[0].set_title("Bagging")
122124

123-
plot_confusion_matrix(balanced_bagging, X_test, y_test, ax=axs[1], colorbar=False)
125+
ConfusionMatrixDisplay.from_estimator(
126+
balanced_bagging, X_test, y_test, ax=axs[1], colorbar=False
127+
)
124128
axs[1].set_title("Balanced Bagging")
125129

126130
fig.tight_layout()
@@ -150,7 +154,7 @@
150154
# %% [markdown]
151155
# Similarly to the previous experiment, the balanced classifier outperform the
152156
# classifier which learn from imbalanced bootstrap samples. In addition, random
153-
# forest outsperforms the bagging classifier.
157+
# forest outperforms the bagging classifier.
154158

155159
# %%
156160
print("Random Forest classifier performance:")
@@ -166,10 +170,10 @@
166170

167171
# %%
168172
fig, axs = plt.subplots(ncols=2, figsize=(10, 5))
169-
plot_confusion_matrix(rf, X_test, y_test, ax=axs[0], colorbar=False)
173+
ConfusionMatrixDisplay.from_estimator(rf, X_test, y_test, ax=axs[0], colorbar=False)
170174
axs[0].set_title("Random forest")
171175

172-
plot_confusion_matrix(brf, X_test, y_test, ax=axs[1], colorbar=False)
176+
ConfusionMatrixDisplay.from_estimator(brf, X_test, y_test, ax=axs[1], colorbar=False)
173177
axs[1].set_title("Balanced random forest")
174178

175179
fig.tight_layout()
@@ -187,12 +191,12 @@
187191

188192
from imblearn.ensemble import EasyEnsembleClassifier, RUSBoostClassifier
189193

190-
base_estimator = AdaBoostClassifier(n_estimators=10)
191-
eec = EasyEnsembleClassifier(n_estimators=10, base_estimator=base_estimator)
194+
estimator = AdaBoostClassifier(n_estimators=10)
195+
eec = EasyEnsembleClassifier(n_estimators=10, estimator=estimator)
192196
eec.fit(X_train, y_train)
193197
y_pred_eec = eec.predict(X_test)
194198

195-
rusboost = RUSBoostClassifier(n_estimators=10, base_estimator=base_estimator)
199+
rusboost = RUSBoostClassifier(n_estimators=10, estimator=estimator)
196200
rusboost.fit(X_train, y_train)
197201
y_pred_rusboost = rusboost.predict(X_test)
198202

@@ -211,9 +215,11 @@
211215
# %%
212216
fig, axs = plt.subplots(ncols=2, figsize=(10, 5))
213217

214-
plot_confusion_matrix(eec, X_test, y_test, ax=axs[0], colorbar=False)
218+
ConfusionMatrixDisplay.from_estimator(eec, X_test, y_test, ax=axs[0], colorbar=False)
215219
axs[0].set_title("Easy Ensemble")
216-
plot_confusion_matrix(rusboost, X_test, y_test, ax=axs[1], colorbar=False)
220+
ConfusionMatrixDisplay.from_estimator(
221+
rusboost, X_test, y_test, ax=axs[1], colorbar=False
222+
)
217223
axs[1].set_title("RUSBoost classifier")
218224

219225
fig.tight_layout()

examples/evaluation/plot_classification_report.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
=============================================
55
66
Specific metrics have been developed to evaluate classifier which has been
7-
trained using imbalanced data. "mod:`imblearn` provides a classification report
7+
trained using imbalanced data. :mod:`imblearn` provides a classification report
88
similar to :mod:`sklearn`, with additional metrics specific to imbalanced
99
learning problem.
1010
"""

examples/model_selection/plot_validation_curve.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
# %%
9494
import matplotlib.pyplot as plt
9595

96-
fig, ax = plt.subplots(figsize=(7, 5))
96+
fig, ax = plt.subplots(figsize=(7, 7))
9797
ax.plot(param_range, test_scores_mean, label="SMOTE")
9898
ax.fill_between(
9999
param_range,
@@ -111,13 +111,13 @@
111111
)
112112

113113
fig.suptitle("Validation Curve with SMOTE-CART")
114-
ax.set_xlabel("k_neighbors")
114+
ax.set_xlabel("Number of neighbors")
115115
ax.set_ylabel("Cohen's kappa")
116116

117117
# make nice plotting
118118
sns.despine(ax=ax, offset=10)
119119
ax.set_xlim([1, 10])
120120
ax.set_ylim([0.4, 0.8])
121-
ax.legend(loc="lower right")
121+
ax.legend(loc="lower right", fontsize=16)
122122
plt.tight_layout()
123123
plt.show()

examples/over-sampling/plot_comparison_over_sampling.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ def plot_decision_function(X, y, clf, ax, title=None):
262262
# density.
263263

264264
# %%
265+
from sklearn.cluster import MiniBatchKMeans
266+
265267
from imblearn.over_sampling import SVMSMOTE, BorderlineSMOTE, KMeansSMOTE
266268

267269
X, y = create_dataset(n_samples=5000, weights=(0.01, 0.05, 0.94), class_sep=0.8)
@@ -272,7 +274,9 @@ def plot_decision_function(X, y, clf, ax, title=None):
272274
SMOTE(random_state=0),
273275
BorderlineSMOTE(random_state=0, kind="borderline-1"),
274276
BorderlineSMOTE(random_state=0, kind="borderline-2"),
275-
KMeansSMOTE(random_state=0),
277+
KMeansSMOTE(
278+
kmeans_estimator=MiniBatchKMeans(n_init=1, random_state=0), random_state=0
279+
),
276280
SVMSMOTE(random_state=0),
277281
]
278282

examples/under-sampling/plot_comparison_under_sampling.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def plot_decision_function(X, y, clf, ax, title=None):
103103

104104
# %%
105105
import matplotlib.pyplot as plt
106+
from sklearn.cluster import MiniBatchKMeans
106107

107108
from imblearn import FunctionSampler
108109
from imblearn.pipeline import make_pipeline
@@ -112,7 +113,9 @@ def plot_decision_function(X, y, clf, ax, title=None):
112113

113114
samplers = {
114115
FunctionSampler(), # identity resampler
115-
ClusterCentroids(random_state=0),
116+
ClusterCentroids(
117+
estimator=MiniBatchKMeans(n_init=1, random_state=0), random_state=0
118+
),
116119
}
117120

118121
fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(15, 15))

0 commit comments

Comments
 (0)