我想以每个集群只包含具有相同入度值的节点的方式对 Cora 数据集的节点进行集群。我可以编写如下代码:
import torch
from torch_geometric.datasets import Planetoid
from torch_geometric.utils import degree
dataset = Planetoid('./data','CORA')
data = dataset[0]
n = data.num_nodes
indegree = degree(data.edge_index[1], n, dtype=torch.long)
counts = torch.bincount(indegree)
但是由于我没有访问节点的索引值,所以不知道如何将每个节点放在哪个集群中?
最佳答案
您可以使用 return_inverse
在 torch.unique
恢复指数。
具有相同值的节点 i
在 indices
属于同一个簇,因为它们的度数都等于 indegree_class[i]
.
indegree_class, indices = torch.unique(indegree, return_inverse=True)
关于python - 如何根据入度值对 Cora 数据集的节点进行聚类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68275680/