開始使用免費開始

探索流失率並切分資料

延續你在第 1 章的總覽,本課將更深入準備機器學習所需的資料,以進行流失預測。你會先探索流失分佈,並在建模前將資料切分為訓練與測試。透過這個步驟,你能了解流失率如何分佈,並先行前處理資料,之後在訓練集上建立模型,並用未使用的測試資料評估效能。

電信資料集已載入為名為 telcompandas DataFrame。目標變數欄位為 Churn

本練習屬於課程

Python 的行銷機器學習

檢視課程

練習說明

  • 列印 Churn 欄位中的唯一值。
  • 計算各流失群組的比例大小。
  • 匯入用於將資料切分為訓練與測試的函式。
  • 將資料切分為 75% 訓練集與 25% 測試集。

動手互動練習

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

# Print the unique Churn values
print(___(telcom['Churn']))

# Calculate the ratio size of each churn group
telcom.___(['Churn']).size() / telcom.shape[0] * 100

# Import the function for splitting data to train and test
from sklearn.model_selection import ___

# Split the data into train and test
train, test = ___(telcom, test_size = .25)
編輯並執行程式碼