Skip to content

Instantly share code, notes, and snippets.

@Polaris000
Created November 4, 2022 16:52
Show Gist options
  • Select an option

  • Save Polaris000/a10e77a3855455d9cbb02046e6e4e254 to your computer and use it in GitHub Desktop.

Select an option

Save Polaris000/a10e77a3855455d9cbb02046e6e4e254 to your computer and use it in GitHub Desktop.

Revisions

  1. Polaris000 created this gist Nov 4, 2022.
    32 changes: 32 additions & 0 deletions numpy_implemenation_without_df.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    def auc_recall_at_k_np_no_df(y_true, y_conf):
    """
    Experiment #3:
    --------------
    Compute AUC under the Recall@k curve using numpy's
    functions. We do away with the conf_df dataframe
    as well.
    y_true: A numpy array of expected predictions
    y_conf: A numpy array of the model's confidence
    scores for each datapoint
    Returns: AUC-Recall@k (float)
    """

    # if there are no positive targets (good leads),
    # auc becomes invalid
    if (y_true == 1).sum() == 0:
    return np.nan

    ranking = y_true[np.argsort(y_conf)[::-1]]

    # calculating recall@k based on sorted ranking
    recall_at_k = (ranking == 1).cumsum() / (ranking == 1).sum()

    # calculating ideal recall@k
    ideal_recall_at_k = np.minimum(
    np.ones(len(ranking)),
    np.array(list(range(1, len(ranking) + 1)))/ (ranking == 1).sum()
    )

    return np.trapz(recall_at_k) / np.trapz(ideal_recall_at_k)