-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
test_clustering_kmeans.py
169 lines (143 loc) 路 5.84 KB
/
test_clustering_kmeans.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
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring
import unittest
import warnings
import numpy as np
from scipy.sparse import csc_matrix, csr_matrix
import Orange
from Orange.clustering.kmeans import KMeans, KMeansModel
from Orange.data import Table, Domain, ContinuousVariable
from Orange.data.table import DomainTransformationError
from Orange.tests.test_dasktable import with_dasktable
class TestKMeans(unittest.TestCase):
def setUp(self):
self.kmeans = KMeans(n_clusters=2)
self.iris = Orange.data.Table('iris')
@with_dasktable
def test_kmeans(self, prepare_table):
iris = prepare_table(self.iris)
c = self.kmeans(iris)
# First 20 iris belong to one cluster
self.assertEqual(type(iris.X), type(c))
self.assertEqual(len(iris), len(c))
self.assertEqual(1, len(np.unique(np.asarray(c[:20]))))
@with_dasktable
def test_kmeans_parameters(self, prepare_table):
kmeans = KMeans(n_clusters=10, max_iter=10, random_state=42, tol=0.001,
init='random')
iris = prepare_table(self.iris)
c = kmeans(iris)
self.assertEqual(type(iris.X), type(c))
self.assertEqual(len(iris), len(c))
@with_dasktable
def test_predict_table(self, prepare_table):
iris = prepare_table(self.iris)
c = self.kmeans(iris)
self.assertEqual(type(iris.X), type(c))
self.assertEqual(len(iris), len(c))
@with_dasktable
def test_predict_numpy(self, prepare_table):
iris = prepare_table(self.iris)
c = self.kmeans.fit(iris.X)
self.assertEqual(KMeansModel, type(c))
self.assertEqual(type(iris.X), type(c.labels))
self.assertEqual(len(iris), len(c.labels))
def test_predict_sparse_csc(self):
with self.iris.unlocked():
self.iris.X = csc_matrix(self.iris.X[::20])
c = self.kmeans(self.iris)
self.assertEqual(np.ndarray, type(c))
self.assertEqual(len(self.iris), len(c))
def test_predict_spares_csr(self):
with self.iris.unlocked():
self.iris.X = csr_matrix(self.iris.X[::20])
c = self.kmeans(self.iris)
self.assertEqual(np.ndarray, type(c))
self.assertEqual(len(self.iris), len(c))
@with_dasktable
def test_model(self, prepare_table):
iris = prepare_table(self.iris)
c = self.kmeans.get_model(iris)
self.assertEqual(KMeansModel, type(c))
self.assertEqual(len(iris), len(c.labels))
c1 = c(iris)
# prediction of the model must be same since data are same
np.testing.assert_array_almost_equal(c.labels, c1)
@with_dasktable
def test_model_np(self, prepare_table):
"""
Test with numpy array as an input in model.
"""
iris = prepare_table(self.iris)
c = self.kmeans.get_model(iris)
c1 = c(iris.X)
# prediction of the model must be same since data are same
np.testing.assert_array_almost_equal(c.labels, c1)
def test_model_sparse_csc(self):
"""
Test with sparse array as an input in model.
"""
c = self.kmeans.get_model(self.iris)
c1 = c(csc_matrix(self.iris.X))
# prediction of the model must be same since data are same
np.testing.assert_array_almost_equal(c.labels, c1)
def test_model_sparse_csr(self):
"""
Test with sparse array as an input in model.
"""
c = self.kmeans.get_model(self.iris)
c1 = c(csr_matrix(self.iris.X))
# prediction of the model must be same since data are same
np.testing.assert_array_almost_equal(c.labels, c1)
@with_dasktable
def test_model_instance(self, prepare_table):
"""
Test with instance as an input in model.
"""
iris = prepare_table(self.iris)
c = self.kmeans.get_model(iris)
c1 = c(iris[0])
# prediction of the model must be same since data are same
self.assertEqual(c1, c.labels[0])
def test_model_list(self):
"""
Test with list as an input in model.
"""
c = self.kmeans.get_model(self.iris)
c1 = c(np.asarray(self.iris.X).tolist())
# prediction of the model must be same since data are same
np.testing.assert_array_almost_equal(c.labels, c1)
# example with a list of only one data item
c1 = c(np.asarray(self.iris.X).tolist()[0])
# prediction of the model must be same since data are same
np.testing.assert_array_almost_equal(c.labels[0], c1)
@with_dasktable
def test_model_bad_datatype(self, prepare_table):
"""
Check model with data-type that is not supported.
"""
iris = prepare_table(self.iris)
c = self.kmeans.get_model(iris)
self.assertRaises(TypeError, c, 10)
def test_model_data_table_domain(self):
"""
Check model with data-type that is not supported.
"""
# ok domain
data = Table(Domain(
list(self.iris.domain.attributes) + [ContinuousVariable("a")]),
np.concatenate((self.iris.X, np.ones((len(self.iris), 1))), axis=1))
c = self.kmeans.get_model(self.iris)
res = c(data)
np.testing.assert_array_almost_equal(c.labels, res)
# totally different domain - should fail
self.assertRaises(DomainTransformationError, c, Table("housing"))
def test_deprecated_silhouette(self):
with warnings.catch_warnings(record=True) as w:
KMeans(compute_silhouette_score=True)
assert len(w) == 1
assert issubclass(w[-1].category, DeprecationWarning)
with warnings.catch_warnings(record=True) as w:
KMeans(compute_silhouette_score=False)
assert len(w) == 1
assert issubclass(w[-1].category, DeprecationWarning)