開始使用免費開始

類別型編碼

你的同事已經用 LabelEncoder() 把信用資料集中的多數欄位轉成數值,但遺漏了 credit_history,這個欄位記錄申請者的信用歷史。你想建立兩個版本的資料集:一個使用 LabelEncoder(),另一個使用獨熱編碼,以便比較。特徵矩陣已以 credit 提供。LabelEncoder() 已預先載入,且 pandaspd 匯入。

本練習屬於課程

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

檢視課程

練習說明

  • 使用 LabelEncoder()credit_history 進行編碼。
  • 將結果串接回原始資料框。
  • 透過將獨熱編碼產生的 dummies 串接到原始資料框,建立新的資料框。
  • 確認獨熱編碼產生的欄數比標籤編碼更多。

動手互動練習

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

# Create numeric encoding for credit_history
credit_history_num = ____.____(
  credit[____])

# Create a new feature matrix including the numeric encoding
X_num = pd.concat([X, pd.Series(____)], ____)

# Create new feature matrix with dummies for credit_history
X_hot = pd.concat(
  [X, ____.____(credit[____])], ____)

# Compare the number of features of the resulting DataFrames
print(X_hot.shape[____] > X_num.shape[____])
編輯並執行程式碼