-
Notifications
You must be signed in to change notification settings - Fork 2
/
malware_classification_so_file.py
129 lines (92 loc) · 3.97 KB
/
malware_classification_so_file.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
import tensorflow as tf
from keras.preprocessing import sequence
import time
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.layers import Embedding
from keras.layers import LSTM
from sklearn.metrics import confusion_matrix, precision_score, recall_score, cohen_kappa_score
import keras_metrics
from keras.layers import Conv1D, MaxPooling1D
from keras.datasets import imdb
from sklearn import preprocessing, svm
import pandas as pd
from keras.optimizers import SGD, Adam, Nadam, Adagrad
from sklearn.model_selection import StratifiedKFold
from keras.layers import BatchNormalization
from keras.layers import Dense, Dropout, LSTM, Embedding, Activation
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.optimizers import SGD, Adam, Nadam
import pandas as pd
import numpy as np
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score, cohen_kappa_score
from keras.utils.vis_utils import plot_model
from keras.layers.normalization import BatchNormalization
import keras
import keras_metrics
from keras import backend as K
import matplotlib.pyplot as plt
input_file = 'dataset.csv'
df = pd.read_csv(input_file)
X = np.array(df.drop(['label'], 1))
df.dropna(inplace=True)
y = np.array(df['label'])
X_train, X_test, y_train, y_test =train_test_split (X, y, test_size=0.2)
# def load_data(test_split = 0.2):
# print ('Loading data...')
# df = pd.read_csv(input_file)
# df['sequence'] = df['sequence'].apply(lambda x: [int(e) for e in x.split()])
# df = df.reindex(np.random.permutation(df.index))
# train_size = int(len(df) * (1 - test_split))
# X_train = df['sequence'].values[:train_size]
# y_train = np.array(df['target'].values[:train_size])
# X_test = np.array(df['sequence'].values[train_size:])
# y_test = np.array(df['target'].values[train_size:])
# return pad_sequences(X_train), y_train, pad_sequences(X_test), y_test
def create_model(input_length):
print ('Creating model...')
model = Sequential()
model.add(Dense(64, input_dim=9, init='uniform'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(64, init='uniform'))
model.add(Activation('tanh'))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Dense(1, init='uniform'))
model.add(Activation('softmax'))
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
adam = Adam(lr=0.05)
print ('Compiling...')
model.compile(loss='categorical_crossentropy',
optimizer=Adam(lr=0.12, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False),
metrics=['accuracy',keras_metrics.precision(), keras_metrics.recall(),keras_metrics.false_positive(), keras_metrics.false_negative()])
#keras_metrics.false_positive(), keras_metrics.false_negative()
return model
# X_train, y_train, X_test, y_test = load_data()
print(X_train.shape)
model = create_model(len(X_train[0]))
print ('Fitting model...')
history = model.fit(X_train, y_train, batch_size=10, nb_epoch=1000, validation_split = 0.1, verbose = 1)
score, acc, prec, rec, false_positive, false_negative = model.evaluate(X_test, y_test, batch_size=50)
# print('Test score:', score)
# print("----------------------------------------------------------")
print("----------------------------------------------------------")
print("----------------------------------------------------------")
print('Test accuracy:', acc ,prec,rec)
print("----------------------------------------------------------")
print("----------------------------------------------------------")
# print(false_negative)
model.summary()
# y_pred = model.predict(X_test)
# y_pred = (y_pred > 0.5)
# cm = confusion_matrix(y_test, y_pred)
# print(cm)
# plt.matshow(cm,cmap='RdBu')
# plt.title('Confusion matrix')
# plt.colorbar()
# plt.ylabel('True label')
# plt.xlabel('Predicted label')
# plt.show()