-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bayes.py
67 lines (55 loc) · 2.17 KB
/
Bayes.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
# -*- code:utf-8 -*-
import pandas as pd
import numpy as np
from sklearn import *
from sklearn import naive_bayes
from sklearn import tree
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier
import random
def get_result(labels, predictions):
TP, FN, TN, FP = 0, 0, 0, 0
for i in range(len(labels)):
if labels[i] == 0:
if predictions[i] == 0:
TP += 1
else:
FN += 1
else:
if predictions[i] == 1:
TN += 1
else:
FP += 1
accuracy = (TP + TN) / (TP + TN + FP + FN)
precision = TP / (TP + FP)
recall = TP / (TP + FN)
F1 = 2 * precision * recall / (precision + recall)
return accuracy, precision, recall, F1
def main():
filename = 'android_traffic.csv'
dataset = pd.read_csv(filename, sep=';')
dataset = np.array(dataset)
le = preprocessing.LabelEncoder()
dataset[:, 0] = le.fit_transform(dataset[:, 0])
dataset[:, -1] = le.fit_transform(dataset[:, -1])
dataset = np.delete(dataset, [11, 12, 13], axis=1)
features = dataset[:, :-1]
labels = np.asarray(dataset[:, -1], dtype=np.float64)
x_train, x_test, y_train, y_test = model_selection.train_test_split(features, labels, test_size=0.3,
random_state=random.randint(0, 100))
# model = naive_bayes.ComplementNB()
# model = tree.DecisionTreeClassifier()
# model = RandomForestClassifier(bootstrap = True, oob_score = True, criterion = 'gini')
model = XGBClassifier()
model.fit(x_train, y_train)
predicted = model.predict(x_test)
accuracy = metrics.accuracy_score(y_test, predicted)
precision = metrics.precision_score(y_test, predicted, pos_label=0)
recall = metrics.recall_score(y_test, predicted, pos_label=0)
F1 = metrics.f1_score(y_test, predicted, pos_label=0)
print('Accuracy: ' + str(accuracy))
print('Precision: ' + str(precision))
print('Recall: ' + str(recall))
print('F1 score: ' + str(F1))
if __name__ == '__main__':
main()