|
26 | 26 | np.random.seed(42) |
27 | 27 |
|
28 | 28 | # Generate train data |
29 | | -X = 0.3 * np.random.randn(100, 2) |
| 29 | +X_inliers = 0.3 * np.random.randn(100, 2) |
| 30 | +X_inliers = np.r_[X_inliers + 2, X_inliers - 2] |
| 31 | + |
30 | 32 | # Generate some abnormal novel observations |
31 | 33 | X_outliers = np.random.uniform(low=-4, high=4, size=(20, 2)) |
32 | | -X = np.r_[X + 2, X - 2, X_outliers] |
| 34 | +X = np.r_[X_inliers, X_outliers] |
33 | 35 |
|
34 | 36 | # fit the model |
35 | 37 | clf = LocalOutlierFactor(n_neighbors=20) |
36 | 38 | y_pred = clf.fit_predict(X) |
37 | | -y_pred_outliers = y_pred[200:] |
38 | 39 |
|
39 | 40 | # plot the level sets of the decision function |
40 | 41 | xx, yy = np.meshgrid(np.linspace(-5, 5, 50), np.linspace(-5, 5, 50)) |
|
44 | 45 | plt.title("Local Outlier Factor (LOF)") |
45 | 46 | plt.contourf(xx, yy, Z, cmap=plt.cm.Blues_r) |
46 | 47 |
|
47 | | -a = plt.scatter(X[:200, 0], X[:200, 1], c='white', |
| 48 | +a = plt.scatter(X_inliers[:, 0], X_inliers[:, 1], c='white', |
48 | 49 | edgecolor='k', s=20) |
49 | | -b = plt.scatter(X[200:, 0], X[200:, 1], c='red', |
| 50 | +b = plt.scatter(X_outliers[:, 0], X_outliers[:, 1], c='red', |
50 | 51 | edgecolor='k', s=20) |
51 | 52 | plt.axis('tight') |
52 | 53 | plt.xlim((-5, 5)) |
|
0 commit comments