-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgrammingTask.py
More file actions
178 lines (134 loc) · 6.09 KB
/
Copy pathProgrammingTask.py
File metadata and controls
178 lines (134 loc) · 6.09 KB
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
import numpy as np
from sklearn.metrics.pairwise import euclidean_distances
# Step 1: Create and save "normal" and "abnormal" arrays to binary files
def create_data():
# Create "normal" array with random values between 0 and 10
normal = np.random.uniform(0, 10, size=(100, 1000))
normal.tofile("normal.bin")
# print(normal.shape) # shape represents (number of rows, number of columns)
# Create "abnormal" array with random values between 5 and 15
abnormal = np.random.uniform(5, 15, size=(100, 1000))
abnormal.tofile("abnormal.bin")
# Step 2: Load the saved data into two arrays
def load_data():
normal = np.fromfile("normal.bin").reshape(100, 1000)
abnormal = np.fromfile("abnormal.bin").reshape(100, 1000)
# print(normal)
# print(abnormal)
return normal, abnormal
# Step 3: Split data into training and test sets
def split_data(normal, abnormal):
training, normal_test = normal[:90], normal[90:]
abnormal_test = abnormal[:10]
test = np.vstack((normal_test, abnormal_test))
# test2 = np.concatenate((normal_test, abnormal_test), axis=0)
# test_labels = np.array([0] * len(normal_test) + [1] * len(abnormal_test))
# return training, test, test_labels
return training, test
# Step 4: Calculate dissimilarity scores for the training set
def calculate_dissimilarity_scores(training):
baseline = []
for i in range(len(training)):
distances = euclidean_distances(training[i].reshape(1, -1), training)
# top_distances = np.partition(distances, 6)[:, 1:6] # Exclude self-distance
sorted_distances = np.sort(distances)
top_distances = sorted_distances[:, 1:6]
score = top_distances.sum()
baseline.append(score)
return baseline
# Step 5: Detect anomalies in the test set
def print_predictions(training, test, baseline):
test_scores = []
for i in range(len(test)):
distances = euclidean_distances(test[i].reshape(1, -1), training)
# top_distances = np.partition(distances, 6)[:, 1:6] # Exclude self-distance
sorted_distances = np.sort(distances)
top_distances = sorted_distances[:, 1:6]
score = top_distances.sum()
test_scores.append(score)
min_baseline_value = min(baseline)
max_baseline_value = max(baseline)
# print(min_baseline_value)
# print(max_baseline_value)
# print(min(test_scores))
# print(max(test_scores))
# print(test_scores)
predictions = []
for score in test_scores:
if min_baseline_value <= score <= max_baseline_value:
predictions.append("Normal")
else:
predictions.append("Abnormal")
return predictions
if __name__ == "__main__":
create_data()
normal, abnormal = load_data()
training, test = split_data(normal, abnormal)
baseline = calculate_dissimilarity_scores(training)
predictions = print_predictions(training, test, baseline)
for i, prediction in enumerate(predictions):
print(f"Data {i}: {prediction}")
"""
import numpy as np
from sklearn.metrics.pairwise import euclidean_distances
from sklearn.preprocessing import StandardScaler
# Step 1: Create and save "normal" and "abnormal" arrays to binary files
def create_and_save_data():
# Create "normal" array with random values between 0 and 10
normal_array = np.random.uniform(0, 10, size=(100, 1000))
normal_array.tofile("normal.bin")
# Create "abnormal" array with random values between 5 and 15
abnormal_array = np.random.uniform(5, 15, size=(100, 1000))
abnormal_array.tofile("abnormal.bin")
# Step 2: Load the saved data into two arrays
def load_data():
normal_data = np.fromfile("normal.bin").reshape(100, 1000)
abnormal_data = np.fromfile("abnormal.bin").reshape(100, 1000)
return normal_data, abnormal_data
# Step 3: Split data into training and test sets
def split_data(normal_data, abnormal_data):
normal_train, normal_test = normal_data[:90], normal_data[90:]
abnormal_test = abnormal_data[:10]
test_data = np.vstack((normal_test, abnormal_test))
test_labels = np.array([0] * len(normal_test) + [1] * len(abnormal_test))
return normal_train, test_data, test_labels
# Step 4: Calculate dissimilarity scores for the training set
def calculate_baseline_scores(normal_train):
baseline_scores = []
for i in range(len(normal_train)):
distances = euclidean_distances(normal_train[i].reshape(1, -1), normal_train)
top_distances = np.partition(distances, 6)[:, 1:6] # Exclude self-distance
score = top_distances.sum()
baseline_scores.append(score)
return baseline_scores
# Step 5: Detect anomalies in the test set
def detect_anomalies(test_data, normal_train, baseline_scores):
scaler = StandardScaler()
baseline_scores = np.array(baseline_scores)
baseline_scores = scaler.fit_transform(baseline_scores.reshape(-1, 1))
test_scores = []
for i in range(len(test_data)):
distances = euclidean_distances(test_data[i].reshape(1, -1), normal_train)
top_distances = np.partition(distances, 6)[:, 1:6] # Exclude self-distance
score = top_distances.sum()
test_scores.append(score)
test_scores = np.array(test_scores)
test_scores = scaler.transform(test_scores.reshape(-1, 1))
min_score = min(baseline_scores)
max_score = max(baseline_scores)
predictions = []
for score in test_scores:
if min_score <= score <= max_score:
predictions.append("Normal")
else:
predictions.append("Abnormal")
return predictions
if __name__ == "__main__":
create_and_save_data()
normal_data, abnormal_data = load_data()
normal_train, test_data, test_labels = split_data(normal_data, abnormal_data)
baseline_scores = calculate_baseline_scores(normal_train)
predictions = detect_anomalies(test_data, normal_train, baseline_scores)
for i, prediction in enumerate(predictions):
print(f"Data point {i}: {prediction}")
"""