def average_precision_score(y_true, y_score): """ Compute average precision score using precision and recall computed from cuml. """ precision, recall, _ = precision_recall_curve(y_true, y_score) # return step function integral return -cp.sum(cp.diff(recall) * cp.array(precision)[:-1])
def test_precision_recall_curve_at_limits(): y_true = np.array([0., 0., 0.], dtype=np.float) y_pred = np.array([0., 0.5, 1.], dtype=np.float) err_msg = ("precision_recall_curve cannot be used when " "y_true is all zero.") with pytest.raises(ValueError, match=err_msg): precision_recall_curve(y_true, y_pred) y_true = np.array([0., 0.5, 1.0], dtype=np.float) y_pred = np.array([0., 0.5, 1.], dtype=np.float) err_msg = ("Continuous format of y_true " "is not supported.") with pytest.raises(ValueError, match=err_msg): precision_recall_curve(y_true, y_pred)
def plot_pr(label, y_scores): ap = metrics(label, y_scores)[1] precision, recall, _ = precision_recall_curve(label, y_scores) plt.plot(recall, precision, label='AP = ' + str(np.round(ap, 2))) plt.ylabel('Precision') plt.xlabel('Recall') plt.legend(loc='best') plt.title('Area under PR curve')
def test_precision_recall_curve(): y_true = np.array([0, 0, 1, 1]) y_score = np.array([0.1, 0.4, 0.35, 0.8]) precision_using_sk, recall_using_sk, thresholds_using_sk = \ sklearn_precision_recall_curve( y_true, y_score) precision, recall, thresholds = precision_recall_curve(y_true, y_score) assert array_equal(precision, precision_using_sk) assert array_equal(recall, recall_using_sk) assert array_equal(thresholds, thresholds_using_sk)
def test_precision_recall_curve_random(n_samples, dtype): y_true, _, _, _ = generate_random_labels( lambda rng: rng.randint(0, 2, n_samples).astype(dtype)) y_score, _, _, _ = generate_random_labels( lambda rng: rng.randint(0, 1000, n_samples).astype(dtype)) precision_using_sk, recall_using_sk, thresholds_using_sk = \ sklearn_precision_recall_curve( y_true, y_score) precision, recall, thresholds = precision_recall_curve(y_true, y_score) assert array_equal(precision, precision_using_sk) assert array_equal(recall, recall_using_sk) assert array_equal(thresholds, thresholds_using_sk)