-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_bc.py
168 lines (125 loc) · 8.35 KB
/
train_bc.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
import sys
from util import file_to_list, get_scores, load_matrices_and_labels
from util import decision_function_to_proba, save_y_pred_prob
import os
from sklearn.feature_extraction.text import CountVectorizer as CountVectorizer
from sklearn.svm import LinearSVC
import argparse
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
def parse_option():
parser = argparse.ArgumentParser('Train BC')
parser.add_argument(
'--path_save', type=str,
help='path where to save outputs', required=True)
parser.add_argument(
'--split_id', type=int,
help='the id of the dataset split to use: 1, 2, 3, 4, 5 or time',
required=True)
opt = parser.parse_args()
return opt
def main():
opt = parse_option()
if not os.path.exists(opt.path_save):
os.makedirs(opt.path_save)
################################# DREBIN #################################
x_train1, x_valid1, x_test1, y_train, y_valid, y_test = load_matrices_and_labels(opt,
"drebin")
clf = LinearSVC(random_state=0)
clf.fit(x_train1, y_train)
y_predict_test, y_proba_test, rec, pre, f1, acc, _ = get_scores(clf, x_test1,
y_test, "Test", 0)
y_predict_valid, y_proba_valid, rec, pre, f1, acc, _ = get_scores(clf, x_valid1,
y_valid, "Valid", 0)
y_predict_train, y_proba_train, rec, pre, f1, acc, _ = get_scores(clf, x_train1,
y_train, "Train", 0)
#Decision_func_to_predict_proba = ((Input - InputLow) /
# (InputHigh - InputLow)) * (OutputHigh - OutputLow) + OutputLow
InputLow_pos = min([i for i in y_proba_train if i>=0])
InputHigh_pos = max([i for i in y_proba_train if i>=0])
#max because they are negative
InputLow_neg = abs(max([i for i in y_proba_train if i<=0]))
InputHigh_neg = abs(min([i for i in y_proba_train if i<=0])) #same for min
y_proba_train, y_proba_valid, y_proba_test = decision_function_to_proba(
InputLow_pos, InputHigh_pos, InputLow_neg, InputHigh_neg, y_train, y_valid,
y_test, y_proba_train, y_proba_valid, y_proba_test)
save_y_pred_prob(y_predict_test, y_predict_valid, y_predict_train,
y_proba_train, y_proba_valid, y_proba_test, opt, "drebin")
#######################"# REVEAL #####################################
x_train1, x_valid1, x_test1, y_train, y_valid, y_test = load_matrices_and_labels(opt,
"reveal")
clf = LinearSVC(C=0.01, penalty="l1", dual=False, random_state=0)
clf.fit(x_train1, y_train)
y_predict_test, y_proba_test, rec, pre, f1, acc, _ = get_scores(clf, x_test1,
y_test, "Test", 0)
y_predict_valid, y_proba_valid, rec, pre, f1, acc, _ = get_scores(clf, x_valid1,
y_valid, "Valid", 0)
y_predict_train, y_proba_train, rec, pre, f1, acc, _ = get_scores(clf, x_train1,
y_train, "Train", 0)
#Decision_func_to_predict_proba = ((Input - InputLow) /
# (InputHigh - InputLow)) * (OutputHigh - OutputLow) + OutputLow
InputLow_pos = min([i for i in y_proba_train if i>=0])
InputHigh_pos = max([i for i in y_proba_train if i>=0])
#max because they are negative
InputLow_neg = abs(max([i for i in y_proba_train if i<=0]))
#same for min
InputHigh_neg = abs(min([i for i in y_proba_train if i<=0]))
y_proba_train, y_proba_valid, y_proba_test = decision_function_to_proba(
InputLow_pos, InputHigh_pos, InputLow_neg, InputHigh_neg, y_train, y_valid,
y_test, y_proba_train, y_proba_valid, y_proba_test)
save_y_pred_prob(y_predict_test, y_predict_valid, y_predict_train,
y_proba_train, y_proba_valid, y_proba_test, opt, "reveal")
############################### MaMaF ###################################
x_train1, x_valid1, x_test1, y_train, y_valid, y_test = load_matrices_and_labels(opt,
"mama_f")
clf = RandomForestClassifier(max_depth=8, n_estimators=51, random_state=0)
clf.fit(x_train1, y_train)
y_predict_test, y_proba_test, rec, pre, f1, acc, _ = get_scores(clf, x_test1,
y_test, "Test", 1)
y_predict_valid, y_proba_valid, rec, pre, f1, acc, _ = get_scores(clf, x_valid1,
y_valid, "Valid", 1)
y_predict_train, y_proba_train, rec, pre, f1, acc, _ = get_scores(clf, x_train1,
y_train, "Train", 1)
save_y_pred_prob(y_predict_test, y_predict_valid, y_predict_train, y_proba_train,
y_proba_valid, y_proba_test, opt, "mama_f")
############################# MaMaP ###################################
x_train1, x_valid1, x_test1, y_train, y_valid, y_test = load_matrices_and_labels(opt,
"mama_p")
clf = RandomForestClassifier(max_depth=64, n_estimators=101, random_state=0)
clf.fit(x_train1, y_train)
y_predict_test, y_proba_test, rec, pre, f1, acc, _ = get_scores(clf, x_test1,
y_test, "Test", 1)
y_predict_valid, y_proba_valid, rec, pre, f1, acc, _ = get_scores(clf, x_valid1,
y_valid, "Valid", 1)
y_predict_train, y_proba_train, rec, pre, f1, acc, _ = get_scores(clf, x_train1,
y_train, "Train", 1)
save_y_pred_prob(y_predict_test, y_predict_valid, y_predict_train, y_proba_train,
y_proba_valid, y_proba_test, opt, "mama_p")
############################### MalScanA #############################
x_train1, x_valid1, x_test1, y_train, y_valid, y_test = load_matrices_and_labels(opt,
"malscan_a")
clf = KNeighborsClassifier(n_neighbors=1)
clf.fit(x_train1, y_train)
y_predict_test, y_proba_test, rec, pre, f1, acc, _ = get_scores(clf, x_test1,
y_test, "Test", 1)
y_predict_valid, y_proba_valid, rec, pre, f1, acc, _ = get_scores(clf, x_valid1,
y_valid, "Valid", 1)
y_predict_train, y_proba_train, rec, pre, f1, acc, _ = get_scores(clf, x_train1,
y_train, "Train", 1)
save_y_pred_prob(y_predict_test, y_predict_valid, y_predict_train, y_proba_train,
y_proba_valid, y_proba_test, opt, "malscan_a")
######################## MalScanCO ###################################
x_train1, x_valid1, x_test1, y_train, y_valid, y_test = load_matrices_and_labels(opt,
"malscan_co")
clf = KNeighborsClassifier(n_neighbors=1)
clf.fit(x_train1, y_train)
y_predict_test, y_proba_test, rec, pre, f1, acc, _ = get_scores(clf, x_test1,
y_test, "Test", 1)
y_predict_valid, y_proba_valid, rec, pre, f1, acc, _ = get_scores(clf, x_valid1,
y_valid, "Valid", 1)
y_predict_train, y_proba_train, rec, pre, f1, acc, _ = get_scores(clf, x_train1,
y_train, "Train", 1)
save_y_pred_prob(y_predict_test, y_predict_valid, y_predict_train, y_proba_train,
y_proba_valid, y_proba_test, opt, "malscan_co")
if __name__ == '__main__':
main()