我想以与precision_recall_curve给出精度和召回率相同的方式来获得特殊性。

precisions, recalls, thresholds = sklearn.metrics.precision_recall_curve(ground_truth, predictions)

我该如何实现?

最佳答案

因此,我查看了sklearn.metrics.precision_recall_curvehttps://github.com/scikit-learn/scikit-learn/blob/2e90b897768fd360ef855cb46e0b37f2b6faaf72/sklearn/metrics/_ranking.py)的源代码,并对其进行了更改以满足自己的需求。

import numpy as np
from sklearn.metrics.ranking import _binary_clf_curve

def specificity_sensitivity_curve(y_true, probas_pred):
    """
    Compute specificity-sensitivity pairs for different probability thresholds.
    For reference, see 'precision_recall_curve'
    """
    fps, tps, thresholds = _binary_clf_curve(y_true, probas_pred)
    sensitivity = tps / tps[-1]
    specificity = (fps[-1] - fps) / fps[-1]
    last_ind = tps.searchsorted(tps[-1])
    sl = slice(last_ind, None, -1)
    return np.r_[specificity[sl], 1], np.r_[sensitivity[sl], 0], thresholds[sl]

10-08 20:02