-
Notifications
You must be signed in to change notification settings - Fork 1
/
m-tfidf-oversampled.py
193 lines (140 loc) · 8.19 KB
/
m-tfidf-oversampled.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
from numpy.ma import count
import pandas as pd
from pandas import DataFrame
import gensim
from gensim.parsing.preprocessing import preprocess_documents
import utils
from gensim.models.coherencemodel import CoherenceModel
import csv
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
import numpy as np, random
from sklearn.model_selection import train_test_split, StratifiedKFold
from sklearn.metrics import classification_report
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import GaussianNB, MultinomialNB
from imblearn.over_sampling import SMOTE
from sklearn.metrics import roc_auc_score
from sklearn.naive_bayes import MultinomialNB
from sklearn import svm
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier
import warnings
warnings.filterwarnings("ignore")
# implements TF-IDF with bias corrections
def main():
pd.set_option('display.max_columns', None)
np.random.RandomState(0)
dfTactics = pd.read_excel('attack-data.xlsx', sheet_name=0)
dfTechniques = pd.read_excel('attack-data.xlsx', sheet_name=2)
dfProcedures = pd.read_excel('attack-data.xlsx', sheet_name=3)
dfTacticsCut = dfTactics.loc[:, ['ID', 'name', 'description']]
dfTacticsCut['type'] = 'tactics'
dfTechniquesCut = dfTechniques.loc[:, ['ID', 'name', 'description']]
dfTechniquesCut['type'] = 'techniques'
dfTechniqueProcedureMerged = pd.merge(dfTechniques, dfProcedures, left_on='ID', right_on='target ID')
dfProceduresCut = dfTechniqueProcedureMerged.loc[:, ['source ID', 'name', 'mapping description']]
dfProceduresCut['ID'] = dfProceduresCut['source ID']
dfProceduresCut['description'] = dfProceduresCut['mapping description']
dfProceduresCut['type'] = 'example'
dfProceduresCut = dfProceduresCut.loc[:, ['ID', 'name', 'description', 'type']]
dataframe = pd.concat([dfTacticsCut, dfTechniquesCut, dfProceduresCut], ignore_index=True)
trainAndTestSet = dataframe.loc[dataframe['type'] == 'example']
trainAndTestSet['name'] = trainAndTestSet['name'].apply(utils.splitTechniqueName)
techniqueNamesWithLessThanFiveExamples = []
trainAndTestSetGrouped = trainAndTestSet.groupby('name')
classCounts = []
for name,group in trainAndTestSetGrouped:
classCounts.append({ 'name' : f'{name}', 'count' : group.shape[0]})
if group.shape[0] < 30:
techniqueNamesWithLessThanFiveExamples.append(name)
file = open('tfidf_bias_corrected_final_result.txt', 'w')
for top_n_class in [2, 4, 8, 16, 32, 64]:
file.write('\n=================')
file.write(f'n = {top_n_class}\n')
print(f'n = {top_n_class}\n')
classCounts_sorted = sorted(classCounts, key = lambda x:x['count'], reverse = True)[0:top_n_class]
classCounts_top_n = [item['name'] for item in classCounts_sorted]
trainAndTestSetFiltered = trainAndTestSet[trainAndTestSet['name'].isin(classCounts_top_n)]
text_corpus = trainAndTestSetFiltered['description'].values
text_corpus = utils.removeURLandCitationBulk(text_corpus)
processed_corpus = preprocess_documents(text_corpus)
list = []
for item in processed_corpus:
text = ' '.join(item)
list.append(text.strip())
trainAndTestSetFiltered['description'] = list
vectorizer = TfidfVectorizer(use_idf=True)
vectors = vectorizer.fit_transform(trainAndTestSetFiltered['description'])
feature_names = vectorizer.get_feature_names()
feature_names = ['feature-' + feature_name for feature_name in feature_names]
dense = vectors.todense()
denselist = dense.tolist()
df = pd.DataFrame(denselist, columns=feature_names)
trainAndTestSetFiltered = pd.concat([trainAndTestSetFiltered.reset_index(drop=True), df.reset_index(drop=True)], axis=1)
numOfColumns = len(trainAndTestSetFiltered.columns)
sm = SMOTE(random_state = 2, sampling_strategy='auto')
X_train_res, y_train_res = sm.fit_resample(trainAndTestSetFiltered.iloc[:, 4:numOfColumns], trainAndTestSetFiltered['name'].ravel())
X_train_res_df = pd.DataFrame(np.array(X_train_res)).add_prefix('column')
y_train_res_df = pd.DataFrame(np.array(y_train_res), columns=['name'])
trainAndTestSetFiltered = pd.concat([ X_train_res_df.reset_index(drop=True), y_train_res_df.reset_index(drop=True) ], axis=1)
# print(trainAndTestSetFiltered.shape)
skf = StratifiedKFold(n_splits=5)
target = trainAndTestSetFiltered.loc[:,'name']
train = []
test = []
for train_index, test_index in skf.split(trainAndTestSetFiltered, target):
train.append( trainAndTestSetFiltered.iloc[train_index] )
test.append( trainAndTestSetFiltered.iloc[test_index] )
for item in ['nb', 'knn', 'svm', 'rf', 'dt', 'nn']:
file.write('\n###################')
file.write(f'classifier: {item}\n')
print(f'classifier: {item}')
accuracy = []
precision_m = []
precision_w = []
recall_m = []
recall_w = []
f1_m = []
f1_w = []
auc = []
for index in range(0, 5):
numOfColumns = len(train[index].columns)
# print(train[index].shape)
# print(test[index].shape)
clf = None
if item == 'nb': clf = GaussianNB().fit(train[index].iloc[:, 0:(numOfColumns-1)], train[index]['name'])
if item == 'nn': clf = MLPClassifier().fit(train[index].iloc[:, 0:(numOfColumns-1)], train[index]['name'])
if item == 'knn': clf = KNeighborsClassifier().fit(train[index].iloc[:, 0:(numOfColumns-1)], train[index]['name'])
if item == 'svm': clf = svm.SVC(probability=True).fit(train[index].iloc[:, 0:(numOfColumns-1)], train[index]['name'])
if item == 'rf': clf = RandomForestClassifier().fit(train[index].iloc[:, 0:(numOfColumns-1)], train[index]['name'])
if item == 'dt': clf = DecisionTreeClassifier().fit(train[index].iloc[:, 0:(numOfColumns-1)], train[index]['name'])
predicted = clf.predict(test[index].iloc[:, 0:(numOfColumns-1)])
output = classification_report(test[index]['name'], predicted, output_dict=True)
probs = clf.predict_proba(test[index].iloc[:, 0:(numOfColumns-1)])
if top_n_class == 2:
auc.append(roc_auc_score( test[index]['name'] , probs[:,1]))
else:
auc.append(roc_auc_score( test[index]['name'] , probs , multi_class='ovr', average='weighted'))
accuracy.append(output['accuracy'])
precision_m.append(output['macro avg']['precision'])
precision_w.append(output['weighted avg']['precision'])
recall_m.append(output['macro avg']['recall'])
recall_w.append(output['weighted avg']['recall'])
f1_m.append(output['macro avg']['f1-score'])
f1_w.append(output['weighted avg']['f1-score'])
file.write(f'accuracy: {sum(accuracy)/5}\n')
file.write(f'precision macro: {sum(precision_m)/5}\n')
file.write(f'precision weighted: {sum(precision_w)/5}\n')
file.write(f'recall macro: {sum(recall_m)/5}\n')
file.write(f'recall weighted: {sum(recall_w)/5}\n')
file.write(f'f1 macro: {sum(f1_m)/5}\n')
file.write(f'f1 weighted: {sum(f1_w)/5}\n')
file.write(f'auc: {sum(auc)/5}\n')
file.write('###################\n')
file.write('=================\n')
file.close()
if __name__ == "__main__":
main()