From 72634be5e4b8b5e6a00f57070414836c32e73ba0 Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Fri, 22 Sep 2023 22:38:57 +0200 Subject: [PATCH 1/3] DOC fix deprecation warning in plot_oneclass --- examples/svm/plot_oneclass.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/svm/plot_oneclass.py b/examples/svm/plot_oneclass.py index d4348fa0ec435..8151832a535e5 100644 --- a/examples/svm/plot_oneclass.py +++ b/examples/svm/plot_oneclass.py @@ -12,6 +12,7 @@ """ import matplotlib.font_manager +import matplotlib.lines as mlines import matplotlib.pyplot as plt import numpy as np @@ -54,7 +55,7 @@ plt.xlim((-5, 5)) plt.ylim((-5, 5)) plt.legend( - [a.collections[0], b1, b2, c], + [mlines.Line2D([], [], color="darkred"), b1, b2, c], [ "learned frontier", "training observations", From b61d64d584176471eec0a57d3f3ec6d21de95715 Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Fri, 22 Sep 2023 23:00:23 +0200 Subject: [PATCH 2/3] use DecisionBoundaryDisplay --- examples/svm/plot_oneclass.py | 70 +++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/examples/svm/plot_oneclass.py b/examples/svm/plot_oneclass.py index 8151832a535e5..847d75f183a2a 100644 --- a/examples/svm/plot_oneclass.py +++ b/examples/svm/plot_oneclass.py @@ -11,14 +11,11 @@ """ -import matplotlib.font_manager -import matplotlib.lines as mlines -import matplotlib.pyplot as plt +# %% import numpy as np from sklearn import svm -xx, yy = np.meshgrid(np.linspace(-5, 5, 500), np.linspace(-5, 5, 500)) # Generate train data X = 0.3 * np.random.randn(100, 2) X_train = np.r_[X + 2, X - 2] @@ -38,22 +35,50 @@ n_error_test = y_pred_test[y_pred_test == -1].size n_error_outliers = y_pred_outliers[y_pred_outliers == 1].size -# plot the line, the points, and the nearest vectors to the plane -Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) -Z = Z.reshape(xx.shape) +# %% +import matplotlib.font_manager +import matplotlib.lines as mlines +import matplotlib.pyplot as plt -plt.title("Novelty Detection") -plt.contourf(xx, yy, Z, levels=np.linspace(Z.min(), 0, 7), cmap=plt.cm.PuBu) -a = plt.contour(xx, yy, Z, levels=[0], linewidths=2, colors="darkred") -plt.contourf(xx, yy, Z, levels=[0, Z.max()], colors="palevioletred") +from sklearn.inspection import DecisionBoundaryDisplay + +_, ax = plt.subplots() + +# generate grid for the boundary display +xx, yy = np.meshgrid(np.linspace(-5, 5, 500), np.linspace(-5, 5, 500)) +X = np.concatenate([xx.reshape(-1, 1), yy.reshape(-1, 1)], axis=1) +DecisionBoundaryDisplay.from_estimator( + clf, + X, + response_method="decision_function", + plot_method="contourf", + ax=ax, + cmap="PuBu", +) +DecisionBoundaryDisplay.from_estimator( + clf, + X, + response_method="decision_function", + plot_method="contourf", + ax=ax, + levels=[0, 10000], + colors="palevioletred", +) +DecisionBoundaryDisplay.from_estimator( + clf, + X, + response_method="decision_function", + plot_method="contour", + ax=ax, + levels=[0], + colors="darkred", + linewidths=2, +) s = 40 -b1 = plt.scatter(X_train[:, 0], X_train[:, 1], c="white", s=s, edgecolors="k") -b2 = plt.scatter(X_test[:, 0], X_test[:, 1], c="blueviolet", s=s, edgecolors="k") -c = plt.scatter(X_outliers[:, 0], X_outliers[:, 1], c="gold", s=s, edgecolors="k") -plt.axis("tight") -plt.xlim((-5, 5)) -plt.ylim((-5, 5)) +b1 = ax.scatter(X_train[:, 0], X_train[:, 1], c="white", s=s, edgecolors="k") +b2 = ax.scatter(X_test[:, 0], X_test[:, 1], c="blueviolet", s=s, edgecolors="k") +c = ax.scatter(X_outliers[:, 0], X_outliers[:, 1], c="gold", s=s, edgecolors="k") plt.legend( [mlines.Line2D([], [], color="darkred"), b1, b2, c], [ @@ -65,8 +90,13 @@ loc="upper left", prop=matplotlib.font_manager.FontProperties(size=11), ) -plt.xlabel( - "error train: %d/200 ; errors novel regular: %d/40 ; errors novel abnormal: %d/40" - % (n_error_train, n_error_test, n_error_outliers) +ax.set( + xlabel=( + f"error train: {n_error_train}/200 ; errors novel regular: {n_error_test}/40 ;" + f" errors novel abnormal: {n_error_outliers}/40" + ), + title="Novelty Detection", + xlim=(-5, 5), + ylim=(-5, 5), ) plt.show() From 10bf6dade46ed68a874e9cb35a26c061d9269bc0 Mon Sep 17 00:00:00 2001 From: Guillaume Lemaitre Date: Mon, 25 Sep 2023 12:13:06 +0200 Subject: [PATCH 3/3] Update examples/svm/plot_oneclass.py Co-authored-by: Arturo Amor <86408019+ArturoAmorQ@users.noreply.github.com> --- examples/svm/plot_oneclass.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/svm/plot_oneclass.py b/examples/svm/plot_oneclass.py index 847d75f183a2a..4f44f42fe338e 100644 --- a/examples/svm/plot_oneclass.py +++ b/examples/svm/plot_oneclass.py @@ -45,7 +45,7 @@ _, ax = plt.subplots() # generate grid for the boundary display -xx, yy = np.meshgrid(np.linspace(-5, 5, 500), np.linspace(-5, 5, 500)) +xx, yy = np.meshgrid(np.linspace(-5, 5, 10), np.linspace(-5, 5, 10)) X = np.concatenate([xx.reshape(-1, 1), yy.reshape(-1, 1)], axis=1) DecisionBoundaryDisplay.from_estimator( clf,