Precision 與 Recall
Precision 與 recall 都與前一個單元提到的四種結果相關,是任何機器學習模型的重要評估指標。理想上,廣告 CTR 模型應具有高 precision(廣告花費的投資報酬率高)與高 recall(能觸及相關受眾)。雖然可以手動計算 precision 與 recall,sklearn 已提供方便的實作,能輕鬆嵌入既有流程。在本練習中,你將建立一個決策樹,並計算 precision 與 recall。
工作環境中已可使用將 pandas 載入為 pd,且樣本 DataFrame 已載入為 df。特徵放在 X,目標變數放在 y。另外,sklearn.metrics 中的 precision_score() 與 recall_score() 也已可使用。
本練習屬於課程
用 Python 透過機器學習預測 CTR
練習說明
- 取得
X與y的訓練集與測試集切分。 - 定義一個決策樹分類器,訓練模型並產生預測
y_pred。 - 使用
sklearn提供的實作來取得 precision 與 recall 分數。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Set up training and testing split
X_train, X_test, y_train, y_test = ____(
____, ____, test_size = .2, random_state = 0)
# Create classifier and make predictions
clf = ____
y_pred = clf.____(____, _____).____(X_test)
# Evaluate precision and recall
prec = ____(y_test, ____, average = 'weighted')
recall = ____(y_test, ____, average = 'weighted')
print("Precision: %s, Recall: %s" %(prec, recall))