二元特徵編碼
轉換資料型別是資料前處理中的重要一步。在本練習中,你要將 'Vmail_Plan' 與 'Churn' 這兩個特徵中的 'yes' 指定為 1,'no' 指定為 0。
在影片中你看到兩種作法:一種使用 pandas,另一種使用 scikit-learn。對於這類直接的任務,建議採用 pandas,本題也會用這個方法。若你要建立機器學習的 pipeline(超出本課程範圍),可以再探索使用 LabelEncoder()。做資料科學時,要記得通常有不只一種方法可以完成同一件事,你需要選擇最適合自己應用情境的作法。
本練習屬於課程
行銷分析:用 Python 預測客戶流失
練習說明
- 在
telco的'Vmail_Plan'欄位中,將'no'取代為0,'yes'取代為1。 - 對
'Churn'欄位也做相同的轉換。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Replace 'no' with 0 and 'yes' with 1 in 'Vmail_Plan'
telco['Vmail_Plan'] = telco['____'].____(____)
# Replace 'no' with 0 and 'yes' with 1 in 'Churn'
telco['Churn'] = ____
# Print the results to verify
print(telco['Vmail_Plan'].head())
print(telco['Churn'].head())