對信用資料做 one-hot 編碼
現在要把非數值欄位準備好,才能加入你的 LogisticRegression() 模型。
用 one-hot 編碼建立新欄位後,你可以把它們和數值欄位串接,產生新的資料框,之後在整門課中都會用它來預測違約機率。
記得只對非數值欄位做 one-hot 編碼。若對數值欄位這樣做,資料集會變得非常寬!
信用貸款資料 cr_loan_clean 已經載入到工作區中。
本練習屬於課程
以 Python 進行信用風險建模
練習說明
- 為所有數值欄位建立名為
cred_num的資料集,並為非數值欄位建立名為cred_str的資料集。 - 對
cred_str做 one-hot 編碼,建立名為cred_str_onehot的新資料集。 - 將
cred_num與新的 one-hot 編碼資料合併,並將結果存為cr_loan_prep。 - 印出新資料集的欄位。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create two data sets for numeric and non-numeric data
____ = ____.select_dtypes(exclude=['object'])
____ = ____.select_dtypes(include=['object'])
# One-hot encode the non-numeric columns
____ = pd.____(____)
# Union the one-hot encoded columns to the numeric ones
____ = pd.concat([____, ____], axis=1)
# Print the columns in the new data set
print(____.columns)