napkinxc.measures.psprecision_at_k

napkinxc.measures.psprecision_at_k(Y_true, Y_pred, inv_ps, k=5, normalize=True)[source]

Calculate Propensity Scored Precision (PSP) at 1-k places. This measure can be also called weighted precision. PSP at k is defined as:

\[psp@k = \frac{1}{k} \sum_{l \in \text{rank}_k(\hat{\pmb{y}})} q_l \hat{y_l},\]

where \(\pmb{y} \in {0, 1}^m\) is ground truth label vector, \(\hat{\pmb{y}} \in \mathbb{R}^m\) is predicted labels score vector, \(\text{rank}_k(\hat{\pmb{y}})\) returns the \(k\) indices of \(\hat{\pmb{y}}\) with the largest values, ordered in descending order, and \(\pmb{q}\) is vector of inverse propensities.

Parameters:
  • Y_true (ndarray, csr_matrix, list[list|set[int|str]]) – Ground truth provided as a matrix with non-zero values for true labels or a list of lists or sets of true labels.

  • Y_pred (ndarray, csr_matrix, list[list[int|str]], list[list[tuple[int|str, float]]) – Predicted labels provided as a matrix with scores or list of rankings as a list of labels or tuples of labels with scores (label, score). In the case of the matrix, the ranking will be calculated by sorting scores in descending order.

  • inv_ps (ndarray, list, dict) – Inverse propensity (propensity scores) for each label (label weights). In case of text labels needs to be a dict.

  • k (int, optional) – Calculate at places from 1 to k, defaults to 5

  • normalize (bool, optional) – Normalize result to [0, 1] range by dividing it by best possible value, commonly used to report results, defaults to True

Returns:

Values of PSP at 1-k places.

Return type:

ndarray