-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKNN-classification-workspace.py
80 lines (71 loc) · 2.49 KB
/
KNN-classification-workspace.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
#Enable importing code from parent directory
import os, sys
p = os.path.abspath('..')
sys.path.insert(1, p)
from trainmodels import crossValidationFunctionGenerator
from loaddata import loadData, trainTestSplit, extractZeroOneClasses, convertZeroOne
from finalAlgoImplementation import final_HTVTC
import regressionmetrics
import classificationmetrics
quantity = 'EXEC-TIME'
task = 'classification'
data = loadData(source='sklearn', identifier='wine', task=task)
binary_data = extractZeroOneClasses(data)
data_split = trainTestSplit(binary_data, method = 'cross_validation')
func = crossValidationFunctionGenerator(data_split, algorithm='knn-classification', task=task)
metric = classificationmetrics.indicatorFunction
#Start timer/memory profiler/CPU timer
a = None
start_time = None
if quantity == 'EXEC-TIME':
import time
start_time = time.perf_counter_ns()
elif quantity == 'CPU-TIME':
import time
start_time = time.process_time_ns()
elif quantity == 'MAX-MEMORY':
import tracemalloc
tracemalloc.start()
ranges_dict = {
'N': {
'type': 'INTEGER',
'start': 1.0,
'end': 100.0,
'interval': 10.0,
},
'weightingFunction': {
'type': 'CATEGORICAL',
'values': ['uniform', 'distance'],
},
'distanceFunction': {
'type': 'CATEGORICAL',
'values': ['minkowski']
},
'p': {
'type': 'INTEGER',
'start': 1.0,
'end': 100.0,
'interval': 10.0,
}
}
recommended_combination, history = final_HTVTC(eval_func=func, ranges_dict=ranges_dict, metric=metric, max_completion_cycles=5, max_size_gridsearch=51)
#End timer/memory profiler/CPU timer
result = None
if quantity == 'EXEC-TIME':
end_time = time.perf_counter_ns()
result = end_time - start_time
elif quantity == 'CPU-TIME':
end_time = time.process_time_ns()
result = end_time - start_time
elif quantity == 'MAX-MEMORY':
_, result = tracemalloc.get_traced_memory()
tracemalloc.stop()
#Recreate cross-validation generator
data_split = trainTestSplit(binary_data, method = 'cross_validation')
#Find the true loss for the selcted combination
truefunc = crossValidationFunctionGenerator(data_split, algorithm='knn-classification', task=task)
true_value = truefunc(metric=metric, **recommended_combination)
print(f'hyperparameters: {recommended_combination}')
print(f'history: {history}')
print(f'True value: {true_value}')
print(f'{quantity}: {result}')