[39d39d]: / py_version / models_ml.py

Download this file

179 lines (143 with data), 5.9 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
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sn
# Machine Learning libraries
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
# Model evaluation libraries
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
### Random Forest Classfier
rf = RandomForestClassifier()
### Support Vector Classifier
svc = SVC()
### Logistic Regression
lr = LogisticRegression(solver='liblinear')
### K Nearest Neighbors
knn = KNeighborsClassifier()
x_data = np.load('featurized_data.npy', allow_pickle = True)
y_data = np.load('labels.npy', allow_pickle = True)
if __name__ == "__main__":
rf_f_scores = cross_val_score(rf, x_data, y_data, cv=5)
rf_f_acc = np.mean(rf_f_scores)
svc_f_scores = cross_val_score(svc, x_data, y_data, cv=5)
svc_f_acc = np.mean(svc_f_scores)
lr_f_scores = cross_val_score(lr, x_data, y_data, cv=5)
lr_f_acc = np.mean(lr_f_scores)
knn_f_scores = cross_val_score(knn, x_data, y_data, cv=5)
knn_f_acc = np.mean(knn_f_scores)
# Visualize performance
data_r = {'RF':rf_f_acc, 'SVC':svc_f_acc, 'LR':lr_f_acc, 'kNN':knn_f_acc}
algorithm = list(data_r.keys())
accuracy = list(data_r.values())
fig = plt.figure(figsize = (10, 5))
plt.bar(algorithm, accuracy, color ='red', width = 0.4)
plt.xlabel("ML models", fontsize = 18)
plt.ylabel("5 fold accuracy", fontsize = 18)
plt.title("Result", fontsize = 18)
plt.xticks(fontsize = 14)
plt.yticks(fontsize = 14)
plt.ylim([0, 1])
plt.show()
print('Random Forest Accuracy: ', rf_f_acc*100)
print('Support Vector Classifier Accuracy: ', svc_f_acc*100)
print('Logistic Regression Accuracy: ', lr_f_acc*100)
print('K Nearest Neighbours Accuracy: ', knn_f_acc*100)
### Retraining RF on shuffeled data
X_train = []
X_test = []
y_train = []
y_test = []
for i in range(7):
current_class_data = x_data[i*20: i*20 + 20]
X_train.append(current_class_data[0: 16])
X_test.append(current_class_data[16: ])
current_class_labels = y_data[i*20: i*20 + 20]
y_train.append(current_class_labels[0: 16])
y_test.append(current_class_labels[16: ])
X_train = np.array(X_train).reshape(-1, 320)
X_test = np.array(X_test).reshape(-1, 320)
y_train = np.array(y_train).reshape(-1)
y_test = np.array(y_test).reshape(-1)
rf = RandomForestClassifier()
rf.fit(X_train, y_train)
predictions = rf.predict(X_test)
accuracy = accuracy_score(predictions, y_test)
print('Accuracy: ', accuracy)
# Confusion Matrix
conf_matrix = confusion_matrix(y_test, predictions)
df_cm = pd.DataFrame(conf_matrix, index = [i for i in "0123456"], columns = [i for i in "0123456"])
plt.figure(figsize = (10,7))
sn.set(font_scale=1.4)
sn.heatmap(df_cm, annot=True, annot_kws={"size": 16})
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()
# Dropping class 4 Datapoints
idx = (y_data != 4)
x_data = x_data[idx]
y_data = np.array([i for i in range(6) for j in range(20)])
#Retrain shallow ML algorithms without class 4
rf = RandomForestClassifier()
rf_f_scores = cross_val_score(rf, x_data, y_data, cv=5)
rf_f_acc = np.mean(rf_f_scores)
svc = SVC()
svc_f_scores = cross_val_score(svc, x_data, y_data, cv=5)
svc_f_acc = np.mean(svc_f_scores)
lr = LogisticRegression(solver='liblinear')
lr_f_scores = cross_val_score(lr, x_data, y_data, cv=5)
lr_f_acc = np.mean(lr_f_scores)
knn = KNeighborsClassifier()
knn_f_scores = cross_val_score(knn, x_data, y_data, cv=5)
knn_f_acc = np.mean(knn_f_scores)
data_r = {'RF':rf_f_acc, 'SVC':svc_f_acc, 'LR':lr_f_acc, 'kNN':knn_f_acc}
algorithm = list(data_r.keys())
accuracy = list(data_r.values())
fig = plt.figure(figsize = (10, 5))
plt.bar(algorithm, accuracy, color ='red', width = 0.4)
plt.xlabel("ML models", fontsize = 18)
plt.ylabel("5 fold accuracy", fontsize = 18)
plt.title("Result", fontsize = 18)
plt.xticks(fontsize = 14)
plt.yticks(fontsize = 14)
plt.ylim([0, 1])
plt.show()
print('Random Forest Accuracy: ', rf_f_acc*100)
print('Support Vector Classifier Accuracy: ', svc_f_acc*100)
print('Logistic Regression Accuracy: ', lr_f_acc*100)
print('K Nearest Neighbours Accuracy: ', knn_f_acc*100)
# Creating train and test set without class 4
X_train = []
X_test = []
y_train = []
y_test = []
for i in range(6):
current_class_data = x_data[i*20: i*20 + 20]
X_train.append(current_class_data[0: 16])
X_test.append(current_class_data[16: ])
current_class_labels = y_data[i*20: i*20 + 20]
y_train.append(current_class_labels[0: 16])
y_test.append(current_class_labels[16: ])
X_train = np.array(X_train).reshape(-1, 320)
X_test = np.array(X_test).reshape(-1, 320)
y_train = np.array(y_train).reshape(-1)
y_test = np.array(y_test).reshape(-1)
# Training the best model (Random Forest)
rf = RandomForestClassifier()
rf.fit(X_train, y_train)
predictions = rf.predict(X_test)
accuracy = accuracy_score(predictions, y_test)
print('Accuracy: ', accuracy)
# See new confusion matrix of best model without class 4
conf_matrix = confusion_matrix(y_test, predictions)
df_cm = pd.DataFrame(conf_matrix, index = [i for i in "012356"], columns = [i for i in "012356"])
plt.figure(figsize = (10,7))
sn.set(font_scale=1.4)
sn.heatmap(df_cm, annot=True, annot_kws={"size": 16})
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()