Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中实现精确性和召回_Python_Machine Learning_Metrics - Fatal编程技术网

在Python中实现精确性和召回

在Python中实现精确性和召回,python,machine-learning,metrics,Python,Machine Learning,Metrics,我正在努力理解如何自己实现精确性和召回。假设clf是一个分类器,y\u测试[i]是真值,X\u测试[i]。重塑(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].res

我正在努力理解如何自己实现精确性和召回。假设
clf
是一个分类器,
y\u测试[i]
是真值,
X\u测试[i]。重塑(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))

在使用除法获得最终结果时要注意,因为现在使用整数除法在某些情况下也要注意被零除。