2017年9月15日 星期五

[Python] 機器學習筆記 - 使用 準確率/召回率 (Precision-Recall) 評估分析成果

研究所時,算是第一次接觸這名詞,老闆的研究領域是 Search Engines ,用來評估索引成果好不好。最近則打算用在機器學習的成果分析,卻想不起當時老闆用來解釋索引成果的案例,還是容易忘記 XD 網路上打滾一下,發現這篇寫的廣告投放實際案例很好懂,也不容易忘,建議可以逛一下:準確率(Precision)與召回率(Recall)

回到本文,單純紀錄如何用既有函式庫計算:

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

沒有留言:

張貼留言