forked from IntelligentDDS/MicroRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagerank.py
274 lines (235 loc) · 8.83 KB
/
pagerank.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import numpy as np
a = np.array([[0, 1, 1 / 2, 0, 1 / 4, 1 / 2, 0],
[1 / 5, 0, 1 / 2, 1 / 3, 0, 0, 0],
[1 / 5, 0, 0, 1 / 3, 1 / 4, 0, 0],
[1 / 5, 0, 0, 0, 1 / 4, 0, 0],
[1 / 5, 0, 0, 1 / 3, 0, 1 / 2, 1],
[0, 0, 0, 0, 1 / 4, 0, 0],
[1 / 5, 0, 0, 0, 0, 0, 0]], dtype=float)
pr = np.array([[1 / 2],
[1 / 3],
[1 / 4],
[0],
[0],
[0],
[0]], dtype=float)
# print Pagerank matrix
def show_matrix(matrix, pr):
print()
print('Metrix:')
n = len(pr)
for i in range(n):
for j in range(n):
print(matrix[i][j], ' ', end=' ')
print()
print()
print('Pr:')
for i in range(n):
print(pr[i][0], ' ', end=' ')
print('\nSize:', len(pr))
def normalization(a):
sumCol = np.sum(a, axis=0)
for i in range(a.shape[0]):
if sumCol[i] == 0:
print('col: %d, sum: %.5f' % (i, sumCol[i]))
continue
for j in range(a.shape[1]):
a[j][i] = a[j][i] / sumCol[i]
return a
def firstPr(c):
pr = np.zeros((c.shape[0], 1), dtype=float)
# sum = np.sum(c, axis=0)[0]
# print(sum)
for i in range(c.shape[0]):
pr[i] = c[i][0] / c.shape[0]
# print pr,"\n==================================================="
return pr
'''
Calculate pagerank weight of anormaly_list or normal_list
:arg
:return
operation weight:
weight[operation][0]: operation
weight[operation][1]: weight
'''
def trace_pagerank(operation_operation, operation_trace, trace_operation, pr_trace, anomaly):
operation_length = len(operation_operation)
trace_length = len(operation_trace)
p_ss = np.zeros((operation_length, operation_length), dtype=np.float32)
p_sr = np.zeros((operation_length, trace_length), dtype=np.float32)
p_rs = np.zeros((trace_length, operation_length), dtype=np.float32)
# matrix = np.zeros((n, n), dtype=np.float32)
pr = np.zeros((trace_length, 1), dtype=np.float32)
node_list = []
for key in operation_operation.keys():
node_list.append(key)
trace_list = []
for key in operation_trace.keys():
trace_list.append(key)
# matrix node*node
for operation in operation_operation:
child_num = len(operation_operation[operation])
for child in operation_operation[operation]:
p_ss[node_list.index(child)][node_list.index(
operation)] = 1.0 / child_num
# matrix node*request
for trace_id in operation_trace:
child_num = len(operation_trace[trace_id])
for child in operation_trace[trace_id]:
p_sr[node_list.index(child)][trace_list.index(trace_id)] \
= 1.0 / child_num
# matrix request*node
for operation in trace_operation:
child_num = len(trace_operation[operation])
for child in trace_operation[operation]:
p_rs[trace_list.index(child)][node_list.index(operation)] \
= 1.0 / child_num
kind_list = np.zeros(len(trace_list))
p_srt = p_sr.T
for i in range(len(trace_list)):
index_list = [i]
if kind_list[i] != 0:
continue
n = 0
for j in range(i, len(trace_list)):
if (p_srt[i] == p_srt[j]).all():
index_list.append(j)
n += 1
for index in index_list:
kind_list[index] = n
num_sum_trace = 0
kind_sum_trace = 0
if not anomaly:
for trace_id in pr_trace:
num_sum_trace += 1.0 / kind_list[trace_list.index(trace_id)]
for trace_id in pr_trace:
pr[trace_list.index(trace_id)] = 1.0 / \
kind_list[trace_list.index(trace_id)] / num_sum_trace
else:
for trace_id in pr_trace:
kind_sum_trace += 1.0 / kind_list[trace_list.index(trace_id)]
num_sum_trace += 1.0 / len(pr_trace[trace_id])
for trace_id in pr_trace:
pr[trace_list.index(trace_id)] = 1.0 / (kind_list[trace_list.index(trace_id)] / kind_sum_trace * 0.5
+ 1.0 / len(pr_trace[trace_id])) / num_sum_trace * 0.5
if anomaly:
print('\nAnomaly_PageRank:')
else:
print('\nNormal_PageRank:')
result = pageRank(p_ss, p_sr, p_rs, pr, operation_length, trace_length)
weight = {}
sum = 0
for operation in operation_operation:
sum += result[node_list.index(operation)][0]
trace_num_list = {}
for operation in operation_operation:
trace_num_list[operation] = 0
i = node_list.index(operation)
for j in range(len(trace_list)):
if p_sr[i][j] != 0:
trace_num_list[operation] += 1
for operation in operation_operation:
weight[operation] = result[node_list.index(
operation)][0] * sum / len(operation_operation)
# for score in sorted(weight.items(), key=lambda x: x[1], reverse=True):
# print('%-50s: %.5f' % (score[0], score[1]))
return weight, trace_num_list
# calculate pageRank vaule
def pageRank(p_ss, p_sr, p_rs, v, operation_length, trace_length, d=0.85, alpha=0.01):
iteration = 25
service_ranking_vector = np.ones(
(operation_length, 1)) / float(operation_length + trace_length)
request_ranking_vector = np.ones(
(trace_length, 1)) / float(operation_length + trace_length)
for i in range(iteration):
updated_service_ranking_vector = d * \
(np.dot(p_sr, request_ranking_vector) +
alpha * np.dot(p_ss, service_ranking_vector))
updated_request_ranking_vector = d * \
np.dot(p_rs, service_ranking_vector) + (1.0 - d) * v
service_ranking_vector = updated_service_ranking_vector / \
np.amax(updated_service_ranking_vector)
request_ranking_vector = updated_request_ranking_vector / \
np.amax(updated_request_ranking_vector)
normalized_service_ranking_vector = service_ranking_vector / \
np.amax(service_ranking_vector)
return normalized_service_ranking_vector
if __name__ == "__main__":
p_ss = np.zeros((3, 4), dtype=np.float32)
print(p_ss)
p_ss[0][1] = 1
p_ss[1][0] = 2
print(p_ss)
print(p_ss.T)
print(p_ss.T[1])
print((p_ss.T[1] == p_ss.T[3]).all())
print((p_ss.T[2] == p_ss.T[3]).all())
# ap_ss = np.array([[0, 0, 0, 0],
# [1 / 3, 0, 0, 0],
# [1 / 3, 0, 0, 0],
# [1 / 3, 1, 1, 0]], dtype=float)
#
# ap_sr = np.array([[1 / 2, 1 / 3, 1 / 3],
# [0, 0, 1 / 3],
# [0, 1 / 3, 0],
# [1 / 2, 1 / 3, 1 / 3]], dtype=float)
# print(ap_ss)
# print(ap_ss[0])
# ap_rs = np.array([[1 / 3, 0, 0, 1 / 3],
# [1 / 3, 0, 1, 1 / 3],
# [1 / 3, 1, 0, 1 / 3]], dtype=float)
#
# a_v = np.array([[1], [1 / 3], [1 / 3]], dtype=float)
#
# p_ss = np.array([[0, 0, 0],
# [1, 0, 0],
# [0, 1, 0]], dtype=float)
#
# p_sr = np.array([[1 / 3], [1 / 3], [1 / 3]], dtype=float)
#
# p_rs = np.array([1, 1, 1], dtype=float)
#
# v = np.array([1 / 3], dtype=float)
#
# anomaly_result = pageRank(ap_ss, ap_sr, ap_rs, a_v, 4, 3)
# print(anomaly_result)
#
# normal_result = pageRank(p_ss, p_sr, p_rs, v, 3, 1)
# print(normal_result)
#
# spectrum = {}
# anomaly_list_len = 3
# normal_list_len = 1
#
# for node in range(4):
# spectrum[node] = {}
# spectrum[node]['ef'] = anomaly_result[node] * anomaly_list_len
# spectrum[node]['nf'] = anomaly_list_len - anomaly_result[node] * anomaly_list_len
# if node in normal_result:
# spectrum[node]['ep'] = normal_result[node] * normal_list_len
# spectrum[node]['np'] = normal_list_len - normal_result[node] * normal_list_len
# else:
# spectrum[node]['ep'] = 0.0000001
# spectrum[node]['np'] = 0.0000001
#
# for node in range(3):
# if node not in spectrum:
# spectrum[node] = {}
# spectrum[node]['ep'] = normal_result[node] * normal_list_len
# spectrum[node]['np'] = normal_list_len - normal_result[node] * normal_list_len
# if node in anomaly_result:
# spectrum[node]['ef'] = anomaly_result[node] * anomaly_list_len
# spectrum[node]['nf'] = anomaly_list_len - anomaly_result[node] * anomaly_list_len
# else:
# spectrum[node]['ef'] = 0.0000001
# spectrum[node]['nf'] = 0.0000001
#
# # print('\n Micro Rank Spectrum raw:')
# # print(json.dumps(spectrum))
# result = {}
#
# for node in spectrum:
# # Dstar2
# result[node] = spectrum[node]['ef'] * spectrum[node]['ef'] / (spectrum[node]['ef'] + spectrum[node]['nf'])
#
# print(result)