-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcal_matrix.py
57 lines (33 loc) · 1.47 KB
/
cal_matrix.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
# -*- coding: utf-8 -*-
import numpy
import random
import argparse
def cal_similarity_matrix(g, t, iterations=100):
s = numpy.ones((t.shape[0], g.shape[0]), dtype=numpy.int64)
for i in range(iterations):
tmp = numpy.dot(numpy.dot(t, s), g.transpose()) + numpy.dot(numpy.dot(t.transpose(), s), g)
tmp = tmp / numpy.linalg.norm(tmp)
s = tmp
return tmp
if __name__ == "__main__":
argument_parser = argparse.ArgumentParser(description="")
argument_parser.add_argument("-p2g", "--path2adjmatg", help="")
argument_parser.add_argument("-p2t", "--path2adjmatt", help="")
argument_parser.add_argument("-i", "--iterations", type=int, help="")
args = argument_parser.parse_args()
#读入两个方阵
if args.path2adjmatg and args.path2adjmatt:
path2adjmatg = args.path2adjmatg
path2adjmatt = args.path2adjmatt
g = numpy.loadtxt(path2adjmatg, dtype=numpy.int64, delimiter=',')
t = numpy.loadtxt(path2adjmatt, dtype=numpy.int64, delimiter=',')
#随机生成两个方阵
else:
nodes4g = random.randint(1, 10)
nodes4t = random.randint(1, 10)
g = numpy.reshape(numpy.random.random_integers(0, 1, size=nodes4g*nodes4g), (nodes4g, nodes4g))
t = numpy.reshape(numpy.random.random_integers(0, 1, size=nodes4t*nodes4t), (nodes4t, nodes4t))
if args.iterations:
cal_similarity_matrix(g, t, args.iterations)
else:
cal_similarity_matrix(g, t)