开始使用免费开始使用

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:", ____)
编辑并运行代码