I want to cluster the nodes of Cora dataset in a way that each cluster only contains the nodes with the same in-degree value. I can code something as follows:
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)
But since I don’t access the index value of the nodes, I don’t know how to place each node in which cluster?
Advertisement
Answer
You can use return_inverse
in torch.unique
to recover the indices.
The nodes with the same value i
in indices
belong to the same cluster because they all have a degree equal to indegree_class[i]
.
indegree_class, indices = torch.unique(indegree, return_inverse=True)