|
1
|
1 |
|
import numpy as np |
|
2
|
1 |
|
import numpy.random as rnd |
|
3
|
1 |
|
from libtlda.iw import ImportanceWeightedClassifier |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
1 |
|
def test_init01(): |
|
7
|
|
|
"""Test for object type.""" |
|
8
|
1 |
|
clf = ImportanceWeightedClassifier() |
|
9
|
1 |
|
assert type(clf) == ImportanceWeightedClassifier |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
1 |
|
def test_init02(): |
|
13
|
|
|
"""Test for is_trained model.""" |
|
14
|
1 |
|
clf = ImportanceWeightedClassifier() |
|
15
|
1 |
|
assert not clf.is_trained |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
1 |
|
def test_iwe_ratio_Gaussians(): |
|
19
|
|
|
"""Test for estimating ratio of Gaussians.""" |
|
20
|
1 |
|
X = rnd.randn(10, 2) |
|
21
|
1 |
|
Z = rnd.randn(10, 2) + 1 |
|
22
|
1 |
|
clf = ImportanceWeightedClassifier() |
|
23
|
1 |
|
iw = clf.iwe_ratio_gaussians(X, Z) |
|
24
|
1 |
|
assert np.all(iw >= 0) |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
1 |
|
def test_iwe_logistic_discrimination(): |
|
28
|
|
|
"""Test for estimating through logistic classifier.""" |
|
29
|
1 |
|
X = rnd.randn(10, 2) |
|
30
|
1 |
|
Z = rnd.randn(10, 2) + 1 |
|
31
|
1 |
|
clf = ImportanceWeightedClassifier() |
|
32
|
1 |
|
iw = clf.iwe_logistic_discrimination(X, Z) |
|
33
|
1 |
|
assert np.all(iw >= 0) |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
1 |
|
def test_iwe_kernel_densities(): |
|
37
|
|
|
"""Test for estimating through kernel density estimation.""" |
|
38
|
1 |
|
X = rnd.randn(10, 2) |
|
39
|
1 |
|
Z = rnd.randn(10, 2) + 1 |
|
40
|
1 |
|
clf = ImportanceWeightedClassifier() |
|
41
|
1 |
|
iw = clf.iwe_kernel_densities(X, Z) |
|
42
|
1 |
|
assert np.all(iw >= 0) |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
1 |
|
def test_iwe_kernel_mean_matching(): |
|
46
|
|
|
"""Test for estimating through kernel mean matching.""" |
|
47
|
1 |
|
X = rnd.randn(10, 2) |
|
48
|
1 |
|
Z = rnd.randn(10, 2) + 1 |
|
49
|
1 |
|
clf = ImportanceWeightedClassifier() |
|
50
|
1 |
|
iw = clf.iwe_kernel_mean_matching(X, Z) |
|
51
|
1 |
|
assert np.all(iw >= 0) |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
1 |
|
def test_iwe_nearest_neighbours(): |
|
55
|
|
|
"""Test for estimating through nearest neighbours.""" |
|
56
|
1 |
|
X = rnd.randn(10, 2) |
|
57
|
1 |
|
Z = rnd.randn(10, 2) + 1 |
|
58
|
1 |
|
clf = ImportanceWeightedClassifier() |
|
59
|
1 |
|
iw = clf.iwe_nearest_neighbours(X, Z) |
|
60
|
|
|
assert np.all(iw >= 0) |
|
61
|
|
|
|