10000 Improve readability of outlier detection example. · scikit-learn/scikit-learn@d37168e · GitHub
[go: up one dir, main page]

Skip to content

Commit d37168e

Browse files
committed
Improve readability of outlier detection example.
1 parent 42ee9b7 commit d37168e

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

examples/covariance/plot_outlier_detection.py

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

4444
print(__doc__)
4545

46-
rng = np.random.RandomState(42)
46+
SEED = 42
47+
48+
rng = np.random.RandomState(SEED)
4749

4850
# Example settings
4951
n_samples = 200
5052
outliers_fraction = 0.25
51-
clusters_separation = [0, 1, 2]
53+
clusters_separation = (0, 1, 2)
5254

5355
# define two outlier detection tools to be compared
5456
classifiers = {
@@ -63,21 +65,23 @@
6365
contamination=outliers_fraction)}
6466

6567
# 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)
68+
xx, yy = np.meshgrid(np.linspace(-7, 7, n_samples // 2),
69+
np.linspace(-7, 7, n_samples // 2))
6870
n_outliers = int(outliers_fraction * n_samples)
71+
n_inliers = n_samples - n_outliers
6972
ground_truth = np.ones(n_samples, dtype=int)
7073
ground_truth[-n_outliers:] = -1
7174

7275
# Fit the problem with varying cluster separation
73-
for i, offset in enumerate(clusters_separation):
74-
np.random.seed(42)
76+
for _, offset in enumerate(clusters_separation):
77+
np.random.seed(SEED)
7578
# Data generation
7679
X1 = 0.3 * np.random.randn(n_inliers // 2, 2) - offset
7780
X2 = 0.3 * np.random.randn(n_inliers // 2, 2) + offset
78-
X = np.r_[X1, X2]
81+
X = np.concatenate([X1, X2], axis=0)
7982
# Add outliers
80-
X = np.r_[X, np.random.uniform(low=-6, high=6, size=(n_outliers, 2))]
83+
X = np.concatenate([X, np.random.uniform(low=-6, high=6,
84+
size=(n_outliers, 2))], axis=0)
8185

8286
# Fit the model
8387
plt.figure(figsize=(9, 7))

0 commit comments

Comments
 (0)
0