This repository was archived by the owner on Feb 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassification.py
More file actions
114 lines (102 loc) · 5.11 KB
/
Copy pathclassification.py
File metadata and controls
114 lines (102 loc) · 5.11 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
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from custom_naive_bayes import CustomNaiveBayes
from custom_ensemble import Custom_Ensemble
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import confusion_matrix
import numpy as np
# Defining tuples for classifiers' options
classifier_tuple = ('Decision tree', 'K nearest neighbor', 'Support Vector Classifier', 'Custom Naive Bayes', 'Custom ensemble')
weights_tuple = ('Uniform', 'Distance')
distance_tuple = ('Euclidean', 'Manhattan', 'Cosine', 'Pearson correlation')
purity_tuple = ('Gini', 'Entropy', 'LogLoss')
kernel_tuple = ('RBF', 'Polinomial')
def init_classification(classifier_str, gui_params):
"Initializes the classifier with tuned hyperparameters"
params = {}
# Decision Tree
if classifier_str == classifier_tuple[0]:
classifier = DecisionTreeClassifier()
if gui_params['tuning']: # Real time tuning
params['criterion'] = ('gini', 'entropy', 'log_loss')
params['max_depth'] = [None] + list(range(2, 8))
params['min_samples_leaf'] = tuple(range(1, 5))
params['min_samples_split'] = tuple(range(2, 5))
else: # Selecting purity metric
if gui_params['option1'] == purity_tuple[0]:
params['criterion'] = 'gini'
elif gui_params['option1'] == purity_tuple[1]:
params['criterion'] = 'entropy'
elif gui_params['option1'] == purity_tuple[2]:
params['criterion'] = 'log_loss'
params['max_depth'] = None # Imputing tuned hyperparameters
params['min_samples_leaf'] = 3
params['min_samples_split'] = 2
# K-Nearest Neighbour
elif classifier_str == classifier_tuple[1]:
classifier = KNeighborsClassifier()
if gui_params['tuning']: # Real time tuning
params['n_neighbors'] = tuple(range(1, 20))
params['weights'] = ('uniform','distance')
params['metric'] = ('euclidean', 'manhattan', 'cosine', 'correlation')
else: # Selecting distance metric
if gui_params['option1'] == distance_tuple[0]:
params['metric'] = 'euclidean'
elif gui_params['option1'] == distance_tuple[1]:
params['metric'] = 'manhattan'
elif gui_params['option1'] == distance_tuple[2]:
params['metric'] = 'cosine'
elif gui_params['option1'] == distance_tuple[3]:
params['metric'] = 'correlation'
if gui_params['option2'] == 0:
params['weights'] = 'uniform'
elif gui_params['option2'] == 1:
params['weights'] = 'distance'
params['n_neighbors'] = 2 # Imputing tuned number of neighbours
# Support Vector Machine
elif classifier_str == classifier_tuple[2]:
classifier = SVC(probability=True)
if gui_params['tuning']: # Real time tuning
params['kernel'] = ['rbf', 'poly']
params['C'] = [float(x)/10 for x in range(15, 25)]
params['gamma'] = [float(x)/10 for x in range(10, 20)]
else: # Selecting kernel
if gui_params['option1'] == kernel_tuple[0]:
params['kernel'] = 'rbf'
elif gui_params['option1'] == kernel_tuple[1]:
params['kernel'] = 'poly'
params['C'] = 1.7 # Imputing tuned hyperparameters
params['gamma'] = 1.4
# Custom Naive Bayes
elif classifier_str == classifier_tuple[3]:
classifier = CustomNaiveBayes()
# Custom Ensemble
elif classifier_str == classifier_tuple[4]:
params['voting'] = gui_params['voting'] # Imputing Voting policy
params['weights'] = gui_params['weights'] # Imputing Weights
params['algorithm'] = gui_params['algorithm'] # Imputing ensemble training algorithm
classifier = Custom_Ensemble()
return classifier, params
def tuning(classifier, params, train_x, train_y):
"Tuning of the hyperparameters of a classifier"
tuner = GridSearchCV(classifier, params, cv=5, n_jobs=-1) # Defining object to tune the hyperparameters
tuner.fit(train_x, train_y) # Fitting the tuner
print(tuner.best_params_) # Output: Optimal hyperparameters
return tuner.best_params_
def compute_performances(test_y, pred_y):
"Computes the performances of a classifier"
cm = confusion_matrix(test_y, pred_y) # Computing the confusion matrix
eps = np.finfo(float).eps
TP = cm[1,1]
TN = cm[0,0]
FP = cm[0,1]
FN = cm[1,0]
acc = (TP + TN) / (TP + TN + FP + FN + eps)
TPR = TP / (TP + FN + eps)
TNR = TN / (TN + FP + eps)
FPR = FP / (TN + FP + eps)
FNR = FN / (TP + FN + eps)
p = TP / (TP + FP + eps)
F1 = 2*TPR*p / (TPR+p + eps)
return round(acc, 6), round(TPR, 6), round(TNR, 6), round(FPR, 6), round(FNR, 6), round(p, 6), round(F1, 6) # Output: Evaluation metrics of the classifier