Esempio n. 1
0
def test_roc_auc_score():
    y_true = np.array([0, 0, 1, 1])
    y_pred = np.array([0.1, 0.4, 0.35, 0.8])
    assert_almost_equal(roc_auc_score(y_true, y_pred),
                        sklearn_roc_auc_score(y_true, y_pred))

    y_true = np.array([0, 0, 1, 1, 0])
    y_pred = np.array([0.8, 0.4, 0.4, 0.8, 0.8])
    assert_almost_equal(roc_auc_score(y_true, y_pred),
                        sklearn_roc_auc_score(y_true, y_pred))
Esempio n. 2
0
 def test_roc_auc_final(self):
     rng = np.random.RandomState(5)
     N = 10**4
     pred = np.arange(0.0, 1.0, step=1. / N)
     obs = np.array(rng.rand(N) < pred, dtype=int)
     self.assertAlmostEqual(roc_auc(obs, pred),
                            sklearn_roc_auc_score(obs, pred))
Esempio n. 3
0
def test_roc_auc_score_random(n_samples, dtype):

    y_true, _, _, _ = generate_random_labels(
        lambda rng: rng.randint(0, 2, n_samples).astype(dtype))

    y_pred, _, _, _ = generate_random_labels(
        lambda rng: rng.randint(0, 1000, n_samples).astype(dtype))

    auc = roc_auc_score(y_true, y_pred)
    skl_auc = sklearn_roc_auc_score(y_true, y_pred)
    assert_almost_equal(auc, skl_auc)
Esempio n. 4
0
def roc_auc_score(predictions, targets):
    r""""""
    predictions = predictions.view(-1, 1).numpy()
    targets = targets.view(-1, 1).numpy()

    return sklearn_roc_auc_score(targets, predictions)
Esempio n. 5
0
    y_true = np.array([
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
    ])
    pred_prob = np.array([
        0.7, 0.9, 0.2, 0.8, 0.3, 0.64, 0.53, 0.12, 0.34, 0.52, 0.98, 0.03,
        0.32, 0.4, 0.8, 0.21, 0.01, 0.67, 0.32, 0.08, 0.05, 0.8, 0.34, 0.8
    ])

    import matplotlib.pyplot as plt
    Ps, Rs = precision_recall_curve(y_true, pred_prob)
    plt.plot(Rs, Ps, label='tinyml')

    from sklearn.metrics import precision_recall_curve as sklearn_pr_curve
    Ps, Rs, _ = sklearn_pr_curve(y_true, pred_prob)
    plt.plot(Rs, Ps, label='sklearn')
    plt.legend()
    plt.title('PRC')
    plt.show()

    FPR, TPR = roc_curve(y_true, pred_prob)
    plt.plot(FPR, TPR, label='tinyml')
    print('tinyml_auc:', roc_auc_score(y_true, pred_prob))
    from sklearn.metrics import roc_curve as sklearn_roc_curve
    from sklearn.metrics import roc_auc_score as sklearn_roc_auc_score
    FPR, TPR, _ = sklearn_roc_curve(y_true, pred_prob)
    plt.plot(FPR, TPR, label='sklearn')
    plt.legend()
    plt.title('ROC')
    plt.show()
    print('sklearn auc:', sklearn_roc_auc_score(y_true, pred_prob))