-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMAS_Function.py
More file actions
178 lines (144 loc) · 5.51 KB
/
Copy pathMAS_Function.py
File metadata and controls
178 lines (144 loc) · 5.51 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
# import pandas as pd
import matplotlib.pyplot as plt
# import seaborn as sns
from tqdm import tqdm, trange # trange(i)是tqdm(range(i))的一种简单写法
"""
X 数据集
nc 数据集的大小
alpha 权衡参数
epsilon 学习率
distance(x, y) 计算两点之间距离
make_identity(nc) 构建单位阵 I
make_adjacency(x, y) 构造邻接矩阵 A
make_degree(X) 构造度矩阵 D
make_weight(A, M, alpha) 构建权重矩阵 W
make_r_weight(W) 构建权重倒数矩阵 Wr
make_r_degree(Wr) 构建权重合矩阵 Dr
make_instance(A) 构建instance集合 instances
make_motif(A) 构建motif矩阵 M
Algorithm_1(x, y, alpha) 收敛方法 MWMS-S
Algorithm_2(x, y, alpha) 收敛方法 MWMS-J
"""
def distance(x): # 计算两点之间距离
distances = np.sqrt((x[:, np.newaxis] - x) ** 2)
return distances
def make_identity(nc): # 构建单位阵I
I = np.zeros((nc, nc))
for i in range(nc):
I[i, i] = 1
return I
def make_adjacency(x): # 构造邻接矩阵A
distances = distance(x)
nc = len(distances)
A = np.zeros((nc, nc))
for i in range(nc):
for j in range(nc):
if distances[i, j] <= 1 and i != j:
A[i, j] = 1
return A
"""
A = np.where(distances <= 1, 1, 0)
for i in range(len(A):
A[i, i] = 0
"""
def make_degree(A): # 构造度矩阵D
nc = A.shape[0]
D = np.zeros(A.shape)
for i in range(nc):
D[i, i] = np.sum(A[i])
return D
def make_weight(A, M, alpha): # 构建权重矩阵W
W = (1 - alpha) * A + alpha * M
return W
def make_r_weight(W): # 构建权重倒数矩阵Wr
with np.errstate(divide='ignore'):
Wr = np.where(W != 0, 1 / W, 0)
return Wr
def make_r_degree(Wr): # 构建权重合矩阵Dr
nc = len(Wr)
Dr = np.zeros(Wr.shape)
for i in range(nc):
Dr[i, i] = np.sum(Wr[i])
return Dr
def make_instance(A): # 构建instance集合
instances = []
nc = len(A)
for i in range(nc):
for j in range(nc):
if A[i, j] == 1:
for k in range(nc):
if A[j, k] == 1 and A[i, k] == 1:
instances.append(sorted([i, j, k])) # 对数据进行排序以便清除相同项
instances = list(set(map(tuple, instances))) # 清除相同项
return instances
def make_motif(A): # 构建motif矩阵M
nc = len(A)
instances = make_instance(A)
M = np.zeros(A.shape)
for i in range(nc):
for j in np.arange(i+1, nc):
if A[i, j] == 1:
num = 0
for k in range(len(instances)):
if (i in instances[k]) and (j in instances[k]):
num += 1
M[i, j] = M[j, i] = num
return M
def Algorithm_1(x, alpha): # 收敛方法 MWMS-S
try:
A = make_adjacency(x)
nc = len(A)
I = make_identity(nc)
D = make_degree(A)
M = make_motif(A)
W = make_weight(A, M, alpha)
Wr = make_r_weight(W)
Dr = make_r_degree(Wr)
epsilon = np.where(D != 0, 1 / D, 0)
x = (I - np.dot(epsilon, D - np.dot(np.dot(D, np.linalg.inv(Dr)), Wr))).dot(x)
return x
except:
return x
def Algorithm_2(x, alpha): # 收敛方法 MWMS-J
A = make_adjacency(x)
nc = len(A)
I = make_identity(nc)
M = make_motif(A)
W = make_weight(A, M, alpha)
Wr = make_r_weight(W)
Dr = make_r_degree(Wr)
x = np.dot(np.linalg.inv(I + Dr), (I + Wr)).dot(x)
return x
def change_value(value_table, weight_matrix):
if len(weight_matrix) == 2:
max_index = np.argmax(weight_matrix)
return value_table[max_index]
else:
# 将 value_tabel 中的值转换为元组并去重
tabel = list(set(tuple(value) for value in value_table))
# 列数为去重后的 value_tabel 长度,行数为 weight_matrix 长度
nc = len(tabel)
matrix = np.zeros((len(weight_matrix), nc))
# 为每个独特的值(元组)创建索引映射
index_map = {tup: i for i, tup in enumerate(tabel)}
# 遍历 weight_matrix
for i, weight in enumerate(weight_matrix):
for values in value_table:
# 检查 values 是否在 index_map 中
if values in index_map:
# 更新矩阵中的值
matrix[i][index_map[values]] = weight
else:
print(f"Warning: {values} not found in index_map")
# 遍历列,并进行算法处理
for col in range(matrix.shape[1]):
for _ in range(3):
matrix[:, col] = Algorithm_2(matrix[:, col], 0.5)
# 创建字典,其中键是 tabel 中的值,值是矩阵的列
value_dict = {key: matrix[:, index_map[key]] for key in tabel}
# 对字典按列的最大值排序
sorted_items = sorted(value_dict.items(), key=lambda item: np.max(item[1]), reverse=True)
# 选择排序后的前三个元素的键
top_three_keys = [item[0] for item in sorted_items[:3]]
return top_three_keys