-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_tsne_predictable.py
More file actions
92 lines (82 loc) · 3.24 KB
/
test_tsne_predictable.py
File metadata and controls
92 lines (82 loc) · 3.24 KB
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
# -*- coding: utf-8 -*-
"""
@brief test log(time=10s)
"""
import unittest
import numpy
from numpy.random import RandomState
from sklearn import datasets
from sklearn.exceptions import ConvergenceWarning
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsRegressor
from sklearn.neural_network import MLPRegressor
from sklearn.manifold import TSNE
from pyquickhelper.pycode import (
ExtTestCase, skipif_circleci, ignore_warnings)
from mlinsights.mlmodel import PredictableTSNE
from mlinsights.mlmodel import test_sklearn_pickle, test_sklearn_clone
class TestPredictableTSNE(ExtTestCase):
@ignore_warnings(ConvergenceWarning)
def test_predictable_tsne(self):
iris = datasets.load_iris()
X, y = iris.data[:20], iris.target[:20]
clr = PredictableTSNE(keep_tsne_outputs=True)
clr.fit(X, y)
pred = clr.transform(X)
self.assertIsInstance(clr.estimator_, MLPRegressor)
self.assertGreater(clr.loss_, 0)
self.assertNotEmpty(pred)
@skipif_circleci('stuck')
@ignore_warnings(ConvergenceWarning)
def test_predictable_tsne_knn(self):
iris = datasets.load_iris()
X, y = iris.data[:20], iris.target[:20]
clr = PredictableTSNE(estimator=KNeighborsRegressor(),
keep_tsne_outputs=True)
clr.fit(X, y)
pred = clr.transform(X)
self.assertTrue(hasattr(clr, "tsne_outputs_"))
self.assertIsInstance(clr.estimator_, KNeighborsRegressor)
self.assertEqual(pred.shape, (X.shape[0], 2))
@ignore_warnings(ConvergenceWarning)
def test_predictable_tsne_intercept_weights(self):
iris = datasets.load_iris()
X, y = iris.data[:20], iris.target[:20]
clr = PredictableTSNE(keep_tsne_outputs=True)
clr.fit(X, y, sample_weight=numpy.ones((X.shape[0],)))
acc = clr.transform(X)
self.assertGreater(clr.loss_, 0)
self.assertEqual(acc.shape, (X.shape[0], 2))
@ignore_warnings(ConvergenceWarning)
def test_predictable_tsne_pickle(self):
iris = datasets.load_iris()
X, y = iris.data[:20], iris.target[:20]
test_sklearn_pickle(lambda: PredictableTSNE(), X, y)
@ignore_warnings(ConvergenceWarning)
def test_predictable_tsne_clone(self):
self.maxDiff = None
test_sklearn_clone(lambda: PredictableTSNE())
@ignore_warnings(ConvergenceWarning)
def test_predictable_tsne_relevance(self):
state = RandomState(seed=0)
Xs = []
Ys = []
n = 20
for i in range(0, 5):
for j in range(0, 4):
x1 = state.rand(n) + i * 1.1
x2 = state.rand(n) + j * 1.1
Xs.append(numpy.vstack([x1, x2]).T)
cl = state.randint(0, 4)
Ys.extend([cl for i in range(n)])
X = numpy.vstack(Xs)
Y = numpy.array(Ys)
clk = PredictableTSNE(transformer=TSNE(n_components=3),
normalizer=StandardScaler(with_mean=False),
keep_tsne_outputs=True)
clk.fit(X, Y)
pred = clk.transform(X)
self.assertGreater(clk.loss_, 0)
self.assertEqual(pred.shape, (X.shape[0], 3))
if __name__ == "__main__":
unittest.main()