|
19 | 19 |
|
20 | 20 | """ # noqa: E501
|
21 | 21 |
|
| 22 | +# %% |
22 | 23 | import matplotlib
|
| 24 | +import matplotlib.lines as mlines |
23 | 25 | import matplotlib.pyplot as plt
|
24 | 26 | import numpy as np
|
25 | 27 |
|
|
44 | 46 | # Generate some abnormal novel observations
|
45 | 47 | X_outliers = rng.uniform(low=-4, high=4, size=(20, 2))
|
46 | 48 |
|
47 |
| -xx, yy = np.meshgrid(np.linspace(-4.5, 4.5, 50), np.linspace(-4.5, 4.5, 50)) |
48 |
| - |
49 | 49 | # OCSVM hyperparameters
|
50 | 50 | nu = 0.05
|
51 | 51 | gamma = 2.0
|
|
60 | 60 | n_error_test = y_pred_test[y_pred_test == -1].size
|
61 | 61 | n_error_outliers = y_pred_outliers[y_pred_outliers == 1].size
|
62 | 62 |
|
63 |
| -Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) |
64 |
| -Z = Z.reshape(xx.shape) |
65 |
| - |
66 |
| - |
67 | 63 | # Fit the One-Class SVM using a kernel approximation and SGD
|
68 | 64 | transform = Nystroem(gamma=gamma, random_state=random_state)
|
69 | 65 | clf_sgd = SGDOneClassSVM(
|
|
78 | 74 | n_error_test_sgd = y_pred_test_sgd[y_pred_test_sgd == -1].size
|
79 | 75 | n_error_outliers_sgd = y_pred_outliers_sgd[y_pred_outliers_sgd == 1].size
|
80 | 76 |
|
81 |
| -Z_sgd = pipe_sgd.decision_function(np.c_[xx.ravel(), yy.ravel()]) |
82 |
| -Z_sgd = Z_sgd.reshape(xx.shape) |
83 | 77 |
|
84 |
| -# plot the level sets of the decision function |
85 |
| -plt.figure(figsize=(9, 6)) |
86 |
| -plt.title("One Class SVM") |
87 |
| -plt.contourf(xx, yy, Z, levels=np.linspace(Z.min(), 0, 7), cmap=plt.cm.PuBu) |
88 |
| -a = plt.contour(xx, yy, Z, levels=[0], linewidths=2, colors="darkred") |
89 |
| -plt.contourf(xx, yy, Z, levels=[0, Z.max()], colors="palevioletred") |
| 78 | +# %% |
| 79 | +from sklearn.inspection import DecisionBoundaryDisplay |
| 80 | + |
| 81 | +_, ax = plt.subplots(figsize=(9, 6)) |
| 82 | + |
| 83 | +xx, yy = np.meshgrid(np.linspace(-4.5, 4.5, 50), np.linspace(-4.5, 4.5, 50)) |
| 84 | +X = np.concatenate([xx.ravel().reshape(-1, 1), yy.ravel().reshape(-1, 1)], axis=1) |
| 85 | +DecisionBoundaryDisplay.from_estimator( |
| 86 | + clf, |
| 87 | + X, |
| 88 | + response_method="decision_function", |
| 89 | + plot_method="contourf", |
| 90 | + ax=ax, |
| 91 | + cmap="PuBu", |
| 92 | +) |
| 93 | +DecisionBoundaryDisplay.from_estimator( |
| 94 | + clf, |
| 95 | + X, |
| 96 | + response_method="decision_function", |
| 97 | + plot_method="contour", |
| 98 | + ax=ax, |
| 99 | + linewidths=2, |
| 100 | + colors="darkred", |
| 101 | + levels=[0], |
| 102 | +) |
| 103 | +DecisionBoundaryDisplay.from_estimator( |
| 104 | + clf, |
| 105 | + X, |
| 106 | + response_method="decision_function", |
| 107 | + plot_method="contourf", |
| 108 | + ax=ax, |
| 109 | + colors="palevioletred", |
| 110 | + levels=[0, clf.decision_function(X).max()], |
| 111 | +) |
90 | 112 |
|
91 | 113 | s = 20
|
92 | 114 | b1 = plt.scatter(X_train[:, 0], X_train[:, 1], c="white", s=s, edgecolors="k")
|
93 | 115 | b2 = plt.scatter(X_test[:, 0], X_test[:, 1], c="blueviolet", s=s, edgecolors="k")
|
94 | 116 | c = plt.scatter(X_outliers[:, 0], X_outliers[:, 1], c="gold", s=s, edgecolors="k")
|
95 |
| -plt.axis("tight") |
96 |
| -plt.xlim((-4.5, 4.5)) |
97 |
| -plt.ylim((-4.5, 4.5)) |
98 |
| -plt.legend( |
99 |
| - [a.collections[0], b1, b2, c], |
| 117 | + |
| 118 | +ax.set( |
| 119 | + title="One-Class SVM", |
| 120 | + xlim=(-4.5, 4.5), |
| 121 | + ylim=(-4.5, 4.5), |
| 122 | + xlabel=( |
| 123 | + f"error train: {n_error_train}/{X_train.shape[0]}; " |
| 124 | + f"errors novel regular: {n_error_test}/{X_test.shape[0]}; " |
| 125 | + f"errors novel abnormal: {n_error_outliers}/{X_outliers.shape[0]}" |
| 126 | + ), |
| 127 | +) |
| 128 | +_ = ax.legend( |
| 129 | + [mlines.Line2D([], [], color="darkred", label="learned frontier"), b1, b2, c], |
100 | 130 | [
|
101 | 131 | "learned frontier",
|
102 | 132 | "training observations",
|
|
105 | 135 | ],
|
106 | 136 | loc="upper left",
|
107 | 137 | )
|
108 |
| -plt.xlabel( |
109 |
| - "error train: %d/%d; errors novel regular: %d/%d; errors novel abnormal: %d/%d" |
110 |
| - % ( |
111 |
| - n_error_train, |
112 |
| - X_train.shape[0], |
113 |
| - n_error_test, |
114 |
| - X_test.shape[0], |
115 |
| - n_error_outliers, |
116 |
| - X_outliers.shape[0], |
117 |
| - ) |
118 |
| -) |
119 |
| -plt.show() |
120 | 138 |
|
121 |
| -plt.figure(figsize=(9, 6)) |
122 |
| -plt.title("Online One-Class SVM") |
123 |
| -plt.contourf(xx, yy, Z_sgd, levels=np.linspace(Z_sgd.min(), 0, 7), cmap=plt.cm.PuBu) |
124 |
| -a = plt.contour(xx, yy, Z_sgd, levels=[0], linewidths=2, colors="darkred") |
125 |
| -plt.contourf(xx, yy, Z_sgd, levels=[0, Z_sgd.max()], colors="palevioletred") |
| 139 | +# %% |
| 140 | +_, ax = plt.subplots(figsize=(9, 6)) |
| 141 | + |
| 142 | +xx, yy = np.meshgrid(np.linspace(-4.5, 4.5, 50), np.linspace(-4.5, 4.5, 50)) |
| 143 | +X = np.concatenate([xx.ravel().reshape(-1, 1), yy.ravel().reshape(-1, 1)], axis=1) |
| 144 | +DecisionBoundaryDisplay.from_estimator( |
| 145 | + pipe_sgd, |
| 146 | + X, |
| 147 | + response_method="decision_function", |
| 148 | + plot_method="contourf", |
| 149 | + ax=ax, |
| 150 | + cmap="PuBu", |
| 151 | +) |
| 152 | +DecisionBoundaryDisplay.from_estimator( |
| 153 | + pipe_sgd, |
| 154 | + X, |
| 155 | + response_method="decision_function", |
| 156 | + plot_method="contour", |
| 157 | + ax=ax, |
| 158 | + linewidths=2, |
| 159 | + colors="darkred", |
| 160 | + levels=[0], |
| 161 | +) |
| 162 | +DecisionBoundaryDisplay.from_estimator( |
| 163 | + pipe_sgd, |
| 164 | + X, |
| 165 | + response_method="decision_function", |
| 166 | + plot_method="contourf", |
| 167 | + ax=ax, |
| 168 | + colors="palevioletred", |
| 169 | + levels=[0, pipe_sgd.decision_function(X).max()], |
| 170 | +) |
126 | 171 |
|
127 | 172 | s = 20
|
128 | 173 | b1 = plt.scatter(X_train[:, 0], X_train[:, 1], c="white", s=s, edgecolors="k")
|
129 | 174 | b2 = plt.scatter(X_test[:, 0], X_test[:, 1], c="blueviolet", s=s, edgecolors="k")
|
130 | 175 | c = plt.scatter(X_outliers[:, 0], X_outliers[:, 1], c="gold", s=s, edgecolors="k")
|
131 |
| -plt.axis("tight") |
132 |
| -plt.xlim((-4.5, 4.5)) |
133 |
| -plt.ylim((-4.5, 4.5)) |
134 |
| -plt.legend( |
135 |
| - [a.collections[0], b1, b2, c], |
| 176 | + |
| 177 | +ax.set( |
| 178 | + title="Online One-Class SVM", |
| 179 | + xlim=(-4.5, 4.5), |
| 180 | + ylim=(-4.5, 4.5), |
| 181 | + xlabel=( |
| 182 | + f"error train: {n_error_train_sgd}/{X_train.shape[0]}; " |
| 183 | + f"errors novel regular: {n_error_test_sgd}/{X_test.shape[0]}; " |
| 184 | + f"errors novel abnormal: {n_error_outliers_sgd}/{X_outliers.shape[0]}" |
| 185 | + ), |
| 186 | +) |
| 187 | +ax.legend( |
| 188 | + [mlines.Line2D([], [], color="darkred", label="learned frontier"), b1, b2, c], |
136 | 189 | [
|
137 | 190 | "learned frontier",
|
138 | 191 | "training observations",
|
|
141 | 194 | ],
|
142 | 195 | loc="upper left",
|
143 | 196 | )
|
144 |
| -plt.xlabel( |
145 |
| - "error train: %d/%d; errors novel regular: %d/%d; errors novel abnormal: %d/%d" |
146 |
| - % ( |
147 |
| - n_error_train_sgd, |
148 |
| - X_train.shape[0], |
149 |
| - n_error_test_sgd, |
150 |
| - X_test.shape[0], |
151 |
| - n_error_outliers_sgd, |
152 |
| - X_outliers.shape[0], |
153 |
| - ) |
154 |
| -) |
155 | 197 | plt.show()
|
0 commit comments