在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))
在使用除法获得最终结果时要注意,因为现在使用整数除法在某些情况下也要注意被零除。