Source code for torch_geometric.utils.homophily

import torch
from torch import Tensor
from torch_sparse import SparseTensor


@torch.jit._overload
def homophily_ratio(adj_t, y):
    # type: (SparseTensor, Tensor) -> float
    pass


@torch.jit._overload
def homophily_ratio(edge_index, y):
    # type: (Tensor, Tensor) -> float
    pass


[docs]def homophily_ratio(edge_index, y): if isinstance(edge_index, SparseTensor): row, col, _ = edge_index.coo() else: row, col = edge_index return int((y[row] == y[col]).sum()) / row.size(0)