開始使用免費開始

真實世界的成本分析

本練習仍使用信用資料集。回想一下,此資料集中「positive」代表「壞信用」,也就是違約的客戶;「negative」代表持續正常還款的客戶。銀行經理告訴你,對每位「風險良好」的客戶平均可獲利 10K,但每位「風險不佳」的客戶會損失 150K。你的演算法將用於篩選申請者,因此被標記為「negative」的會核貸,而「positive」的會被婉拒。請計算你的分類器造成的總成本。可用的資料為 X_trainX_testy_trainy_test。可使用的函式與類別有 confusion_matrix()f1_score()precision_score()RandomForestClassifier()

本練習屬於課程

在 Python 設計機器學習工作流程

檢視課程

練習說明

  • 將隨機森林分類器擬合在訓練資料上。
  • 用它為測試資料產生標籤。
  • confusion_matrix() 取出假陰性與假陽性。你需要先將矩陣攤平成一維。
  • 把「好」客戶誤判為「壞」表示銀行錯失 10K 的利潤;把「壞」客戶誤判為「好」表示因客戶違約導致銀行損失 150K。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Fit a random forest classifier to the training data
clf = ____(random_state=2).fit(____, ____)

# Label the test data
preds = clf.____(____)

# Get false positives/negatives from the confusion matrix
tn, ____, ____, tp = confusion_matrix(y_test, preds).____()

# Now compute the cost using the manager's advice
cost = fp*____ + fn*____
編輯並執行程式碼