-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathtest_wrapper.py
179 lines (161 loc) · 6.24 KB
/
test_wrapper.py
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""Tests for the wrapper module."""
from unittest import TestCase
import category_encoders as encoders
import numpy as np
import pandas as pd
from category_encoders.wrapper import NestedCVWrapper, PolynomialWrapper
from sklearn.model_selection import GroupKFold
import tests.helpers as th
class TestMultiClassWrapper(TestCase):
"""Tests for the PolynomialWrapper class."""
def test_invariance_to_data_types(self):
"""Test that the wrapper is invariant to data types."""
x = np.array(
[
['a', 'b', 'c'],
['a', 'b', 'c'],
['b', 'b', 'c'],
['b', 'b', 'b'],
['b', 'b', 'b'],
['a', 'b', 'a'],
]
)
y = [1, 2, 3, 3, 3, 3]
wrapper = PolynomialWrapper(encoders.TargetEncoder())
result = wrapper.fit_transform(x, y)
th.verify_numeric(result)
x = pd.DataFrame(
[
['a', 'b', 'c'],
['a', 'b', 'c'],
['b', 'b', 'c'],
['b', 'b', 'b'],
['b', 'b', 'b'],
['a', 'b', 'a'],
],
columns=['f1', 'f2', 'f3'],
)
y = ['bee', 'cat', 'dog', 'dog', 'dog', 'dog']
wrapper = PolynomialWrapper(encoders.TargetEncoder())
result2 = wrapper.fit_transform(x, y)
self.assertTrue(
(result.to_numpy() == result2.to_numpy()).all(),
'The content should be the same regardless whether we pass Numpy or Pandas data type.',
)
def test_transform_only_selected(self):
"""Test that the wrapper only transforms the selected columns."""
x = pd.DataFrame(
[
['a', 'b', 'c'],
['a', 'a', 'c'],
['b', 'a', 'c'],
['b', 'c', 'b'],
['b', 'b', 'b'],
['a', 'b', 'a'],
],
columns=['f1', 'f2', 'f3'],
)
y = ['bee', 'cat', 'dog', 'dog', 'dog', 'dog']
wrapper = PolynomialWrapper(encoders.LeaveOneOutEncoder(cols=['f2']))
# combination fit() + transform()
wrapper.fit(x, y)
result = wrapper.transform(x, y)
self.assertEqual(
len(result.columns),
4,
'We expect 2 untouched features + f2 target encoded into 2 features',
)
# directly fit_transform()
wrapper = PolynomialWrapper(encoders.LeaveOneOutEncoder(cols=['f2']))
result2 = wrapper.fit_transform(x, y)
self.assertEqual(
len(result2.columns),
4,
'We expect 2 untouched features + f2 target encoded into 2 features',
)
pd.testing.assert_frame_equal(result, result2)
def test_refit_stateless(self):
"""Test that when the encoder is fitted multiple times no old state is carried."""
x = pd.DataFrame(
[
['a', 'b', 'c'],
['a', 'b', 'c'],
['b', 'b', 'c'],
['b', 'b', 'b'],
['b', 'b', 'b'],
['a', 'b', 'a'],
],
columns=['f1', 'f2', 'f3'],
)
y1 = ['bee', 'cat', 'dog', 'dog', 'dog', 'dog']
y2 = ['bee', 'cat', 'duck', 'duck', 'duck', 'duck']
wrapper = PolynomialWrapper(encoders.TargetEncoder())
_ = wrapper.fit_transform(x, y1)
expected_categories_1 = {
'cat',
'dog',
} # 'bee' is dropped since first label is always dropped
expected_categories_2 = {'cat', 'duck'}
self.assertEqual(
set(wrapper.label_encoder.ordinal_encoder.category_mapping[0]['mapping'].index),
{'bee', 'cat', 'dog'},
)
self.assertEqual(set(wrapper.feature_encoders.keys()), expected_categories_1)
_ = wrapper.fit_transform(x, y2)
self.assertEqual(
set(wrapper.label_encoder.ordinal_encoder.category_mapping[0]['mapping'].index),
{'bee', 'cat', 'duck'},
)
self.assertEqual(set(wrapper.feature_encoders.keys()), expected_categories_2)
class TestNestedCVWrapper(TestCase):
"""Tests for the NestedCVWrapper class."""
def test_train_not_equal_to_valid(self):
"""Test that the train and valid results are not equal."""
x = np.array(
[
['a', 'b', 'c'],
['a', 'b', 'c'],
['a', 'b', 'c'],
['a', 'b', 'c'],
['b', 'b', 'c'],
['b', 'b', 'c'],
['b', 'b', 'b'],
['b', 'b', 'b'],
['b', 'b', 'b'],
['b', 'b', 'b'],
['a', 'b', 'a'],
['a', 'b', 'a'],
]
)
y = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3]
wrapper = NestedCVWrapper(encoders.TargetEncoder(), cv=3)
result_train, result_valid = wrapper.fit_transform(x, y, X_test=x)
# We would expect result_train != result_valid since result_train has been generated using
# nested # folds and result_valid is generated by fitting the encoder on all the x & y data
self.assertFalse(np.allclose(result_train, result_valid))
def test_custom_cv(self):
"""Test custom cross validation."""
x = np.array(
[
['a', 'b', 'c'],
['a', 'b', 'c'],
['a', 'b', 'c'],
['a', 'b', 'c'],
['b', 'b', 'c'],
['b', 'b', 'c'],
['b', 'b', 'b'],
['b', 'b', 'b'],
['b', 'b', 'b'],
['b', 'b', 'b'],
['a', 'b', 'a'],
['a', 'b', 'a'],
]
)
groups = [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
y = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3]
gkfold = GroupKFold(n_splits=3)
wrapper = NestedCVWrapper(encoders.TargetEncoder(), cv=gkfold)
result_train, result_valid = wrapper.fit_transform(x, y, X_test=x, groups=groups)
# We would expect result_train != result_valid since result_train has been generated using
# nested # folds and result_valid is generated by fitting the encoder on all the x & y data
self.assertFalse(np.allclose(result_train, result_valid))