-
-
Notifications
You must be signed in to change notification settings - Fork 554
/
test_elbow.py
449 lines (373 loc) · 14.6 KB
/
test_elbow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# tests.test_cluster.test_elbow
# Tests for the KElbowVisualizer
#
# Author: Benjamin Bengfort
# Created: Thu Mar 23 22:30:19 2017 -0400
#
# Copyright (C) 2017 The scikit-yb developers
# For license information, see LICENSE.txt
#
# ID: test_elbow.py [5a370c8] benjamin@bengfort.com $
"""
Tests for the KElbowVisualizer
"""
##########################################################################
## Imports
##########################################################################
import sys
import pytest
import numpy as np
import matplotlib.pyplot as plt
from scipy.sparse import csc_matrix, csr_matrix
from numpy.testing import assert_array_almost_equal
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans, MiniBatchKMeans
from sklearn.feature_extraction.text import TfidfVectorizer
from tests.fixtures import Dataset
from tests.base import VisualTestCase
from yellowbrick.datasets import load_hobbies
from yellowbrick.cluster.elbow import distortion_score
from yellowbrick.cluster.elbow import KElbowVisualizer, kelbow_visualizer
from yellowbrick.exceptions import YellowbrickValueError, YellowbrickWarning
from tests.base import IS_WINDOWS_OR_CONDA
try:
import pandas as pd
except ImportError:
pd = None
##########################################################################
## Data
##########################################################################
@pytest.fixture(scope="class")
def clusters(request):
# TODO: replace with make_blobs
X = np.array(
[
[-0.40020753, -4.67055317, -0.27191127, -1.49156318],
[0.37143349, -4.89391622, -1.23893945, 0.48318165],
[8.625142, -1.2372284, 1.39301471, 4.3394457],
[7.65803596, -2.21017215, 1.99175714, 3.71004654],
[0.89319875, -5.37152317, 1.50313598, 1.95284886],
[2.68362166, -5.78810913, -0.41233406, 1.94638989],
[7.63541182, -1.99606076, 0.9241231, 4.53478238],
[9.04699415, -0.74540679, 0.98042851, 5.99569071],
[1.02552122, -5.73874278, -1.74804915, -0.07831216],
[7.18135665, -3.49473178, 1.14300963, 4.46065816],
[0.58812902, -4.66559815, -0.72831685, 1.40171779],
[1.48620862, -5.9963108, 0.19145963, -1.11369256],
[7.6625556, -1.21328083, 2.06361094, 6.2643551],
[9.45050727, -1.36536078, 1.31154384, 3.89103468],
[6.88203724, -1.62040255, 3.89961049, 2.12865388],
[5.60842705, -2.10693356, 1.93328514, 3.90825432],
[2.35150936, -6.62836131, -1.84278374, 0.51540886],
[1.17446451, -5.62506058, -2.18420699, 1.21385128],
]
)
y = np.array([0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0])
request.cls.clusters = Dataset(X, y)
##########################################################################
## K-Elbow Helpers Test Cases
##########################################################################
@pytest.mark.usefixtures("clusters")
class TestKElbowHelper(object):
"""
Helper functions for K-Elbow Visualizer
"""
def test_distortion_score(self):
"""
Test the distortion score metric function
"""
score = distortion_score(self.clusters.X, self.clusters.y)
assert score == pytest.approx(69.10006514142941)
@pytest.mark.parametrize("func", [csc_matrix, csr_matrix], ids=["csc", "csr"])
def test_distortion_score_sparse_matrix_input(self, func):
"""
Test the distortion score metric on a sparse array
"""
score = distortion_score(func(self.clusters.X), self.clusters.y)
assert score == pytest.approx(69.10006514142938)
@pytest.mark.skipif(pd is None, reason="pandas is required")
def test_distortion_score_pandas_input(self):
"""
Test the distortion score metric on pandas DataFrame and Series
"""
df = pd.DataFrame(self.clusters.X)
s = pd.Series(self.clusters.y)
score = distortion_score(df, s)
assert score == pytest.approx(69.10006514142941)
##########################################################################
## KElbowVisualizer Test Cases
##########################################################################
@pytest.mark.usefixtures("clusters")
class TestKElbowVisualizer(VisualTestCase):
"""
K-Elbow Visualizer Tests
"""
@pytest.mark.xfail(reason="images not close due to timing lines")
def test_integrated_kmeans_elbow(self):
"""
Test no exceptions for kmeans k-elbow visualizer on blobs dataset
"""
# NOTE #182: cannot use occupancy dataset because of memory usage
# Generate a blobs data set
X, y = make_blobs(
n_samples=1000, n_features=12, centers=6, shuffle=True, random_state=42
)
try:
_, ax = plt.subplots()
visualizer = KElbowVisualizer(KMeans(random_state=42), k=4, ax=ax)
visualizer.fit(X)
visualizer.finalize()
self.assert_images_similar(visualizer)
except Exception as e:
pytest.fail("error during k-elbow: {}".format(e))
@pytest.mark.xfail(reason="images not close due to timing lines")
def test_integrated_mini_batch_kmeans_elbow(self):
"""
Test no exceptions for mini-batch kmeans k-elbow visualizer
"""
# NOTE #182: cannot use occupancy dataset because of memory usage
# Generate a blobs data set
X, y = make_blobs(
n_samples=1000, n_features=12, centers=6, shuffle=True, random_state=42
)
try:
_, ax = plt.subplots()
visualizer = KElbowVisualizer(MiniBatchKMeans(random_state=42), k=4, ax=ax)
visualizer.fit(X)
visualizer.finalize()
self.assert_images_similar(visualizer)
except Exception as e:
pytest.fail("error during k-elbow: {}".format(e))
@pytest.mark.skip(reason="takes over 20 seconds to run")
def test_topic_modeling_k_means(self):
"""
Test topic modeling k-means on the hobbies corpus
"""
corpus = load_hobbies()
tfidf = TfidfVectorizer()
docs = tfidf.fit_transform(corpus.data)
visualizer = KElbowVisualizer(KMeans(), k=(4, 8))
visualizer.fit(docs)
visualizer.finalize()
self.assert_images_similar(visualizer)
def test_invalid_k(self):
"""
Assert that invalid values of K raise exceptions
"""
with pytest.raises(YellowbrickValueError):
KElbowVisualizer(KMeans(), k=(1, 2, 3, "foo", 5))
with pytest.raises(YellowbrickValueError):
KElbowVisualizer(KMeans(), k="foo")
def test_valid_k(self):
"""
Assert that valid values of K generate correct k_values_
"""
# if k is an int, k_values_ = range(2, k+1)
# if k is a tuple of 2 ints, k_values = range(k[0], k[1])
# if k is an iterable, k_values_ = list(k)
visualizer = KElbowVisualizer(KMeans(), k=8)
assert visualizer.k_values_ == list(np.arange(2, 8 + 1))
visualizer = KElbowVisualizer(KMeans(), k=(4, 12))
assert visualizer.k_values_ == list(np.arange(4, 12))
visualizer = KElbowVisualizer(KMeans(), k=np.arange(10, 100, 10))
assert visualizer.k_values_ == list(np.arange(10, 100, 10))
visualizer = KElbowVisualizer(KMeans(), k=[10, 20, 30, 40, 50, 60, 70, 80, 90])
assert visualizer.k_values_ == list(np.arange(10, 100, 10))
@pytest.mark.xfail(sys.platform == "win32", reason="images not close on windows")
def test_distortion_metric(self):
"""
Test the distortion metric of the k-elbow visualizer
"""
visualizer = KElbowVisualizer(
KMeans(random_state=0),
k=5,
metric="distortion",
timings=False,
locate_elbow=False,
)
visualizer.fit(self.clusters.X)
expected = np.array(
[
69.10006514142941,
54.081571290449936,
44.491830981793605,
33.99887993254433,
]
)
assert len(visualizer.k_scores_) == 4
visualizer.finalize()
self.assert_images_similar(visualizer, tol=0.03)
assert_array_almost_equal(visualizer.k_scores_, expected)
@pytest.mark.xfail(sys.platform == "win32", reason="images not close on windows")
def test_silhouette_metric(self):
"""
Test the silhouette metric of the k-elbow visualizer
"""
visualizer = KElbowVisualizer(
KMeans(random_state=0),
k=5,
metric="silhouette",
timings=False,
locate_elbow=False,
)
visualizer.fit(self.clusters.X)
expected = np.array(
[
0.6916363804000003,
0.456645663683503,
0.26918583373704463,
0.25523298106687914,
]
)
assert len(visualizer.k_scores_) == 4
visualizer.finalize()
self.assert_images_similar(visualizer)
assert_array_almost_equal(visualizer.k_scores_, expected)
@pytest.mark.xfail(sys.platform == "win32", reason="images not close on windows")
def test_calinski_harabasz_metric(self):
"""
Test the calinski-harabasz metric of the k-elbow visualizer
"""
visualizer = KElbowVisualizer(
KMeans(random_state=0),
k=5,
metric="calinski_harabasz",
timings=False,
locate_elbow=False,
)
visualizer.fit(self.clusters.X)
assert len(visualizer.k_scores_) == 4
assert visualizer.elbow_value_ is None
expected = np.array(
[
81.66272625603568,
50.992378259195554,
39.573201061900455,
37.06865804955547,
]
)
visualizer.finalize()
self.assert_images_similar(visualizer)
assert_array_almost_equal(visualizer.k_scores_, expected)
@pytest.mark.xfail(
IS_WINDOWS_OR_CONDA,
reason="computation of k_scores_ varies by 2.867 max absolute difference",
)
def test_locate_elbow(self):
"""
Test the addition of locate_elbow to an image
"""
X, y = make_blobs(
n_samples=1000, n_features=5, centers=3, shuffle=True, random_state=42
)
visualizer = KElbowVisualizer(
KMeans(random_state=0),
k=6,
metric="calinski_harabasz",
timings=False,
locate_elbow=True,
)
visualizer.fit(X)
assert len(visualizer.k_scores_) == 5
assert visualizer.elbow_value_ == 3
expected = np.array([4286.5, 12463.4, 8763.3, 6938.2, 5858.4])
visualizer.finalize()
self.assert_images_similar(visualizer, tol=0.5, windows_tol=2.2)
assert_array_almost_equal(visualizer.k_scores_, expected, decimal=1)
def test_no_knee(self):
"""
Assert that a warning is issued if there is no knee detected
"""
X, y = make_blobs(n_samples=1000, centers=3, n_features=12, random_state=12)
message = (
"No 'knee' or 'elbow point' detected "
"This could be due to bad clustering, no "
"actual clusters being formed etc."
)
with pytest.warns(YellowbrickWarning, match=message):
visualizer = KElbowVisualizer(
KMeans(random_state=12), k=(4, 12), locate_elbow=True
)
visualizer.fit(X)
def test_bad_metric(self):
"""
Assert KElbow raises an exception when a bad metric is supplied
"""
with pytest.raises(YellowbrickValueError):
KElbowVisualizer(KMeans(), k=5, metric="foo")
@pytest.mark.xfail(
IS_WINDOWS_OR_CONDA,
reason="font rendering different in OS and/or Python; see #892",
)
def test_timings(self):
"""
Test the twinx double axes with k-elbow timings
"""
visualizer = KElbowVisualizer(
KMeans(random_state=0), k=5, timings=True, locate_elbow=False
)
visualizer.fit(self.clusters.X)
# Check that we kept track of time
assert len(visualizer.k_timers_) == 4
assert all([t > 0 for t in visualizer.k_timers_])
# Check that we plotted time on a twinx
assert hasattr(visualizer, "axes")
assert len(visualizer.axes) == 2
# delete the timings axes and
# overwrite k_timers_, k_values_ for image similarity Tests
visualizer.axes[1].remove()
visualizer.k_timers_ = [
0.01084589958190918,
0.011144161224365234,
0.017028093338012695,
0.010634183883666992,
]
visualizer.k_values_ = [2, 3, 4, 5]
# call draw again which is normally called in fit
visualizer.draw()
visualizer.finalize()
self.assert_images_similar(visualizer)
def test_sample_weights(self):
"""
Test that passing in sample weights correctly influences the clusterer's fit
"""
seed = 1234
# original data has 5 clusters
X, y = make_blobs(
n_samples=[5, 30, 30, 30, 30],
n_features=5,
random_state=seed,
shuffle=False,
)
visualizer = KElbowVisualizer(
KMeans(random_state=seed), k=(2, 12), timings=False
)
visualizer.fit(X)
assert visualizer.elbow_value_ == 5
# weights should push elbow down to 4
weights = np.concatenate([np.ones(5) * 0.0001, np.ones(120)])
visualizer.fit(X, sample_weight=weights)
assert visualizer.elbow_value_ == 4
@pytest.mark.xfail(reason="images not close due to timing lines")
def test_quick_method(self):
"""
Test the quick method producing a valid visualization
"""
X, y = make_blobs(
n_samples=1000, n_features=12, centers=8, shuffle=False, random_state=2
)
model = MiniBatchKMeans(3, random_state=43)
oz = kelbow_visualizer(model, X, show=False)
assert isinstance(oz, KElbowVisualizer)
self.assert_images_similar(oz)
def test_quick_method_params(self):
"""
Test the quick method correctly consumes the user-provided parameters
"""
X, y = make_blobs(centers=3)
custom_title = "My custom title"
model = KMeans(3, random_state=13)
oz = kelbow_visualizer(
model, X, sample_weight=np.ones(X.shape[0]), title=custom_title
)
assert oz.title == custom_title