-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathclassification-with-confidence.py
More file actions
182 lines (144 loc) · 8.51 KB
/
Copy pathclassification-with-confidence.py
File metadata and controls
182 lines (144 loc) · 8.51 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
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
# Clear any created variables
#%reset -f
import os
import sys
import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split, StratifiedKFold
class ResultData:
def __init__(self, related_classes, count_of_classes_per_instance, hist_of_instances, acc, acc_no_conf, acc_conf):
self._related_classess = related_classes
self._count_of_classes_per_instance = count_of_classes_per_instance
self._hist_of_instances = hist_of_instances
self._acc = acc
self._acc_no_conf = acc_no_conf
self._acc_conf = acc_conf
def calculate_accuracy(classifier, x, y):
confidence = 0.85
probabilities = classifier.predict_proba(x)
# Accuracy variables
cnt_all = 0.
cnt_suggest_first_two_scenarios = 0.
cnt_with_confidence = 0.
# Hist of non zero probabilities per class
counts = np.count_nonzero(probabilities, axis = 1)
unique_counts = np.unique(counts)
n = len(unique_counts)
hist = [0]*n
for i in range(n):
hist[i] = np.count_nonzero(counts == unique_counts[i])
# Confusion Matrix (used to check related misclassified scenarios)
classes = classifier.classes_
n2 = len(classes)
related_classes = np.zeros([n2, n2 + 2]) # (1 for all misclassified count and the other for the misclassified and correctly classified by second probability count)
# Calculate accuracy and build confusion matrix
for i in range(len(probabilities)):
indecies = np.argsort(-probabilities[i])
# Report first scenario
if classes[indecies[0]] == y[i]:
cnt_all += 1
# Report first and second probable scenarios
if classes[indecies[0]] == y[i] or (probabilities[i, indecies[1]] > 0 and classes[indecies[1]] == y[i]):
cnt_suggest_first_two_scenarios += 1
# Report second probable scenario only if > confidence
if (probabilities[i,indecies[0]] >= confidence and classes[indecies[0]] == y[i]) or (probabilities[i,indecies[0]] < confidence and (classes[indecies[0]] == y[i] or (probabilities[i,indecies[1]] > 0 and classes[indecies[1]] == y[i]))):
cnt_with_confidence +=1
index_of_y = classes.tolist().index(y[i])
if classes[indecies[0]] != y[i]:
related_classes[index_of_y, n2] += 1
related_classes[index_of_y, indecies[0]] += 1
if classes[indecies[0]] != y[i] and probabilities[i, indecies[1]] > 0 and classes[indecies[1]] == y[i]:
related_classes[index_of_y, n2 + 1] += 1
res_data = ResultData(related_classes, unique_counts, hist, cnt_all/len(probabilities), cnt_suggest_first_two_scenarios/len(probabilities), cnt_with_confidence/len(probabilities))
return res_data
def write_data_to_file(data, fileName):
with open(fileName, 'a') as cv_file:
cv_file.write('Number of predicted classes per instance\n')
np.savetxt(cv_file, data._count_of_classes_per_instance, delimiter=',', fmt='%1.3f')
cv_file.write('Number of instances hist (count of instances in each number of predicted classes per instances)\n')
np.savetxt(cv_file, data._hist_of_instances, delimiter=',', fmt='%1.3f')
cv_file.write('Related Classes\n')
np.savetxt(cv_file, data._related_classess, delimiter=',', fmt='%1.3f')
def main(data_file_path):
random_state = 0
dataset = pd.read_csv(data_file_path)
dataset = dataset.dropna()
X = dataset.iloc[:, 0: 10].values
y = dataset.iloc[:, 10: 16].values
#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = test_size, random_state = random_state)
kfold = StratifiedKFold(n_splits = 5, shuffle = True, random_state = 0)
counter = 0
for train, test in kfold.split(X, y[:, 2]):
X_train = X[train]
y_train = y[train, 2]
X_test = X[test]
y_test = y[test, 2]
# Normalization
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)
# index_of_y_to_classify = 2
folder_name = '3-scenario/fold_{}/'.format(counter)
# Begin Classification
y_current_train = y_train
y_current_test = y_test
# 1- linear regression
linear_classifier = LogisticRegression(random_state = random_state)
linear_classifier.fit(X_train, y_current_train)
accuracies_linear = calculate_accuracy(linear_classifier, X_test, y_current_test)
# 2- KNN
knn_classifier = KNeighborsClassifier()
knn_classifier.fit(X_train, y_current_train)
accuracies_KNN = calculate_accuracy(knn_classifier, X_test, y_current_test)
# 3- SVM
svm_classifier = SVC(kernel = 'linear', random_state = random_state, probability=True)
svm_classifier.fit(X_train, y_current_train)
accuracies_svm = calculate_accuracy(svm_classifier, X_test, y_current_test)
#4- Kernel SVM
kernel_svm_classifier = SVC(kernel = 'rbf', random_state = random_state, probability=True)
kernel_svm_classifier.fit(X_train, y_current_train)
accuracies_kernel_svm = calculate_accuracy(kernel_svm_classifier, X_test, y_current_test)
#5- Naive Bayes
naive_classifier = GaussianNB()
naive_classifier.fit(X_train, y_current_train)
accuracies_naive = calculate_accuracy(naive_classifier, X_test, y_current_test)
#6- Decision Tree
decision_tree_classifier = DecisionTreeClassifier(criterion = 'entropy', random_state = random_state)
decision_tree_classifier.fit(X_train, y_current_train)
accuracies_decision_tree = calculate_accuracy(decision_tree_classifier, X_test, y_current_test)
#7- Random Forest
random_forest_classifier = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = random_state)
random_forest_classifier.fit(X_train, y_current_train)
accuracies_random_forest = calculate_accuracy(random_forest_classifier, X_test, y_current_test)
if os.path.isdir(folder_name) == False:
os.mkdir(folder_name)
with open(folder_name + 'Results.csv', 'a') as cv_file:
cv_file.write('Algorithm, One Scenario, No Confidence, Confidence')
cv_file.write('\nLinear , ' + str(accuracies_linear._acc) + "," + str(accuracies_linear._acc_no_conf) + "," +str(accuracies_linear._acc_conf))
cv_file.write('\nNaive , ' + str(accuracies_naive._acc) + "," + str(accuracies_naive._acc_no_conf) + "," +str(accuracies_naive._acc_conf))
cv_file.write('\nKNN , ' + str(accuracies_KNN._acc) + "," + str(accuracies_KNN._acc_no_conf) + "," +str(accuracies_KNN._acc_conf))
cv_file.write('\nSVM , ' + str(accuracies_svm._acc) + "," + str(accuracies_svm._acc_no_conf) + "," +str(accuracies_svm._acc_conf))
cv_file.write('\nKern SVM , ' + str(accuracies_kernel_svm._acc) + "," + str(accuracies_kernel_svm._acc_no_conf) + "," +str(accuracies_kernel_svm._acc_conf))
cv_file.write('\nD Trees , ' + str(accuracies_decision_tree._acc) + "," + str(accuracies_decision_tree._acc_no_conf) + "," +str(accuracies_decision_tree._acc_conf))
cv_file.write('\nRand For , ' + str(accuracies_random_forest._acc) + "," + str(accuracies_random_forest._acc_no_conf) + "," +str(accuracies_random_forest._acc_conf))
write_data_to_file(accuracies_linear, folder_name + 'Linear.csv')
write_data_to_file(accuracies_naive, folder_name + 'Naive.csv')
write_data_to_file(accuracies_KNN, folder_name + 'KNN.csv')
write_data_to_file(accuracies_svm, folder_name + 'SVM.csv')
write_data_to_file(accuracies_kernel_svm, folder_name + 'KernelSVM.csv')
write_data_to_file(accuracies_decision_tree, folder_name + 'DT.csv')
write_data_to_file(accuracies_random_forest, folder_name + 'Rand Forest.csv')
counter += 1
if __name__ == "__main__":
if len(sys.argv) > 1:
path = sys.argv[1]
else:
path = os.getcwd() + '/dataset_processed.csv'
main(path)