-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall_classifier_for github.py
191 lines (174 loc) · 8.43 KB
/
all_classifier_for github.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
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
183
184
185
186
187
188
189
190
191
#Importing Libraries
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
#from sklearn.preprocessing import StandardScaler
# import matplotlib.pyplot as plt
from sklearn.externals import joblib
from sklearn import tree #do sth about this
from sklearn.model_selection import cross_val_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier, AdaBoostClassifier, VotingClassifier
from sklearn.linear_model import LogisticRegression, LogisticRegressionCV, RidgeClassifier
from sklearn.svm import SVC
from sklearn.neural_network import MLPClassifier
from sklearn.naive_bayes import GaussianNB
print('Libraries Imported')
# Reading the dataset, cleaning
dataset = pd.read_csv(r'C:\\Modelling\final_trng_set.csv')
dataset.head()
dataset.fillna(0, inplace=True)
features =dataset.drop('Classification', axis = 1)
feature_list = list(features.columns)
#Creating the dependent variable class
factor = pd.factorize(dataset['Classification'])
dataset.Classification = factor[0]
definitions = factor[1]
#Splitting the data into independent and dependent variables
X = dataset.iloc[:,12:].values
# X = dataset.iloc[:,11:].values # Includes number of pages scraped
y = dataset.iloc[:,10].values
# Creating the Training and Test set from data - Validation set approach, 80/20
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20, random_state = 21)
def Gradient_Boost(train_X, test_X, train_y, test_y):
params = {'n_estimators' : 1800, 'learning_rate' : 0.05}
clf = GradientBoostingClassifier(**params).fit(train_X, train_y)
reversefactor = dict(zip(range(4),definitions))
predicted_y = clf.predict(test_X)
predicted_y = np.vectorize(reversefactor.get)(predicted_y)
correct_y = np.vectorize(reversefactor.get)(test_y)
cm = confusion_matrix(correct_y, predicted_y)
print('')
print("THIS IS THE RESULT FOR GRADIENT BOOST")
print(cm)
acc = accuracy_score(correct_y, predicted_y)
print("Accuracy of training data is " + str(acc))
return acc
def Ada_Boost(train_X, test_X, train_y, test_y):
params = {'n_estimators' : 1000}
clf = AdaBoostClassifier(**params).fit(train_X, train_y)
reversefactor = dict(zip(range(4),definitions))
predicted_y = clf.predict(test_X)
predicted_y = np.vectorize(reversefactor.get)(predicted_y)
correct_y = np.vectorize(reversefactor.get)(test_y)
cm = confusion_matrix(correct_y, predicted_y)
print('')
print("THIS IS THE RESULT FOR ADA_BOOST")
print(cm)
acc = accuracy_score(correct_y, predicted_y)
print("Accuracy of training data is " + str(acc))
return acc
def Logistic_Regression_CV(train_X, test_X, train_y, test_y):
clf = LogisticRegressionCV(multi_class='multinomial', solver ='newton-cg', max_iter=4000).fit(train_X, train_y)
#if no max iter, cannot converge!
reversefactor = dict(zip(range(4),definitions))
predicted_y = clf.predict(test_X)
predicted_y = np.vectorize(reversefactor.get)(predicted_y)
correct_y = np.vectorize(reversefactor.get)(test_y)
cm = confusion_matrix(correct_y, predicted_y)
print('')
print("THIS IS THE RESULT FOR LOGISTIC REGRESSION CV")
print(cm)
acc = accuracy_score(correct_y, predicted_y)
print("Accuracy of training data is " + str(acc))
return acc
def Logistic_Regression(train_X, test_X, train_y, test_y):
# if __name__ == '__main__':
clf = LogisticRegression(multi_class='multinomial', solver ='newton-cg', max_iter=4000).fit(train_X, train_y)
#if no max iter, cannot converge!
# , n_jobs=-1
reversefactor = dict(zip(range(4),definitions))
predicted_y = clf.predict(test_X)
predicted_y = np.vectorize(reversefactor.get)(predicted_y)
correct_y = np.vectorize(reversefactor.get)(test_y)
cm = confusion_matrix(correct_y, predicted_y)
print('')
print("THIS IS THE RESULT FOR LOGISTIC REGRESSION")
print(cm)
acc = accuracy_score(correct_y, predicted_y)
print("Accuracy of training data is " + str(acc))
return acc
def SVM(train_X, test_X, train_y, test_y):
clf = SVC(kernel = 'linear' ).fit(X_train, y_train)
# kernel = 'sigmoid' # return 0.2
# kernel = 'poly' # return 0.65
# kernel = 'rbf' # return 0.38 AKA Gaussian
reversefactor = dict(zip(range(4),definitions))
predicted_y = clf.predict(test_X)
predicted_y = np.vectorize(reversefactor.get)(predicted_y)
correct_y = np.vectorize(reversefactor.get)(test_y)
cm = confusion_matrix(correct_y, predicted_y)
print('')
print("THIS IS THE RESULT FOR SUPPORT VECTOR MACHINE (SVM)")
print(cm)
acc = accuracy_score(correct_y, predicted_y)
print("Accuracy of training data is " + str(acc))
return acc
def Neural_Network(train_X, test_X, train_y, test_y):
clf = MLPClassifier(solver='lbfgs', activation = 'logistic', alpha=1e-5,hidden_layer_sizes=(15, ), random_state=1).fit(train_X, train_y)
# solver = 'lbfgs' #0.66, use this for smaller trng set, converge faster
# solver = 'sgd' #0.60
# solver = 'adam' #0.64
# activation = 'relu' #0.66
# activation = 'logistic' #0.71
# activation = 'tanh' #0.69
# activation = 'identity' #0.65
reversefactor = dict(zip(range(4),definitions))
predicted_y = clf.predict(test_X)
predicted_y = np.vectorize(reversefactor.get)(predicted_y)
correct_y = np.vectorize(reversefactor.get)(test_y)
cm = confusion_matrix(correct_y, predicted_y)
print('')
print("THIS IS THE RESULT FOR NEURAL NETWORK")
print(cm)
acc = accuracy_score(correct_y, predicted_y)
print("Accuracy of training data is " + str(acc))
return acc
def Ridge_Classifier(train_X, test_X, train_y, test_y):
clf = RidgeClassifier().fit(train_X, train_y)
reversefactor = dict(zip(range(4),definitions))
predicted_y = clf.predict(test_X)
predicted_y = np.vectorize(reversefactor.get)(predicted_y)
correct_y = np.vectorize(reversefactor.get)(test_y)
cm = confusion_matrix(correct_y, predicted_y)
print('')
print("THIS IS THE RESULT FOR RIDGE CLASSIFIER")
print(cm)
acc = accuracy_score(correct_y, predicted_y)
print("Accuracy of training data is " + str(acc))
return acc
def Voting_Classifier(all_x, all_y):
clf1 = LogisticRegression(multi_class='multinomial',solver ='newton-cg', max_iter=4000, random_state=1)
clf2 = RandomForestClassifier(n_estimators = 1800, criterion = 'gini', random_state=1)
clf3 = GradientBoostingClassifier(n_estimators = 1800, learning_rate = 0.05 )
eclf = VotingClassifier(estimators=[('lr', clf1), ('rf', clf2), ('gb', clf3)], voting='hard') #, n_jobs=-1
print('')
print("THIS IS THE RESULT FOR VOTING CLASSIFIER")
for clf, label in zip([clf1, clf2, clf3, eclf], ['Logistic Regression', 'Random Forest', 'Gradient Boost', 'Ensemble']):
scores = cross_val_score(clf, all_x, all_y, cv=5, scoring='accuracy') #cross validate compute score 5 consec times
print("Accuracy: %0.2f (+/- %0.2f) [%s]" % (scores.mean(), scores.std(), label))
def Naive_Bayes(train_X, test_X, train_y, test_y):
clf = GaussianNB().fit(train_X, train_y)
reversefactor = dict(zip(range(4),definitions))
predicted_y = clf.predict(test_X)
predicted_y = np.vectorize(reversefactor.get)(predicted_y)
correct_y = np.vectorize(reversefactor.get)(test_y)
cm = confusion_matrix(correct_y, predicted_y)
print('')
print("THIS IS THE RESULT FOR NAIVE BAYES")
print(cm)
acc = accuracy_score(correct_y, predicted_y)
print("Accuracy of training data is " + str(acc))
return acc
# if __name__ == '__main__':
# # Logistic_Regression_CV(X_train, X_test, y_train, y_test) # TAKES SUPER DUPER LONG, average 72%
# Logistic_Regression(X_train, X_test, y_train, y_test) # average 72%
# Gradient_Boost(X_train, X_test, y_train, y_test)
# Ada_Boost(X_train, X_test, y_train, y_test) #ADAPTIVE BOOSTING, average 67%
# Logistic_Regression(X_train, X_test, y_train, y_test) # average 72%
# SVM(X_train, X_test, y_train, y_test) #TAKES VERY LONG FOR LINEAR KERNEL, average 68%
# Neural_Network(X_train, X_test, y_train, y_test) #average 71%
# Ridge_Classifier(X_train, X_test, y_train, y_test)
# Voting_Classifier(X, y) #average 77%
# Naive_Bayes(X_train, X_test, y_train, y_test) #average 61%