開始使用免費開始

對特定欄位做 one-hot 編碼

一家在地二手車商希望你協助預測車輛的售價。如果你對整個 used_cars 資料集做 one-hot 編碼,新的資料集會有超過 1,200 個欄位。你擔心這在訓練用來預測價格的機器學習模型時會造成問題。因此你決定採用更簡單的方法,只對少數幾個欄位進行 one-hot 編碼。

本練習屬於課程

在 Python 中處理類別資料

檢視課程

練習說明

  • 建立新的資料集 used_cars_simple,對這些欄位進行 one-hot 編碼(依此順序):"manufacturer_name""transmission"
  • 將所有新欄位的前綴設定為 "dummy",以方便你過濾出新建立的欄位。

動手互動練習

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

# Create one-hot encoding for just two columns
used_cars_simple = pd.____(
  used_cars,
  # Specify the columns from the instructions
  ____,
  # Set the prefix
  ____
)

# Print the shape of the new dataset
print(used_cars_simple.shape)
編輯並執行程式碼