-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerateerrortensor_test.py
289 lines (260 loc) · 10.1 KB
/
generateerrortensor_test.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
import unittest
from generateerrortensor import generateIncompleteErrorTensor
from trainmodels import evaluationFunctionGenerator
from loaddata import loadData, trainTestSplit, extractZeroOneClasses, convertZeroOne
import regressionmetrics
import classificationmetrics
import numpy as np
class TestGenerateErrorTensor(unittest.TestCase):
# Using mock evaluation function
def test_mock_example(self):
eval_func = lambda metric, alpha=0, beta=0, **kwargs : alpha + beta
ranges_dict = {
'alpha': {
'start': 1,
'end': 5,
'interval': 0.1,
},
'beta': {
'start': 0,
'end': 10,
'interval': 1,
}
}
tensor, indices = generateIncompleteErrorTensor(eval_func, ranges_dict, 0.4, metric=regressionmetrics.mse)
#Test tensor
self.assertTrue(np.shape(tensor) == (41, 11))
self.assertTrue(np.count_nonzero(tensor) == 180)
#Test indices
self.assertTrue(len(indices) == 180)
maxvalues = list(map(max, indices))
self.assertTrue(max(maxvalues) <= 40)
minvalues = list(map(min, indices))
self.assertTrue(min(minvalues) >= 0)
# Specify known fraction as 0.0
def test_allzero(self):
eval_func = lambda metric, alpha=0, beta=0, gamma=0, **kwargs : (alpha + beta)*gamma
ranges_dict = {
'alpha': {
'start': 1,
'end': 5,
'interval': 0.1,
},
'beta': {
'start': 0,
'end': 10,
'interval': 1,
},
'gamma': {
'start': 1,
'end': 2.2,
'interval': 0.1,
},
}
tensor, indices = generateIncompleteErrorTensor(eval_func, ranges_dict, 0.0, metric=classificationmetrics.hingeLoss)
#Test tensor
self.assertTrue(np.shape(tensor) == (41, 11, 13))
self.assertTrue(np.allclose(tensor, np.zeros((41,11,13))))
#Test indices
self.assertTrue(len(indices) == 0)
# Using real data------------------------------------------------------------------------------
# ridge regression
def test_ridge_regression(self):
task = 'regression'
data = loadData(source='sklearn', identifier='california_housing', task=task)
data_split = trainTestSplit(data)
func = evaluationFunctionGenerator(data_split, algorithm='ridge-regression', task=task)
ranges_dict = {
'alpha': {
'start': 0,
'end': 3,
'interval': 0.05,
},
}
tensor, indices = generateIncompleteErrorTensor(func, ranges_dict, 0.3, metric=regressionmetrics.mape)
#Test tensor
self.assertTrue(np.shape(tensor) == (61,))
self.assertTrue(np.count_nonzero(tensor) == 18)
#Test indices
self.assertTrue(len(indices) == 18)
maxvalues = list(map(max, indices))
self.assertTrue(max(maxvalues) <= 60)
minvalues = list(map(min, indices))
self.assertTrue(min(minvalues) >= 0)
# svm-rbf
def test_svm_rbf(self):
task = 'classification'
data = loadData(source='sklearn', identifier='breast_cancer', task=task)
data_split = trainTestSplit(data)
func = evaluationFunctionGenerator(data_split, algorithm='svm-rbf', task=task)
ranges_dict = {
'C': {
'start': 0.1,
'end': 5.0,
'interval': 0.1,
},
'gamma': {
'start': 0.1,
'end': 1.0,
'interval': 0.1,
}
}
tensor, indices = generateIncompleteErrorTensor(func, ranges_dict, 0.2, metric=classificationmetrics.indicatorFunction)
#Test tensor
self.assertTrue(np.shape(tensor) == (50,10))
self.assertTrue(np.count_nonzero(tensor) == 100)
#Test indices
self.assertTrue(len(indices) == 100)
maxvalues = list(map(max, indices))
self.assertTrue(max(maxvalues) <= 49)
minvalues = list(map(min, indices))
self.assertTrue(min(minvalues) >= 0)
# svm-polynomial
def test_svm_polynomial(self):
task = 'classification'
data = loadData(source='sklearn', identifier='iris', task=task)
binary_data = extractZeroOneClasses(data)
adjusted_data = convertZeroOne(binary_data, -1, 1)
data_split = trainTestSplit(adjusted_data)
func = evaluationFunctionGenerator(data_split, algorithm='svm-polynomial', task=task)
ranges_dict = {
'C': {
'start': 0.1,
'end': 3.0,
'interval': 0.1,
},
'gamma': {
'start': 0.1,
'end': 3.0,
'interval': 0.1,
},
'constant_term': {
'start': 0.0,
'end': 3.0,
'interval': 0.1,
},
'degree': {
'start': 0.0,
'end': 3.0,
'interval': 1.0,
}
}
tensor, indices = generateIncompleteErrorTensor(func, ranges_dict, 0.1, metric=classificationmetrics.hingeLoss, eval_trials=2, evaluation_mode='raw-score')
#Test tensor
self.assertTrue(np.shape(tensor) == (30,30,31,4))
self.assertTrue(np.count_nonzero(tensor) <= 31*12*30)
#Test indices
self.assertTrue(len(indices) == 31*12*30)
maxvalues = list(map(max, indices))
self.assertTrue(max(maxvalues) <= 30)
minvalues = list(map(min, indices))
self.assertTrue(min(minvalues) >= 0)
# KNN-regression
def test_KNN_regression(self):
task = 'regression'
data = loadData(source='sklearn', identifier='diabetes', task=task)
data_split = trainTestSplit(data)
func = evaluationFunctionGenerator(data_split, algorithm='knn-regression', task=task)
ranges_dict = {
'N': {
'start': 1.0,
'end': 20.0,
'interval': 1.0,
},
'weightingFunction': {
'values': ['uniform', 'distance'],
},
'distanceFunction': {
'values': ['minkowski']
},
'p': {
'start': 1.0,
'end': 10.0,
'interval': 1.0,
}
}
tensor, indices = generateIncompleteErrorTensor(func, ranges_dict, 0.5, metric=regressionmetrics.logcosh, eval_trials=10)
#Test tensor
self.assertTrue(np.shape(tensor) == (20,2,1,10))
self.assertTrue(np.count_nonzero(tensor) == 200)
#Test indices
self.assertTrue(len(indices) == 200)
maxvalues = list(map(max, indices))
self.assertTrue(max(maxvalues) <= 19)
minvalues = list(map(min, indices))
self.assertTrue(min(minvalues) >= 0)
# KNN-classification
def test_KNN_classification(self):
task = 'classification'
data = loadData(source='sklearn', identifier='wine', task=task)
binary_data = extractZeroOneClasses(data)
data_split = trainTestSplit(binary_data)
func = evaluationFunctionGenerator(data_split, algorithm='knn-classification', task=task)
ranges_dict = {
'N': {
'start': 1.0,
'end': 20.0,
'interval': 1.0,
},
'weightingFunction': {
'values': ['uniform', 'distance'],
},
'distanceFunction': {
'values': ['minkowski']
},
'p': {
'start': 1.0,
'end': 10.0,
'interval': 0.1,
}
}
tensor, indices = generateIncompleteErrorTensor(func, ranges_dict, 0.3, metric=classificationmetrics.indicatorFunction, eval_trials=5)
#Test tensor
self.assertTrue(np.shape(tensor) == (20,2,1,91))
self.assertTrue(np.count_nonzero(tensor) <= 1092)
#Test indices
self.assertTrue(len(indices) == 1092)
maxvalues = list(map(max, indices))
self.assertTrue(max(maxvalues) <= 90)
minvalues = list(map(min, indices))
self.assertTrue(min(minvalues) >= 0)
# Random forest
def test_random_forest(self):
task = 'classification'
data = loadData(source='sklearn', identifier='wine', task=task)
binary_data = extractZeroOneClasses(data)
data_split = trainTestSplit(binary_data)
func = evaluationFunctionGenerator(data_split, algorithm='random-forest', task=task)
ranges_dict = {
'no_trees': {
'values':[1,10,20,30,40]
},
'max_tree_depth': {
'values':[1, 5, 10, 15, 20]
},
'bootstrap': {
'values': [True, False]
},
'min_samples_split': {
'start': 2.0,
'end': 10.0,
'interval': 1.0,
},
'no_features': {
'start': 1.0,
'end': 10.0,
'interval': 1.0,
},
}
tensor, indices = generateIncompleteErrorTensor(func, ranges_dict, 0.4, metric=classificationmetrics.KullbackLeiblerDivergence, evaluation_mode='probability')
#Test tensor
self.assertTrue(np.shape(tensor) == (5,5,2,9,10))
self.assertTrue(np.count_nonzero(tensor) <= 5*5*2*9*10*0.4)
#Test indices
self.assertTrue(len(indices) == 5*5*2*9*10*0.4)
maxvalues = list(map(max, indices))
self.assertTrue(max(maxvalues) <= 9)
minvalues = list(map(min, indices))
self.assertTrue(min(minvalues) >= 0)
if __name__ == '__main__':
unittest.main()