|
11 | 11 |
|
12 | 12 | """
|
13 | 13 |
|
14 |
| -import matplotlib.font_manager |
15 |
| -import matplotlib.pyplot as plt |
| 14 | +# %% |
16 | 15 | import numpy as np
|
17 | 16 |
|
18 | 17 | from sklearn import svm
|
19 | 18 |
|
20 |
| -xx, yy = np.meshgrid(np.linspace(-5, 5, 500), np.linspace(-5, 5, 500)) |
21 | 19 | # Generate train data
|
22 | 20 | X = 0.3 * np.random.randn(100, 2)
|
23 | 21 | X_train = np.r_[X + 2, X - 2]
|
|
37 | 35 | n_error_test = y_pred_test[y_pred_test == -1].size
|
38 | 36 | n_error_outliers = y_pred_outliers[y_pred_outliers == 1].size
|
39 | 37 |
|
40 |
| -# plot the line, the points, and the nearest vectors to the plane |
41 |
| -Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) |
42 |
| -Z = Z.reshape(xx.shape) |
| 38 | +# %% |
| 39 | +import matplotlib.font_manager |
| 40 | +import matplotlib.lines as mlines |
| 41 | +import matplotlib.pyplot as plt |
| 42 | + |
| 43 | +from sklearn.inspection import DecisionBoundaryDisplay |
43 | 44 |
|
44 |
| -plt.title("Novelty Detection") |
45 |
| -plt.contourf(xx, yy, Z, levels=np.linspace(Z.min(), 0, 7), cmap=plt.cm.PuBu) |
46 |
| -a = plt.contour(xx, yy, Z, levels=[0], linewidths=2, colors="darkred") |
47 |
| -plt.contourf(xx, yy, Z, levels=[0, Z.max()], colors="palevioletred") |
| 45 | +_, ax = plt.subplots() |
| 46 | + |
| 47 | +# generate grid for the boundary display |
| 48 | +xx, yy = np.meshgrid(np.linspace(-5, 5, 10), np.linspace(-5, 5, 10)) |
| 49 | +X = np.concatenate([xx.reshape(-1, 1), yy.reshape(-1, 1)], axis=1) |
| 50 | +DecisionBoundaryDisplay.from_estimator( |
| 51 | + clf, |
| 52 | + X, |
| 53 | + response_method="decision_function", |
| 54 | + plot_method="contourf", |
| 55 | + ax=ax, |
| 56 | + cmap="PuBu", |
| 57 | +) |
| 58 | +DecisionBoundaryDisplay.from_estimator( |
| 59 | + clf, |
| 60 | + X, |
| 61 | + response_method="decision_function", |
| 62 | + plot_method="contourf", |
| 63 | + ax=ax, |
| 64 | + levels=[0, 10000], |
| 65 | + colors="palevioletred", |
| 66 | +) |
| 67 | +DecisionBoundaryDisplay.from_estimator( |
| 68 | + clf, |
| 69 | + X, |
| 70 | + response_method="decision_function", |
| 71 | + plot_method="contour", |
| 72 | + ax=ax, |
| 73 | + levels=[0], |
| 74 | + colors="darkred", |
| 75 | + linewidths=2, |
| 76 | +) |
48 | 77 |
|
49 | 78 | s = 40
|
50 |
| -b1 = plt.scatter(X_train[:, 0], X_train[:, 1], c="white", s=s, edgecolors="k") |
51 |
| -b2 = plt.scatter(X_test[:, 0], X_test[:, 1], c="blueviolet", s=s, edgecolors="k") |
52 |
| -c = plt.scatter(X_outliers[:, 0], X_outliers[:, 1], c="gold", s=s, edgecolors="k") |
53 |
| -plt.axis("tight") |
54 |
| -plt.xlim((-5, 5)) |
55 |
| -plt.ylim((-5, 5)) |
| 79 | +b1 = ax.scatter(X_train[:, 0], X_train[:, 1], c="white", s=s, edgecolors="k") |
| 80 | +b2 = ax.scatter(X_test[:, 0], X_test[:, 1], c="blueviolet", s=s, edgecolors="k") |
| 81 | +c = ax.scatter(X_outliers[:, 0], X_outliers[:, 1], c="gold", s=s, edgecolors="k") |
56 | 82 | plt.legend(
|
57 |
| - [a.collections[0], b1, b2, c], |
| 83 | + [mlines.Line2D([], [], color="darkred"), b1, b2, c], |
58 | 84 | [
|
59 | 85 | "learned frontier",
|
60 | 86 | "training observations",
|
|
64 | 90 | loc="upper left",
|
65 | 91 | prop=matplotlib.font_manager.FontProperties(size=11),
|
66 | 92 | )
|
67 |
| -plt.xlabel( |
68 |
| - "error train: %d/200 ; errors novel regular: %d/40 ; errors novel abnormal: %d/40" |
69 |
| - % (n_error_train, n_error_test, n_error_outliers) |
| 93 | +ax.set( |
| 94 | + xlabel=( |
| 95 | + f"error train: {n_error_train}/200 ; errors novel regular: {n_error_test}/40 ;" |
| 96 | + f" errors novel abnormal: {n_error_outliers}/40" |
| 97 | + ), |
| 98 | + title="Novelty Detection", |
| 99 | + xlim=(-5, 5), |
| 100 | + ylim=(-5, 5), |
70 | 101 | )
|
71 | 102 | plt.show()
|
0 commit comments