-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathtest_LocalClassifiers.py
More file actions
134 lines (114 loc) · 3.4 KB
/
Copy pathtest_LocalClassifiers.py
File metadata and controls
134 lines (114 loc) · 3.4 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
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
import numpy as np
import pytest
from numpy.testing import assert_array_equal
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.utils.validation import check_is_fitted
from hiclass import (
LocalClassifierPerNode,
LocalClassifierPerLevel,
LocalClassifierPerParentNode,
)
from hiclass.ConstantClassifier import ConstantClassifier
classifiers = [
LocalClassifierPerLevel,
LocalClassifierPerParentNode,
LocalClassifierPerNode,
]
@pytest.mark.parametrize("classifier", classifiers)
def test_fit_1_class(classifier):
clf = classifier(local_classifier=LogisticRegression(), n_jobs=2)
y = np.array([["1", "2"]])
X = np.array([[1, 2]])
ground_truth = np.array([["1", "2"]])
clf.fit(X, y)
prediction = clf.predict(X)
assert_array_equal(ground_truth, prediction)
@pytest.fixture
def empty_levels():
X = [
[1],
[2],
[3],
]
y = np.array(
[
["1"],
["2", "2.1"],
["3", "3.1", "3.1.2"],
],
dtype=object,
)
return X, y
@pytest.mark.parametrize("classifier", classifiers)
def test_empty_levels(empty_levels, classifier):
clf = classifier()
X, y = empty_levels
clf.fit(X, y)
predictions = clf.predict(X)
ground_truth = [
["1", "", ""],
["2", "2.1", ""],
["3", "3.1", "3.1.2"],
]
assert list(clf.hierarchy_.nodes) == [
"1",
"2",
"2" + clf.separator_ + "2.1",
"3",
"3" + clf.separator_ + "3.1",
"3" + clf.separator_ + "3.1" + clf.separator_ + "3.1.2",
clf.root_,
]
assert_array_equal(ground_truth, predictions)
@pytest.mark.parametrize("classifier", classifiers)
def test_fit_bert(classifier):
bert = ConstantClassifier()
clf = classifier(
local_classifier=bert,
bert=True,
)
X = ["Text 1", "Text 2"]
y = ["a", "a"]
clf.fit(X, y)
check_is_fitted(clf)
predictions = clf.predict(X)
assert_array_equal(y, predictions)
@pytest.mark.parametrize("classifier", classifiers)
def test_knn(classifier):
knn = KNeighborsClassifier(
n_neighbors=2,
)
clf = classifier(
local_classifier=knn,
)
y = np.array([["a", "b"], ["a", "c"]])
X = np.array([[1, 2], [3, 4]])
clf.fit(X, y)
check_is_fitted(clf)
# predictions = lcpn.predict(X)
# assert_array_equal(y, predictions)
@pytest.mark.parametrize("classifier", classifiers)
def test_fit_multiple_dim_input(classifier):
clf = classifier()
X = np.random.rand(1, 1, 275, 3)
y = np.array([["a", "b", "c"]])
clf.fit(X, y)
check_is_fitted(clf)
@pytest.mark.parametrize("classifier", classifiers)
def test_predict_multiple_dim_input(classifier):
clf = classifier()
X = np.random.rand(1, 275, 3)
y = np.array([["a", "b", "c"]])
clf.fit(X, y)
predictions = clf.predict(X)
assert predictions is not None
@pytest.mark.parametrize("classifier", classifiers)
def test_change_local_classifier(classifier):
clf = classifier(local_classifier=LogisticRegression())
y = np.array([["a", "b", "c"], ["a", "b", "d"]])
X = np.random.randint(1, 11, size=(2, 10))
clf.fit(X, y)
assert isinstance(clf.local_classifier_, LogisticRegression)
clf._change_local_classifier(KNeighborsClassifier())
assert isinstance(clf.local_classifier_, KNeighborsClassifier)