示例#1
0
 def test_kprotoypes_cao_stocks(self):
     kproto_cao = kprototypes.KPrototypes(n_clusters=4, init='Cao',
                                          verbose=2, random_state=42)
     result = kproto_cao.fit_predict(STOCKS, categorical=[1, 2])
     expected = np.array([2, 3, 3, 3, 3, 0, 0, 0, 0, 1, 1, 1])
     assert_cluster_splits_equal(result, expected)
     self.assertTrue(result.dtype == np.dtype(np.uint16))
示例#2
0
 def test_kprotoypes_cao_stocks_ng(self):
     np.random.seed(42)
     kproto_cao = kprototypes.KPrototypes(n_clusters=4, init='Cao', verbose=2, cat_dissim=ng_dissim)
     result = kproto_cao.fit_predict(STOCKS, categorical=[1, 2])
     expected = np.array([2, 3, 3, 3, 3, 0, 0, 0, 0, 1, 1, 1])
     assert_cluster_splits_equal(result, expected)
     self.assertTrue(result.dtype == np.dtype(np.uint8))
示例#3
0
 def test_kprotoypes_cao_stocks(self):
     kproto_cao = kprototypes.KPrototypes(n_clusters=4, init='Cao',
                                          verbose=2, random_state=42)
     result = kproto_cao.fit_predict(STOCKS, categorical=[1, 2])
     expected = np.array([2, 3, 3, 3, 3, 0, 0, 0, 0, 1, 1, 1])
     assert_cluster_splits_equal(result, expected)
     self.assertTrue(result.dtype == np.dtype(np.uint16))
示例#4
0
    def test_kprotoypes_init_stocks(self):
        # Wrong order
        init_vals = [
            np.array([[3, 2],
                      [0, 2],
                      [3, 2],
                      [2, 2]]),
            np.array([[356.975],
                      [275.35],
                      [738.5],
                      [197.667]])
        ]
        kproto_init = kprototypes.KPrototypes(n_clusters=2, init=init_vals,
                                              verbose=2)
        with self.assertRaises(AssertionError):
            kproto_init.fit_predict(STOCKS, categorical=[1, 2])

        # Wrong number of clusters
        init_vals = [
            np.array([356.975, 275.35, 738.5, 197.667, 0.]),
            np.array([[3, 2],
                      [0, 2],
                      [3, 2],
                      [2, 2]])
        ]
        kproto_init = kprototypes.KPrototypes(n_clusters=4, init=init_vals,
                                              verbose=2)
        with self.assertRaises(AssertionError):
            kproto_init.fit_predict(STOCKS, categorical=[1, 2])

        # Wrong number of attributes
        init_vals = [
            np.array([356.975, 275.35, 738.5, 197.667]),
            np.array([3, 0, 3, 2])
        ]
        kproto_init = kprototypes.KPrototypes(n_clusters=4, init=init_vals,
                                              verbose=2)
        with self.assertRaises(AssertionError):
            kproto_init.fit_predict(STOCKS, categorical=[1, 2])

        init_vals = [
            np.array([[356.975],
                      [275.35],
                      [738.5],
                      [197.667]]),
            np.array([[3, 2],
                      [0, 2],
                      [3, 2],
                      [2, 2]])
        ]
        kproto_init = kprototypes.KPrototypes(n_clusters=4, init=init_vals,
                                              verbose=2, random_state=42)
        result = kproto_init.fit_predict(STOCKS, categorical=[1, 2])
        expected = np.array([2, 0, 0, 0, 0, 1, 1, 1, 1, 3, 3, 3])
        assert_cluster_splits_equal(result, expected)
        self.assertTrue(result.dtype == np.dtype(np.uint16))
示例#5
0
    def test_kprotoypes_init_stocks(self):
        # Wrong order
        init_vals = [
            np.array([[3, 2],
                      [0, 2],
                      [3, 2],
                      [2, 2]]),
            np.array([[356.975],
                      [275.35],
                      [738.5],
                      [197.667]])
        ]
        kproto_init = kprototypes.KPrototypes(n_clusters=2, init=init_vals,
                                              verbose=2)
        with self.assertRaises(AssertionError):
            kproto_init.fit_predict(STOCKS, categorical=[1, 2])

        # Wrong number of clusters
        init_vals = [
            np.array([356.975, 275.35, 738.5, 197.667, 0.]),
            np.array([[3, 2],
                      [0, 2],
                      [3, 2],
                      [2, 2]])
        ]
        kproto_init = kprototypes.KPrototypes(n_clusters=4, init=init_vals,
                                              verbose=2)
        with self.assertRaises(AssertionError):
            kproto_init.fit_predict(STOCKS, categorical=[1, 2])

        # Wrong number of attributes
        init_vals = [
            np.array([356.975, 275.35, 738.5, 197.667]),
            np.array([3, 0, 3, 2])
        ]
        kproto_init = kprototypes.KPrototypes(n_clusters=4, init=init_vals,
                                              verbose=2)
        with self.assertRaises(AssertionError):
            kproto_init.fit_predict(STOCKS, categorical=[1, 2])

        init_vals = [
            np.array([[356.975],
                      [275.35],
                      [738.5],
                      [197.667]]),
            np.array([[3, 2],
                      [0, 2],
                      [3, 2],
                      [2, 2]])
        ]
        kproto_init = kprototypes.KPrototypes(n_clusters=4, init=init_vals,
                                              verbose=2, random_state=42)
        result = kproto_init.fit_predict(STOCKS, categorical=[1, 2])
        expected = np.array([2, 0, 0, 0, 0, 1, 1, 1, 1, 3, 3, 3])
        assert_cluster_splits_equal(result, expected)
        self.assertTrue(result.dtype == np.dtype(np.uint16))
