8000 Improve readability of outlier detection example. (#9973) · scikit-learn/scikit-learn@0e19710 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0e19710

Browse files
hristogagramfort
authored andcommitted
Improve readability of outlier detection example. (#9973)
1 parent 933f953 commit 0e19710

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

examples/covariance/plot_outlier_detection.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,15 @@
4343

4444
print(__doc__)
4545

46-
rng = np.random.RandomState(42)
46+
SEED = 42
47+
GRID_PRECISION = 100
48+
49+
rng = np.random.RandomState(SEED)
4750

4851
# Example settings
4952
n_samples = 200
5053
outliers_fraction = 0.25
51-
clusters_separation = [0, 1, 2]
54+
clusters_separation = (0, 1, 2)
5255

5356
# define two outlier detection tools to be compared
5457
classifiers = {
@@ -63,21 +66,23 @@
6366
contamination=outliers_fraction)}
6467

6568
# Compare given classifiers under given settings
66-
xx, yy = np.meshgrid(np.linspace(-7, 7, 100), np.linspace(-7, 7, 100))
67-
n_inliers = int((1. - outliers_fraction) * n_samples)
69+
xx, yy = np.meshgrid(np.linspace(-7, 7, GRID_PRECISION),
70+
np.linspace(-7, 7, GRID_PRECISION))
6871
n_outliers = int(outliers_fraction * n_samples)
72+
n_inliers = n_samples - n_outliers
6973
ground_truth = np.ones(n_samples, dtype=int)
7074
ground_truth[-n_outliers:] = -1
7175

7276
# Fit the problem with varying cluster separation
73-
for i, offset in enumerate(clusters_separation):
74-
np.random.seed(42)
77+
for _, offset in enumerate(clusters_separation):
78+
np.random.seed(SEED)
7579
# Data generation
7680
X1 = 0.3 * np.random.randn(n_inliers // 2, 2) - offset
7781
X2 = 0.3 * np.random.randn(n_inliers // 2, 2) + offset
78-
X = np.r_[X1, X2]
82+
X = np.concatenate([X1, X2], axis=0)
7983
# Add outliers
80-
X = np.r_[X, np.random.uniform(low=-6, high=6, size=(n_outliers, 2))]
84+
X = np.concatenate([X, np.random.uniform(low=-6, high=6,
85+
size=(n_outliers, 2))], axis=0)
8186

8287
# Fit the model
8388
plt.figure(figsize=(9, 7))

0 commit comments

Comments
 (0)
0