在Python中实现精度和召回

时间:2017-04-01 14:52:25

标签: python machine-learning metrics

我试图了解如何自行实现精度召回。鉴于clf是分类器,y_test[i]是真值,X_test[i].reshape(1,-1)是预测值,这些定义是否正确?

精密

def testPosValueMetric(clf, X_test, y_test):
    success =0
    fail = 0
    for i in range(len(y_test)):
        if y_test[i] == 1:
            if clf.predict(X_test[i].reshape(1,-1)) == 1:
                success += 1
            else:
                fail +=1

    return (success/(success+fail))

提取

def recall(clf, X_test, y_test):
    tp =0
    fp = 0
    for i in range(len(y_test)):
        if y_test[i] == 1:
            if clf.predict(X_test[i].reshape(1,-1)) == 1:
                tp += 1
            else:
                fp +=1

    tn =0
    fn = 0
    for i in range(len(y_test)):
        if y_test[i] == 0:
            if clf.predict(X_test[i].reshape(1,-1)) == 0:
                tn += 1
            else:
                fn +=1

    return (tp/(tp+fn))

0 个答案:

没有答案