-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassificationModels.py
159 lines (107 loc) · 4.6 KB
/
ClassificationModels.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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('ggplot')
from sklearn.model_selection import train_test_split, StratifiedKFold
from sklearn.metrics import confusion_matrix
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import roc_curve, auc
import matplotlib as mpl
import matplotlib.pyplot as plt
import sklearn.metrics
from numpy import mean
from numpy import std
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.dummy import DummyClassifier
from matplotlib import pyplot
from sklearn.model_selection import KFold
from sklearn.datasets import make_classification
from sklearn.model_selection import StratifiedKFold
def classificationModelsFun(x_train, x_test, y_train, y_test):
#Train each model and generate score. Use one at a time and comment lines of other models
#RandomForest Classifier
clf = RandomForestClassifier(n_estimators = 200)
clf.fit(x_train, y_train)
y_pred = rf.predict(x_test)
#Decision Tree Classifier
decision_tree = DecisionTreeClassifier(max_leaf_nodes=63)
decision_tree.fit(x_train, y_train)
y_pred = decision_tree.predict(x_test)
#SVM
clf = SVC(kernel='linear')
clf.fit(x_train, y_train)
y_pred = clf.predict(x_test)
confusion_matrix(y_test,y_pred)
pd.crosstab(y_test, y_pred, rownames=['True'], colnames=['Predicted'], margins=True)
#Guassian Naive Bayes Classifier
model = GaussianNB()
model.fit(x_train, y_train)
y_pred = model.predict(x_test)
confusion_matrix(y_test,y_pred)
pd.crosstab(y_test, y_pred, rownames=['True'], colnames=['Predicted'], margins=True)
#kNeartest Neighbors Classifier
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(x_train,y_train)
knn.score(x_test,y_test)
y_pred = knn.predict(x_test)
confusion_matrix(y_test,y_pred)
pd.crosstab(y_test, y_pred, rownames=['True'], colnames=['Predicted'], margins=True)
#Score on test data set for one classifier
acc = accuracy_score(y_test, y_pred, normalize=True)
print('Accuracy: %.3f' % acc)
precision = precision_score(y_test, y_pred, average='binary')
print('Precision: %.3f' % precision)
precision = precision_score(y_test, y_pred, average='micro')
print('micro Precision: %.3f' % precision)
precision = precision_score(y_test, y_pred, average='macro')
print('macro Precision: %.3f' % precision)
recall = recall_score(y_test, y_pred, average='binary')
print('Recall: %.3f' % recall)
recall = recall_score(y_test, y_pred, average='micro')
print('micro Recall: %.3f' % recall)
recall = recall_score(y_test, y_pred, average='macro')
print('macro Recall: %.3f' % recall)
score = f1_score(y_test, y_pred, average='binary')
print('F-Measure: %.3f' % score)
score = f1_score(y_test, y_pred, average='micro')
print('micro F-Measure: %.3f' % score)
score = f1_score(y_test, y_pred, average='macro')
print('macro F-Measure: %.3f' % score)
def main():
feature_cols = []
for col in df.columns:
if col != "from" and col != "event" and col != " status" and col != "from-time" and col != "from-pub-uri" and col != "Country" and col != "class" and col != "Class" and col != "status" and col != "1-Cosine":
feature_cols.append(col)
target_var = "class"
x = df[feature_cols].values
y = df[target_var].values
#Do not encode for geographical and economical barrier but for all others
labelencoder_X = LabelEncoder()
for i in range(len(feature_cols)):
x[:,i] = labelencoder_X.fit_transform(x[:,i])
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=1)
for train_ix, test_ix in kfold.split(x, y):
x_train, x_test = x[train_ix], x[test_ix]
y_train, y_test = y[train_ix], y[test_ix]
classificationModelsFun(x_train, x_test, y_train, y_test)
if __name__ == '__main__':
main()
# In[ ]:
# In[ ]:
feature_cols = []
for col in df.columns:
if col != "from" and col != "event" and col != " status" and col != "from-time" and col != "from-pub-uri" and col != "Country" and col != "class" and col != "Class" and col != "status" and col != "1-Cosine":
feature_cols.append(col)
target_var = "class"
# In[ ]:
# In[ ]: