Skip to content

LinkPredMetricData

LinkPredMetricData(
    pred_index_mat: ArrayLike,
    edge_label_index: EdgeLabelIndex,
    edge_label_weight: Optional[ArrayLike] = None,
)

The predictions and ground truth of a mini-batch, with the quantities shared by the link prediction metrics.

Ground-truth entries with a non-positive edge_label_weight are removed.

Parameters:

Name Type Description Default
pred_index_mat ArrayLike

Top-\(k\) predictions of every example, shape [batch_size, k]

required
edge_label_index EdgeLabelIndex

Ground-truth (example, item) pairs, shape [2, num_labels] (or a tuple of two arrays)

required
edge_label_weight Optional[ArrayLike]

Relevance of every ground-truth pair, shape [num_labels]. Default: None

None

Attributes:

Name Type Description
pred_rel_mat ndarray

Relevance of the \(k\)-th prediction of every example: a boolean

label_count ndarray

Number of ground-truth labels of every example.

label_weight_sum ndarray

Sum of the edge label weights of every example.

edge_label_weight_pos Optional[ndarray]

Position of every ground-truth label within its example, in

Source code in mlx_graphs/metrics/link_pred.py
def __init__(
    self,
    pred_index_mat: ArrayLike,
    edge_label_index: EdgeLabelIndex,
    edge_label_weight: Optional[ArrayLike] = None,
):
    self.pred_index_mat = _host(pred_index_mat).astype(np.int64)
    if self.pred_index_mat.ndim == 1:
        self.pred_index_mat = self.pred_index_mat.reshape(-1, 1)
    if isinstance(edge_label_index, (tuple, list)):
        row, col = edge_label_index
    else:
        row, col = _host(edge_label_index)
    self.row = _host(row).reshape(-1).astype(np.int64)
    self.col = _host(col).reshape(-1).astype(np.int64)
    self.edge_label_weight = None
    if edge_label_weight is not None:
        weight = _host(edge_label_weight).reshape(-1).astype(np.float64)
        pos_mask = weight > 0
        self.edge_label_weight = weight[pos_mask]
        self.row, self.col = self.row[pos_mask], self.col[pos_mask]

pred_rel_mat property

pred_rel_mat: ndarray

Relevance of the \(k\)-th prediction of every example: a boolean matrix, or the edge label weights if given.

label_count property

label_count: ndarray

Number of ground-truth labels of every example.

label_weight_sum property

label_weight_sum: ndarray

Sum of the edge label weights of every example.

edge_label_weight_pos property

edge_label_weight_pos: Optional[ndarray]

Position of every ground-truth label within its example, in decreasing order of weight.

from mlx_graphs.metrics import LinkPredMetricData · mlx_graphs/metrics/link_pred.py