Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cluster the nodes of the Cora dataset based on their in-degree values?

Tags:

python

pytorch

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?

like image 714
plpm Avatar asked Nov 06 '22 01:11

plpm


1 Answers

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)
like image 132
LGrementieri Avatar answered Nov 12 '22 14:11

LGrementieri