-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrainDynamic.py
More file actions
47 lines (37 loc) · 1.76 KB
/
TrainDynamic.py
File metadata and controls
47 lines (37 loc) · 1.76 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
# -*- coding: utf-8 -*-
"""Untitled
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1Fppd_YPUPO2k5h_FoxBu1kH1QO4JmReA
"""
import csv
import numpy as np
data_benign1 = np.genfromtxt('/content/benign1.txt',delimiter=',')
data_benign2 = np.genfromtxt('/content/benign2.txt',delimiter=',')
data_backdoor = np.genfromtxt('/content/backdoor1.txt',delimiter=',')
data_trojan = np.genfromtxt('/content/trojan1.txt',delimiter=',')
data_trojandownloader = np.genfromtxt('/content/trojandownloader2.txt',delimiter=',')
data_trojandropper = np.genfromtxt('/content/trojandropper2.txt',delimiter=',')
data_virus = np.genfromtxt('/content/virus2.txt',delimiter=',')
data_worm = np.genfromtxt('/content/worm2.txt',delimiter=',')
data = np.concatenate((data_backdoor, data_trojan, data_trojandownloader, data_trojandropper, data_worm, data_virus, data_benign1, data_benign2))
y = np.ones(data_backdoor.shape[0]+data_trojan.shape[0]+data_trojandownloader.shape[0]+data_trojandropper.shape[0]+data_worm.shape[0]+data_virus.shape[0])
y = np.concatenate((y, np.zeros(data_benign1.shape[0]+data_benign2.shape[0])))
print(y[-1])
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(data,y,test_size=0.75)
clf = RandomForestClassifier(n_estimators=5)
clf.fit(X_train,y_train)
clf.score(X_test,y_test)
from sklearn.metrics import confusion_matrix
y_pred = clf.predict(X_test)
confusion_matrix(y_test, y_pred)
import pickle
rf = RandomForestClassifier(n_estimators=5)
rf.fit(X_train, y_train)
with open('dynamic_model', 'wb') as f:
pickle.dump(rf, f)
with open('/content/dynamic_model', 'rb') as f:
rf = pickle.load(f)
rf.score(X_test, y_test)