預測違約機率
所有資料前處理都已完成,現在要開始建立違約機率的預測。你想在資料上訓練一個 LogisticRegression() 模型,並檢視它如何預測違約機率。
為了更清楚理解 predict_proba 的輸出,你應該把範例資料列與模型預測的違約機率並列查看。前 5 筆預測和實際的 loan_status 值相比起來如何?
資料集 cr_loan_prep 以及 X_train、X_test、y_train 和 y_test 都已經載入到工作環境中。
本練習屬於課程
以 Python 進行信用風險建模
練習說明
- 在訓練資料上訓練一個羅吉斯回歸模型,並將其儲存為
clf_logistic。 - 對測試資料使用
predict_proba()產生預測,並將結果儲存到preds。 - 建立兩個資料框
preds_df與true_df,用來存放前 5 筆預測與實際的loan_status值。 - 使用
.concat()將true_df與preds_df合併後印出為同一組。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Train the logistic regression model on the training data
____ = ____(solver='lbfgs').____(____, np.ravel(____))
# Create predictions of probability for loan status using test data
____ = clf_logistic.____(____)
# Create dataframes of first five predictions, and first five true labels
____ = pd.DataFrame(____[:,1][0:5], columns = ['prob_default'])
____ = y_test.____()
# Concatenate and print the two data frames for comparison
print(pd.____([true_df.reset_index(drop = True), preds_df], axis = 1))