dgl.node_homophily

dgl.node_homophily(graph, y)[source]

Homophily measure from Geom-GCN: Geometric Graph Convolutional Networks

We follow the practice of a later paper Large Scale Learning on Non-Homophilous Graphs: New Benchmarks and Strong Simple Methods to call it node homophily.

Mathematically it is defined as follows:

\[\frac{1}{|\mathcal{V}|} \sum_{v \in \mathcal{V}} \frac{ | \{u \in \mathcal{N}(v): y_v = y_u \} | } { |\mathcal{N}(v)| },\]

where \(\mathcal{V}\) is the set of nodes, \(\mathcal{N}(v)\) is the predecessors of node \(v\), and \(y_v\) is the class of node \(v\).

Parameters:
  • graph (DGLGraph) – The graph.

  • y (torch.Tensor) – The node labels, which is a tensor of shape (|V|).

Returns:

The node homophily value.

Return type:

float

Examples

>>> import dgl
>>> import torch
>>> graph = dgl.graph(([1, 2, 0, 4], [0, 1, 2, 3]))
>>> y = torch.tensor([0, 0, 0, 0, 1])
>>> dgl.node_homophily(graph, y)
0.6000000238418579