回到本文,單純紀錄如何用既有函式庫計算:
import numpy as np
from sklearn.metrics import average_precision_score, precision_score, recall_score
# Classification metrics can't handle a mix of binary and continuous targets
#y = np.array([0, 0, 1, 1])
#scores = np.array([0.1, 0.3, 0.2, 0.8])
y = [0, 0, 1, 1]
scores = [0, 1, 1, 1]
#print(precision_score(y, scores, average='macro'))
#print(recall_score(y, scores, average='macro'))
#print(average_precision_score(y, scores))
#import sys
#sys.exit(0)
print('precision: %0.2f, recall: %0.2f, score: %0.2f' % (
precision_score(y, scores, average='macro'),
recall_score(y, scores, average='macro'),
average_precision_score(y, scores)
))
成果:
precision: 0.83, recall: 0.75, score: 0.67
需要更詳細的範例,請參考:http://scikit-learn.org/stable/auto_examples/model_selection/plot_precision_recall.html
沒有留言:
張貼留言