-
Notifications
You must be signed in to change notification settings - Fork 28
/
trainer.py
211 lines (135 loc) · 8.4 KB
/
trainer.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import pandas
from sklearn import preprocessing
from sklearn.ensemble import RandomForestClassifier
import numpy
from sklearn import svm
from sklearn import cross_validation as cv
import matplotlib.pylab as plt
import warnings
from sklearn.ensemble import BaggingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
warnings.filterwarnings("ignore", category=DeprecationWarning,
module="pandas", lineno=570)
from sklearn.ensemble import GradientBoostingClassifier
#from xgboost import XGBClassifier
def return_nonstring_col(data_cols): # giving columns that are not string in nature like url , host, path
cols_to_keep=[]
train_cols=[]
for col in data_cols:
if col!='URL' and col!='host' and col!='path':
cols_to_keep.append(col)
if col!='malicious' and col!='result':
train_cols.append(col)
return [cols_to_keep,train_cols]
def svm_classifier(train,query,train_cols): # train is train dataset and query is test dataset and train_cols is are the columns of train dataset exclude malicious
clf = svm.SVC()
print (clf.fit(train[train_cols], train['malicious']))
scores = cv.cross_val_score(clf, train[train_cols], train['malicious'], cv=30)
print('Estimated score SVM: %0.5f (+/- %0.5f)' % (scores.mean(), scores.std() / 2))
query['result']=clf.predict(query[train_cols])
print (query[['URL','result']])
query[['URL','result']].to_csv("C:/Users/hp/phishing/test_predicted_target_svm.csv")
# Called from gui
def forest_classifier_gui(train,query,train_cols):# train is train dataset and query is test dataset and train_cols is are the columns of train dataset exclude malicious
rf = RandomForestClassifier(n_estimators=150)
print (rf.fit(train[train_cols], train['malicious']))
query['result']=rf.predict(query[train_cols])
print (query[['URL','result']].head(2))
return query['result']
def svm_classifier_gui(train,query,train_cols): # train is train dataset and query is test dataset and train_cols is are the columns of train dataset exclude malicious
clf = svm.SVC()
train[train_cols] = preprocessing.scale(train[train_cols])
query[train_cols] = preprocessing.scale(query[train_cols])
print (clf.fit(train[train_cols], train['malicious']))
query['result']=clf.predict(query[train_cols])
print (query[['URL','result']].head(2))
return query['result']
def GradientBoosting_Classifier_gui(train,query,train_cols): # train is train dataset and query is test dataset and train_cols is are the columns of train dataset exclude malicious
grad = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1, random_state=0)
print (grad.fit(train[train_cols], train['malicious']))
query['result']=grad.predict(query[train_cols])
print (query[['URL','result']].head(2))
return query['result']
def forest_classifier(train,query,train_cols): # train is train dataset and query is test dataset and train_cols is are the columns of train dataset exclude malicious
rf = RandomForestClassifier(n_estimators=150)
print (rf.fit(train[train_cols], train['malicious']))
scores = cv.cross_val_score(rf, train[train_cols], train['malicious'], cv=30)
print('Estimated score RandomForestClassifier: %0.5f (+/- %0.5f)' % (scores.mean(), scores.std() / 2))
query['result']=rf.predict(query[train_cols])
print (query[['URL','result']])
query[['URL','result']].to_csv("C:/Users/hp/phishing/test_predicted_target_rf.csv")
def Bagging_Classifier(train,query,train_cols): # train is train dataset and query is test dataset and train_cols is are the columns of train dataset exclude malicious
bag = BaggingClassifier(n_estimators=150)
print (bag.fit(train[train_cols], train['malicious']))
scores = cv.cross_val_score(bag, train[train_cols], train['malicious'], cv=30)
print('Estimated score BaggingClassifier : %0.5f (+/- %0.5f)' % (scores.mean(), scores.std() / 2))
query['result']=bag.predict(query[train_cols])
print (query[['URL','result']])
query[['URL','result']].to_csv("C:/Users/hp/phishing/test_predicted_target_bag.csv")
def logistic_regression(train,query,train_cols): # train is train dataset and query is test dataset and train_cols is are the columns of train dataset exclude malicious
logis = LogisticRegression()
print (logis.fit(train[train_cols], train['malicious']))
scores = cv.cross_val_score(logis, train[train_cols], train['malicious'], cv=30)
print('Estimated score logisticregression : %0.5f (+/- %0.5f)' % (scores.mean(), scores.std() / 2))
query['result']=logis.predict(query[train_cols])
print (query[['URL','result']])
query[['URL','result']].to_csv("C:/Users/hp/phishing/test_predicted_target_logis.csv")
def DecisionTree_Classifier(train,query,train_cols): # train is train dataset and query is test dataset and train_cols is are the columns of train dataset exclude malicious
deci = DecisionTreeClassifier(random_state = 100,max_depth=3, min_samples_leaf=5)
print (deci.fit(train[train_cols], train['malicious']))
scores = cv.cross_val_score(deci, train[train_cols], train['malicious'], cv=30)
print('Estimated score decisiontreeclassifier : %0.5f (+/- %0.5f)' % (scores.mean(), scores.std() / 2))
query['result']=deci.predict(query[train_cols])
print (query[['URL','result']])
query[['URL','result']].to_csv("C:/Users/hp/phishing/test_predicted_target_deci.csv")
def KNeighbors_Classifier(train,query,train_cols): # train is train dataset and query is test dataset and train_cols is are the columns of train dataset exclude malicious
Kneigh = KNeighborsClassifier()
print (Kneigh.fit(train[train_cols], train['malicious']))
scores = cv.cross_val_score(Kneigh, train[train_cols], train['malicious'], cv=30)
print('Estimated score KNeighborsClassifier : %0.5f (+/- %0.5f)' % (scores.mean(), scores.std() / 2))
query['result']=Kneigh.predict(query[train_cols])
print (query[['URL','result']])
query[['URL','result']].to_csv("C:/Users/hp/phishing/test_predicted_target_Kneigh.csv")
def GradientBoosting_Classifier(train,query,train_cols): # train is train dataset and query is test dataset and train_cols is are the columns of train dataset exclude malicious
grad = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1, random_state=0)
print (grad.fit(train[train_cols], train['malicious']))
scores = cv.cross_val_score(grad, train[train_cols], train['malicious'], cv=30)
print('Estimated score GradientBoostingClassifier : %0.5f (+/- %0.5f)' % (scores.mean(), scores.std() / 2))
query['result']=grad.predict(query[train_cols])
print (query[['URL','result']])
query[['URL','result']].to_csv("C:/Users/hp/phishing/test_predicted_target_grad.csv")
def Xg_boost(train,query,train_cols): # train is train dataset and query is test dataset and train_cols is are the columns of train dataset exclude malicious
xg = XGBClassifier()
print (xg.fit(train[train_cols], train['malicious']))
scores = cv.cross_val_score(xg, train[train_cols], train['malicious'], cv=30)
print('Estimated score Xgboost : %0.5f (+/- %0.5f)' % (scores.mean(), scores.std() / 2))
query['result']=xg.predict(query[train_cols])
print (query[['URL','result']])
query[['URL','result']].to_csv("C:/Users/hp/phishing/test_predicted_target_xg.csv")
def train(db,test_db):
query_csv = pandas.read_csv(test_db)
cols_to_keep,train_cols=return_nonstring_col(query_csv.columns)
#query=query_csv[cols_to_keep]
train_csv = pandas.read_csv(db)
cols_to_keep,train_cols=return_nonstring_col(train_csv.columns)
train=train_csv[cols_to_keep]
#svm_classifier(train_csv,query_csv,train_cols)
#forest_classifier(train_csv,query_csv,train_cols)
#Bagging_Classifier(train_csv,query_csv,train_cols)
#logistic_regression(train_csv,query_csv,train_cols)
#DecisionTree_Classifier(train_csv,query_csv,train_cols)
#KNeighbors_Classifier(train_csv,query_csv,train_cols)
GradientBoosting_Classifier(train_csv,query_csv,train_cols)
#Xg_boost(train_csv,query_csv,train_cols)
def gui_caller(db,test_db):
query_csv = pandas.read_csv(test_db)
cols_to_keep,train_cols=return_nonstring_col(query_csv.columns)
#query=query_csv[cols_to_keep]
train_csv = pandas.read_csv(db)
cols_to_keep,train_cols=return_nonstring_col(train_csv.columns)
train=train_csv[cols_to_keep]
return forest_classifier_gui(train_csv,query_csv,train_cols)
#return svm_classifier_gui(train_csv,query_csv,train_cols)
#return GradientBoosting_Classifier_gui(train_csv,query_csv,train_cols)