示例#6
0
 def test_kprotoypes_predict_stocks(self):
     np.random.seed(42)
     kproto_cao = kprototypes.KPrototypes(n_clusters=4,
                                          init='Cao',
                                          verbose=2)
     kproto_cao = kproto_cao.fit(STOCKS, categorical=[1, 2])
     result = kproto_cao.predict(STOCKS2, categorical=[1, 2])
     expected = np.array([1, 1, 3, 1])
     assert_cluster_splits_equal(result, expected)
     self.assertTrue(result.dtype == np.dtype(np.uint8))
示例#7
0
 def test_kprotoypes_huang_stocks(self):
     np.random.seed(42)
     kproto_huang = kprototypes.KPrototypes(n_clusters=4, n_init=1, init='Huang', verbose=2)
     # Untrained model
     with self.assertRaises(AssertionError):
         kproto_huang.predict(STOCKS, categorical=[1, 2])
     result = kproto_huang.fit_predict(STOCKS, categorical=[1, 2])
     expected = np.array([0, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1])
     assert_cluster_splits_equal(result, expected)
     self.assertTrue(result.dtype == np.dtype(np.uint8))
示例#8
0
 def test_kprotoypes_huang_stocks_parallel(self):
     kproto_huang = kprototypes.KPrototypes(n_clusters=4, n_init=4,
                                            init='Huang', verbose=2,
                                            random_state=42, n_jobs=4)
     # Untrained model
     with self.assertRaises(AssertionError):
         kproto_huang.predict(STOCKS, categorical=[1, 2])
     result = kproto_huang.fit_predict(STOCKS, categorical=[1, 2])
     expected = np.array([0, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1])
     assert_cluster_splits_equal(result, expected)
     self.assertTrue(result.dtype == np.dtype(np.uint16))
示例#9
0
 def test_kprotoypes_init_stocks_ng(self):
     init_vals = [
         np.array([[356.975], [275.35], [738.5], [197.667]]),
         np.array([[3, 2], [0, 2], [3, 2], [2, 2]])
     ]
     kproto_init = kprototypes.KPrototypes(n_clusters=4,
                                           init=init_vals,
                                           verbose=2,
                                           cat_dissim=ng_dissim,
                                           random_state=42)
     result = kproto_init.fit_predict(STOCKS, categorical=[1, 2])
     expected = np.array([2, 0, 0, 0, 0, 1, 1, 1, 1, 3, 3, 3])
     assert_cluster_splits_equal(result, expected)
     self.assertTrue(result.dtype == np.dtype(np.uint16))
示例#10
0
 def test_kmodes_fit_predict_equality(self):
     """Test whether fit_predict interface works the same as fit and predict."""
     kproto = kprototypes.KPrototypes(n_clusters=3,
                                      init='Cao',
                                      random_state=42)
     sample_weight = [0.5] * STOCKS.shape[0]
     model1 = kproto.fit(STOCKS,
                         categorical=[1, 2],
                         sample_weight=sample_weight)
     data1 = model1.predict(STOCKS, categorical=[1, 2])
     data2 = kproto.fit_predict(STOCKS,
                                categorical=[1, 2],
                                sample_weight=sample_weight)
     assert_cluster_splits_equal(data1, data2)
示例#11
0
 def test_kprotoypes_huang_stocks_ng(self):
     np.random.seed(42)
     kproto_huang = kprototypes.KPrototypes(n_clusters=4,
                                            n_init=1,
                                            init='Huang',
                                            verbose=2,
                                            cat_dissim=ng_dissim)
     # Untrained model
     with self.assertRaises(AssertionError):
         kproto_huang.predict(STOCKS, categorical=[1, 2])
     result = kproto_huang.fit_predict(STOCKS, categorical=[1, 2])
     expected = np.array([0, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1])
     assert_cluster_splits_equal(result, expected)
     self.assertTrue(result.dtype == np.dtype(np.uint8))
示例#12
0
 def test_kprotoypes_init_stocks_ng(self):
     init_vals = [
         np.array([[356.975],
                   [275.35],
                   [738.5],
                   [197.667]]),
         np.array([[3, 2],
                   [0, 2],
                   [3, 2],
                   [2, 2]])
     ]
     np.random.seed(42)
     kproto_init = kprototypes.KPrototypes(n_clusters=4, init=init_vals, verbose=2, cat_dissim=ng_dissim)
     result = kproto_init.fit_predict(STOCKS, categorical=[1, 2])
     expected = np.array([2, 0, 0, 0, 0, 1, 1, 1, 1, 3, 3, 3])
     assert_cluster_splits_equal(result, expected)
     self.assertTrue(result.dtype == np.dtype(np.uint8))