|
67 | 67 |
|
68 | 68 | # %% |
69 | 69 | import seaborn as sns |
70 | | -from sklearn.metrics import plot_confusion_matrix |
| 70 | +from sklearn.metrics import ConfusionMatrixDisplay |
71 | 71 |
|
72 | 72 | sns.set_context("poster") |
73 | 73 |
|
74 | | -disp = plot_confusion_matrix(tree, X_test, y_test, colorbar=False) |
| 74 | +disp = ConfusionMatrixDisplay.from_estimator(tree, X_test, y_test, colorbar=False) |
75 | 75 | _ = disp.ax_.set_title("Decision tree") |
76 | 76 |
|
77 | 77 | # %% [markdown] |
78 | 78 | # Classification using bagging classifier with and without sampling |
79 | 79 | # ----------------------------------------------------------------- |
80 | 80 | # |
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 |
82 | 82 | # can actually alleviate the issue induced by the class imbalancing. First, we |
83 | 83 | # 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. |
85 | 85 |
|
86 | 86 | # %% |
87 | 87 | from sklearn.ensemble import BaggingClassifier |
|
117 | 117 | import matplotlib.pyplot as plt |
118 | 118 |
|
119 | 119 | 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 | +) |
121 | 123 | axs[0].set_title("Bagging") |
122 | 124 |
|
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 | +) |
124 | 128 | axs[1].set_title("Balanced Bagging") |
125 | 129 |
|
126 | 130 | fig.tight_layout() |
|
150 | 154 | # %% [markdown] |
151 | 155 | # Similarly to the previous experiment, the balanced classifier outperform the |
152 | 156 | # classifier which learn from imbalanced bootstrap samples. In addition, random |
153 | | -# forest outsperforms the bagging classifier. |
| 157 | +# forest outperforms the bagging classifier. |
154 | 158 |
|
155 | 159 | # %% |
156 | 160 | print("Random Forest classifier performance:") |
|
166 | 170 |
|
167 | 171 | # %% |
168 | 172 | 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) |
170 | 174 | axs[0].set_title("Random forest") |
171 | 175 |
|
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) |
173 | 177 | axs[1].set_title("Balanced random forest") |
174 | 178 |
|
175 | 179 | fig.tight_layout() |
|
187 | 191 |
|
188 | 192 | from imblearn.ensemble import EasyEnsembleClassifier, RUSBoostClassifier |
189 | 193 |
|
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) |
192 | 196 | eec.fit(X_train, y_train) |
193 | 197 | y_pred_eec = eec.predict(X_test) |
194 | 198 |
|
195 | | -rusboost = RUSBoostClassifier(n_estimators=10, base_estimator=base_estimator) |
| 199 | +rusboost = RUSBoostClassifier(n_estimators=10, estimator=estimator) |
196 | 200 | rusboost.fit(X_train, y_train) |
197 | 201 | y_pred_rusboost = rusboost.predict(X_test) |
198 | 202 |
|
|
211 | 215 | # %% |
212 | 216 | fig, axs = plt.subplots(ncols=2, figsize=(10, 5)) |
213 | 217 |
|
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) |
215 | 219 | 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 | +) |
217 | 223 | axs[1].set_title("RUSBoost classifier") |
218 | 224 |
|
219 | 225 | fig.tight_layout() |
|
0 commit comments