22# pylint: disable=missing-docstring
33
44import unittest
5+ import warnings
56
67import numpy as np
7- from scipy .sparse import csc_matrix
8+ from scipy .sparse import csc_matrix , csr_matrix
89
910import Orange
10- from Orange .clustering .kmeans import KMeans
11+ from Orange .clustering .kmeans import KMeans , KMeansModel
12+ from Orange .data import Table , Domain , ContinuousVariable
13+ from Orange .data .table import DomainTransformationError
1114
1215
1316class TestKMeans (unittest .TestCase ):
@@ -18,25 +21,130 @@ def setUp(self):
1821 def test_kmeans (self ):
1922 c = self .kmeans (self .iris )
2023 # First 20 iris belong to one cluster
24+ self .assertEqual (np .ndarray , type (c ))
25+ self .assertEqual (len (self .iris ), len (c ))
2126 self .assertEqual (1 , len (set (c [:20 ].ravel ())))
2227
2328 def test_kmeans_parameters (self ):
2429 kmeans = KMeans (n_clusters = 10 , max_iter = 10 , random_state = 42 , tol = 0.001 ,
2530 init = 'random' )
26- kmeans (self .iris )
31+ c = kmeans (self .iris )
32+ self .assertEqual (np .ndarray , type (c ))
33+ self .assertEqual (len (self .iris ), len (c ))
2734
2835 def test_predict_table (self ):
29- kmeans = KMeans ()
30- c = kmeans (self .iris )
36+ c = self .kmeans (self .iris )
3137 self .assertEqual (np .ndarray , type (c ))
38+ self .assertEqual (len (self .iris ), len (c ))
3239
3340 def test_predict_numpy (self ):
34- kmeans = KMeans ( )
35- c = kmeans . fit ( self .iris . X )
41+ c = self . kmeans . fit ( self . iris . X )
42+ self .assertEqual ( KMeansModel , type ( c ) )
3643 self .assertEqual (np .ndarray , type (c .labels ))
44+ self .assertEqual (len (self .iris ), len (c .labels ))
3745
38- def test_predict_sparse (self ):
39- kmeans = KMeans ()
46+ def test_predict_sparse_csc (self ):
4047 self .iris .X = csc_matrix (self .iris .X [::20 ])
41- c = kmeans (self .iris )
48+ c = self . kmeans (self .iris )
4249 self .assertEqual (np .ndarray , type (c ))
50+ self .assertEqual (len (self .iris ), len (c ))
51+
52+ def test_predict_spares_csr (self ):
53+ self .iris .X = csr_matrix (self .iris .X [::20 ])
54+ c = self .kmeans (self .iris )
55+ self .assertEqual (np .ndarray , type (c ))
56+ self .assertEqual (len (self .iris ), len (c ))
57+
58+ def test_model (self ):
59+ c = self .kmeans .get_model (self .iris )
60+ self .assertEqual (KMeansModel , type (c ))
61+ self .assertEqual (len (self .iris ), len (c .labels ))
62+
63+ c1 = c (self .iris )
64+ # prediction of the model must be same since data are same
65+ np .testing .assert_array_almost_equal (c .labels , c1 )
66+
67+ def test_model_np (self ):
68+ """
69+ Test with numpy array as an input in model.
70+ """
71+ c = self .kmeans .get_model (self .iris )
72+ c1 = c (self .iris .X )
73+ # prediction of the model must be same since data are same
74+ np .testing .assert_array_almost_equal (c .labels , c1 )
75+
76+ def test_model_sparse_csc (self ):
77+ """
78+ Test with sparse array as an input in model.
79+ """
80+ c = self .kmeans .get_model (self .iris )
81+ c1 = c (csc_matrix (self .iris .X ))
82+ # prediction of the model must be same since data are same
83+ np .testing .assert_array_almost_equal (c .labels , c1 )
84+
85+ def test_model_sparse_csr (self ):
86+ """
87+ Test with sparse array as an input in model.
88+ """
89+ c = self .kmeans .get_model (self .iris )
90+ c1 = c (csr_matrix (self .iris .X ))
91+ # prediction of the model must be same since data are same
92+ np .testing .assert_array_almost_equal (c .labels , c1 )
93+
94+ def test_model_instance (self ):
95+ """
96+ Test with instance as an input in model.
97+ """
98+ c = self .kmeans .get_model (self .iris )
99+ c1 = c (self .iris [0 ])
100+ # prediction of the model must be same since data are same
101+ self .assertEqual (c1 , c .labels [0 ])
102+
103+ def test_model_list (self ):
104+ """
105+ Test with list as an input in model.
106+ """
107+ c = self .kmeans .get_model (self .iris )
108+ c1 = c (self .iris .X .tolist ())
109+ # prediction of the model must be same since data are same
110+ np .testing .assert_array_almost_equal (c .labels , c1 )
111+
112+ # example with a list of only one data item
113+ c1 = c (self .iris .X .tolist ()[0 ])
114+ # prediction of the model must be same since data are same
115+ np .testing .assert_array_almost_equal (c .labels [0 ], c1 )
116+
117+ def test_model_bad_datatype (self ):
118+ """
119+ Check model with data-type that is not supported.
120+ """
121+ c = self .kmeans .get_model (self .iris )
122+ self .assertRaises (TypeError , c , 10 )
123+
124+ def test_model_data_table_domain (self ):
125+ """
126+ Check model with data-type that is not supported.
127+ """
128+ # ok domain
129+ data = Table (Domain (
130+ list (self .iris .domain .attributes ) + [ContinuousVariable ("a" )]),
131+ np .concatenate ((self .iris .X , np .ones ((len (self .iris ), 1 ))), axis = 1 ))
132+ c = self .kmeans .get_model (self .iris )
133+ res = c (data )
134+ np .testing .assert_array_almost_equal (c .labels , res )
135+
136+ # totally different domain - should fail
137+ self .assertRaises (DomainTransformationError , c , Table ("housing" ))
138+
139+ def test_deprecated_silhouette (self ):
140+ with warnings .catch_warnings (record = True ) as w :
141+ KMeans (compute_silhouette_score = True )
142+
143+ assert len (w ) == 1
144+ assert issubclass (w [- 1 ].category , DeprecationWarning )
145+
146+ with warnings .catch_warnings (record = True ) as w :
147+ KMeans (compute_silhouette_score = False )
148+
149+ assert len (w ) == 1
150+ assert issubclass (w [- 1 ].category , DeprecationWarning )
0 commit comments