-
Notifications
You must be signed in to change notification settings - Fork 1
/
m-lsi.py
186 lines (131 loc) · 7.74 KB
/
m-lsi.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
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 imblearn.over_sampling import SMOTE
from scipy import spatial
from sklearn.metrics import roc_auc_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn import svm
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neural_network import MLPClassifier
import warnings
warnings.filterwarnings("ignore")
# implements LSI-NN non bias corrected
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)
# trainAndTestSet2 = dataframe.loc[dataframe['type'] == 'techniques']
# trainAndTestSet2['name'] = trainAndTestSet2['name'].apply(utils.splitTechniqueName)
# print(trainAndTestSet2.head())
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('lsi_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}')
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)]
# why 500: https://radimrehurek.com/gensim/auto_examples/core/run_topics_and_transformations.html
text_corpus = trainAndTestSetFiltered['description'].values
text_corpus = utils.removeURLandCitationBulk(text_corpus)
processed_corpus = preprocess_documents(text_corpus)
text_clean = []
for tokens in processed_corpus:
text_clean.append(tokens)
dictionary = gensim.corpora.Dictionary(processed_corpus)
bow_corpus = [dictionary.doc2bow(text) for text in processed_corpus]
tfidf = gensim.models.TfidfModel(bow_corpus, smartirs='nfc', id2word=dictionary)
corpus_tfidf = tfidf[bow_corpus]
lsi = gensim.models.LsiModel(corpus_tfidf, num_topics=500, power_iters=100)
index = gensim.similarities.MatrixSimilarity(lsi[corpus_tfidf])
dfLsi = pd.DataFrame(np.array(index.index)).add_prefix('column')
trainAndTestSetFiltered = pd.concat([trainAndTestSetFiltered.reset_index(drop=True), dfLsi.reset_index(drop=True)], axis=1)
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 ['knn', 'nb', 'svm', 'rf', 'dt', 'nn']:
file.write('\n###################\n')
file.write(f'classifier: {item}')
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)
clf = None
if item == 'nb': clf = GaussianNB().fit(train[index].iloc[:, 4:(numOfColumns)], train[index]['name'])
if item == 'knn': clf = KNeighborsClassifier().fit(train[index].iloc[:, 4:(numOfColumns)], train[index]['name'])
if item == 'nn': clf = MLPClassifier().fit(train[index].iloc[:, 4:(numOfColumns)], train[index]['name'])
if item == 'svm': clf = svm.SVC(probability=True).fit(train[index].iloc[:, 4:(numOfColumns)], train[index]['name'])
if item == 'dt': clf = DecisionTreeClassifier().fit(train[index].iloc[:, 4:(numOfColumns)], train[index]['name'])
if item == 'rf': clf = RandomForestClassifier().fit(train[index].iloc[:, 4:(numOfColumns)], train[index]['name'])
predicted = clf.predict(test[index].iloc[:, 4:(numOfColumns)])
output = classification_report(test[index]['name'], predicted, output_dict = True)
probs = clf.predict_proba(test[index].iloc[:, 4:(numOfColumns)])
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.close()
if __name__ == "__main__":
main()