-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprior.py
53 lines (47 loc) · 2 KB
/
prior.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
import numpy as np
import matplotlib.pyplot as plt
import arff
names = ["33-1-2-43-1-32", "CTU-IoT-Malware-Capture-1-1_0", "CTU-IoT-Malware-Capture-33-1-p_2", "CTU-IoT-Malware-Capture-43-1-p_0", "CTU-IoT-Malware-Capture-43-1-p_3"]
titles = ["33-1-2-43-1-3", "CTU-IoT-Malware-Capture-1-1 | benign vs all", "CTU-IoT-Malware-Capture-33-1-p | benign vs 2", "CTU-IoT-Malware-Capture-43-1-p | benign vs all", "CTU-IoT-Malware-Capture-43-1-p | benign vs 3"]
for x, name in enumerate(names):
dataset = arff.load(open("arff/" + name + ".arff"))
data = np.array(dataset['data'])
y = data[:,-1].astype(int)
chunk_samples = 250
n_chunks = int(y.shape[0]/chunk_samples)
print(name)
print(n_chunks)
start = 0
stop = 0
prior = np.zeros((2, n_chunks))
for i in range(n_chunks):
stop = start+chunk_samples
# print("%i -- %i" % (start, stop))
chunk_y = y[start:stop]
start += chunk_samples
classes, counts = np.unique(chunk_y, return_counts=True)
# print(classes, counts)
if counts.shape[0] == 2:
prior[0, i] = counts[0]/chunk_samples
prior[1, i] = counts[1]/chunk_samples
# if counts.shape[0] == 1 and 0 in classes:
# prior[0, i] = 1.0
# prior[1, i] = 0.0
# # chuj = i
# if counts.shape[0] == 1 and 1 in classes:
# prior[0, i] = 0.0
# prior[1, i] = 1.0
fig = plt.figure(figsize=(15, 5))
plt.plot(prior[0], c="black", label="Benign")
plt.plot(prior[1], c="red", label="Malicious")
plt.xlim(0, n_chunks)
plt.ylim(-0.006, 1.006)
plt.title("%s" % titles[x], fontsize=16)
plt.legend(ncol=2, loc=1, bbox_to_anchor=(1.0, 1.1), frameon=False, fontsize=12)
plt.grid(ls="--", color=(0.85, 0.85, 0.85))
plt.xlabel("Data chunk", fontsize=12)
plt.ylabel("Prior", fontsize=12)
plt.tight_layout()
plt.savefig("figures/prior/%s.eps" % (name), dpi=200)
# plt.savefig("figures/%s_%i.eps" % (filename[:-4], p))
plt.close()