開始使用免費開始

KFold 交叉驗證

在處理機器學習模型時,必須在看不見的新資料上評估其效能,並確保評估方式穩健。常見的方法之一是 k 折交叉驗證。這個練習中,你將觀察 k 折交叉驗證如何把資料集切分成訓練集與測試集。KFold 已為你匯入,同時也提供了心臟疾病資料集的特徵 heart_disease_df_X

本練習屬於課程

端到端機器學習

檢視課程

練習說明

  • 建立一個 KFold 物件,設定 n_splits=5shuffle=Truerandom_state=42
  • 使用 kfold.split() 進行資料切分。
  • 列印訓練與測試切分中的資料筆數。

動手互動練習

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

# Create a KFold object
kfold = ____(____, ____, ____)

# Get the train and test data from the first split from the shuffled KFold
train_data_split, test_data_split = next(____.____(____))

# Print out the number of datapoints in the train and test splits
print("Number of training datapoints in heart_disease_df_X:", ____)
print("Number of training datapoints in split:", ____)
print("Number of testing datapoints in split:", ____)
編輯並執行程式碼