图卷积 中搭建边信息的代码解释

代码:

import numpy as np
import itertools
import scipy.sparse as sp
edge_index=[] # 建立一个空列表
dst = {2:[5,6,9,8,8],3:[9,8,8],6:[5,9,8]} # 边的信息
for i, j in dst.items():  # 格式为 {index:[index_of_neighbor_nodes]}
    edge_index.extend([i, v] for v in j) 
    edge_index.extend([v, i] for v in j)
print(edge_index)
print(sorted(edge_index))
edge_index = list(k for k, _ in itertools.groupby(sorted(edge_index)))  # 去除重复值
print(edge_index)
print(np.array(edge_index)[:, 1])
print(np.array(edge_index)[:, 0])
edge_index = np.array(edge_index)


adjacency = sp.coo_matrix((np.ones(len(edge_index)),
                           (edge_index[:, 0], edge_index[:, 1])),
                          shape=(10, 10), dtype="float32")  # shape=(10, 10) 因为 dst最大值为9,所以为10

print(adjacency)

输出:

D:\Anaconda3\python.exe D:/0_me_python/1.py
[[2, 5], [2, 6], [2, 9], [2, 8], [2, 8], [5, 2], [6, 2], [9, 2], [8, 2], [8, 2], [3, 9], [3, 8], [3, 8], [9, 3], [8, 3], [8, 3], [6, 5], [6, 9], [6, 8], [5, 6], [9, 6], [8, 6]]
[[2, 5], [2, 6], [2, 8], [2, 8], [2, 9], [3, 8], [3, 8], [3, 9], [5, 2], [5, 6], [6, 2], [6, 5], [6, 8], [6, 9], [8, 2], [8, 2], [8, 3], [8, 3], [8, 6], [9, 2], [9, 3], [9, 6]]
[[2, 5], [2, 6], [2, 8], [2, 9], [3, 8], [3, 9], [5, 2], [5, 6], [6, 2], [6, 5], [6, 8], [6, 9], [8, 2], [8, 3], [8, 6], [9, 2], [9, 3], [9, 6]]
[5 6 8 9 8 9 2 6 2 5 8 9 2 3 6 2 3 6]
[2 2 2 2 3 3 5 5 6 6 6 6 8 8 8 9 9 9]
  (2, 5)	1.0
  (2, 6)	1.0
  (2, 8)	1.0
  (2, 9)	1.0
  (3, 8)	1.0
  (3, 9)	1.0
  (5, 2)	1.0
  (5, 6)	1.0
  (6, 2)	1.0
  (6, 5)	1.0
  (6, 8)	1.0
  (6, 9)	1.0
  (8, 2)	1.0
  (8, 3)	1.0
  (8, 6)	1.0
  (9, 2)	1.0
  (9, 3)	1.0
  (9, 6)	1.0

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清纯世纪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值