-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_models.py
299 lines (263 loc) · 12.6 KB
/
test_models.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import unittest
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.linear_model import LogisticRegression, Perceptron
from sklearn.model_selection import RepeatedStratifiedKFold, cross_validate, train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.tree import DecisionTreeClassifier
import numpy as np
from codetemplate.src.batch_score import batch_prediction
from codetemplate.src.data_processing import load_data, preprocess_dataset
class ModelTestCase(unittest.TestCase):
def setUp(self) -> None:
"""
Setting up the dataset for unit tests.
"""
filename = \
'../../../../data/data_unittest.csv'
self.dataset = load_data(filename)
# Putting response variable to y
y = self.dataset['b_gekauft_gesamt']
# dropping the target variable for the training data
X = self.dataset.drop('b_gekauft_gesamt', axis=1)
# Splitting the data into train and test
self.X_train, self.X_test, self.y_train, self.y_test\
= train_test_split(X, y, train_size=0.8, test_size=0.2, random_state=100)
def test_output_shape_of_decision_tree_classifier(self):
"""
Test to check the output shape of the Decision Tree Classifier.
"""
dt = DecisionTreeClassifier()
dt.fit(self.X_train, self.y_train)
pred_train = dt.predict(self.X_train)
pred_test = dt.predict(self.X_test)
# =================================
# TEST SUITE
# =================================
assert pred_train.shape == (self.X_train.shape[0],),\
'DecisionTree output should be same as training labels.'
assert pred_test.shape == (self.X_test.shape[0],),\
'DecisionTree output should be same as testing labels.'
def test_output_shape_of_random_forrest_classifier(self):
"""
Test to check the output shape of the Random Forrest Classifier.
"""
rf = RandomForestClassifier()
rf.fit(self.X_train, self.y_train)
pred_train = rf.predict(self.X_train)
pred_test = rf.predict(self.X_test)
# =================================
# TEST SUITE
# =================================
assert pred_train.shape == (self.X_train.shape[0],),\
'RandomForrest output should be same as training labels.'
assert pred_test.shape == (self.X_test.shape[0],),\
'RandomForrest output should be same as testing labels.'
def test_output_shape_of_logistic_regression_classifier(self):
"""
Test to check the output shape of the Logistic Regression Classifier.
"""
lr = LogisticRegression()
lr.fit(self.X_train, self.y_train)
pred_train = lr.predict(self.X_train)
pred_test = lr.predict(self.X_test)
# =================================
# TEST SUITE
# =================================
assert pred_train.shape == (self.X_train.shape[0],),\
'Logistic Regression output should be same as training labels.'
assert pred_test.shape == (self.X_test.shape[0],),\
'Logistic Regression output should be same as testing labels.'
def test_output_shape_of_perceptron_classifier(self):
"""
Test to check the output shape of the Perceptron Classifier.
"""
perc = Perceptron()
perc.fit(self.X_train, self.y_train)
pred_train = perc.predict(self.X_train)
pred_test = perc.predict(self.X_test)
# =================================
# TEST SUITE
# =================================
assert pred_train.shape == (self.X_train.shape[0],), \
'Perceptron output should be same as training labels.'
assert pred_test.shape == (self.X_test.shape[0],), \
'Perceptron output should be same as testing labels.'
def test_output_shape_of_mlp_classifier(self):
"""
Test to check the output shape of the Multi-layer Perceptron Classifier.
"""
mlp = MLPClassifier(random_state=1, early_stopping=True)
mlp.fit(self.X_train, self.y_train)
pred_train = mlp.predict(self.X_train)
pred_test = mlp.predict(self.X_test)
# =================================
# TEST SUITE
# =================================
assert pred_train.shape == (self.X_train.shape[0],), \
'Multi-layer Perceptron output should be same as training labels.'
assert pred_test.shape == (self.X_test.shape[0],), \
'Multi-layer Perceptron output should be same as testing labels.'
def test_output_shape_of_gbr_classifier(self):
"""
Test to check the output shape of the Gradient Boosting Classifier.
"""
gbr = GradientBoostingClassifier()
gbr.fit(self.X_train, self.y_train)
pred_train = gbr.predict(self.X_train)
pred_test = gbr.predict(self.X_test)
# =================================
# TEST SUITE
# =================================
assert pred_train.shape == (self.X_train.shape[0],), \
'Gradient Boosting output should be same as training labels.'
assert pred_test.shape == (self.X_test.shape[0],), \
'Gradient Boosting output should be same as testing labels.'
def test_output_range_of_decision_tree_classifier(self):
"""
Test to check the output range of the Decision Tree Classifier.
"""
dt = DecisionTreeClassifier()
dt.fit(self.X_train, self.y_train)
pred_train = dt.predict(self.X_train)
pred_test = dt.predict(self.X_test)
# =================================
# TEST SUITE
# =================================
assert (pred_train <= 1).all() & (pred_train >= 0).all(), \
'Decision tree output should range from 0 to 1 inclusive'
assert (pred_test <= 1).all() & (pred_test >= 0).all(), \
'Decision tree output should range from 0 to 1 inclusive'
def test_output_range_of_logistic_regression_classifier(self):
"""
Test to check the output range of the Logistic Regression Classifier.
"""
lr = LogisticRegression()
lr.fit(self.X_train, self.y_train)
pred_train = lr.predict(self.X_train)
pred_test = lr.predict(self.X_test)
# =================================
# TEST SUITE
# =================================
assert (pred_train <= 1).all() & (pred_train >= 0).all(), \
'Logistic Regression output should range from 0 to 1 inclusive'
assert (pred_test <= 1).all() & (pred_test >= 0).all(), \
'Logistic Regression output should range from 0 to 1 inclusive'
def test_output_range_of_gbr_classifier(self):
"""
Test to check the output range of the Gradient Boosting Classifier.
"""
gbr = GradientBoostingClassifier()
gbr.fit(self.X_train, self.y_train)
pred_train = gbr.predict(self.X_train)
pred_test = gbr.predict(self.X_test)
# =================================
# TEST SUITE
# =================================
assert (pred_train <= 1).all() & (pred_train >= 0).all(), \
'Gradient Boosting Regression output should range from 0 to 1 inclusive'
assert (pred_test <= 1).all() & (pred_test >= 0).all(), \
'Gradient Boosting Regression output should range from 0 to 1 inclusive'
def test_output_range_of_random_forrest_classifier(self):
"""
Test to check the output range of the Random Forrest Classifier.
"""
rf = RandomForestClassifier()
rf.fit(self.X_train, self.y_train)
pred_train = rf.predict(self.X_train)
pred_test = rf.predict(self.X_test)
# =================================
# TEST SUITE
# =================================
assert (pred_train <= 1).all() & (pred_train >= 0).all(), \
'RandomForrest output should range from 0 to 1 inclusive'
assert (pred_test <= 1).all() & (pred_test >= 0).all(), \
'RandomForrest output should range from 0 to 1 inclusive'
def test_output_range_of_perceptron_classifier(self):
"""
Test to check the output range of the Perceptron Classifier.
"""
pr = Perceptron()
pr.fit(self.X_train, self.y_train)
pred_train = pr.predict(self.X_train)
pred_test = pr.predict(self.X_test)
# =================================
# TEST SUITE
# =================================
assert (pred_train <= 1).all() & (pred_train >= 0).all(), \
'Perceptron output should range from 0 to 1 inclusive'
assert (pred_test <= 1).all() & (pred_test >= 0).all(), \
'Perceptron output should range from 0 to 1 inclusive'
def test_output_range_of_mlp_classifier(self):
"""
Test to check the output range of the Multi-layer Perceptron Classifier.
"""
mlp = MLPClassifier(random_state=1, early_stopping=True)
mlp.fit(self.X_train, self.y_train)
pred_train = mlp.predict(self.X_train)
pred_test = mlp.predict(self.X_test)
# =================================
# TEST SUITE
# =================================
assert (pred_train <= 1).all() & (pred_train >= 0).all(), \
'Multi-layer Perceptron output should range from 0 to 1 inclusive'
assert (pred_test <= 1).all() & (pred_test >= 0).all(), \
'Multi-layer Perceptron output should range from 0 to 1 inclusive'
def test_model_returns_correct_type_object(self):
"""
Test for the return of the correct object of the modeling function.
"""
cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=3, random_state=1)
scores = cross_validate(RandomForestClassifier(), self.X_train, self.y_train,
scoring=('accuracy', 'f1_weighted'),
cv=cv, n_jobs=-1, return_train_score=True)
# =================================
# TEST SUITE
# =================================
# Check the return object type
assert isinstance(scores, dict)
# Check the length of the returned object
assert len(scores) == 6
# Check the correctness of the names of the returned dict keys
assert 'test_accuracy' in scores and 'test_f1_weighted' in scores
assert 'train_accuracy' in scores and 'train_f1_weighted' in scores
def test_model_returns_correct_values(self):
"""
Tests for the returned values of the modeling function.
"""
cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=3, random_state=1)
scores = cross_validate(RandomForestClassifier(), self.X_train, self.y_train,
scoring=('accuracy', 'f1_weighted'),
cv=cv, n_jobs=-1, return_train_score=True)
# =================================
# TEST SUITE
# =================================
# Check returned scores' type
print(type(scores['train_accuracy']))
assert isinstance(scores['train_accuracy'], np.ndarray)
assert isinstance(scores['test_accuracy'], np.ndarray)
assert isinstance(scores['train_f1_weighted'], np.ndarray)
assert isinstance(scores['test_f1_weighted'], np.ndarray)
# Check returned scores' range
assert scores['train_accuracy'].mean() >= 0.0
assert scores['test_accuracy'].mean() <= 1.0
assert scores['train_f1_weighted'].mean() >= 0.0
assert scores['test_f1_weighted'].mean() <= 1.0
def test_wrong_input_raises_assertion(self):
"""
Tests for various assertion cheks written in the modeling function.
"""
cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=3, random_state=1)
scores = cross_validate(RandomForestClassifier(), self.X_train, self.y_train,
scoring=('accuracy', 'f1_weighted'),
cv=cv, n_jobs=-1, return_train_score=True)
# =================================
# TEST SUITE
# =================================
# Test that it handles the case of: X is a string
with self.assertRaises(TypeError) as exception:
msg = preprocess_dataset('X')
assert "TypeError: string indices must be integers" in str(exception.value)
msg = batch_prediction(self.X_train, self.y_train, self.X_test, self.y_test, None)
assert "ValueError" in str(exception.value)
if __name__ == '__main__':
unittest.main()