scikit-learn 的 KFold()
您刚刚运行了一位同事的代码,该代码训练了一个随机森林模型并计算了样本外准确率。您注意到同事的代码没有设置随机种子,结果您发现的错误与同事报告的完全不同。
为了更好地估计这个随机森林模型在新数据上的准确率,您决定生成一些用于 KFold 交叉验证的索引。
本练习是课程的一部分
Python 中的模型验证
练习说明
- 调用
KFold()方法,设置为 5 折划分、启用洗牌,并将随机种子设为 1111。 - 对
X使用KFold的split()方法。 - 打印训练索引列表和验证索引列表中索引的数量。
交互式实操练习
通过完成这段示例代码来试试这个练习。
from sklearn.model_selection import KFold
# Use KFold
kf = KFold(____, ____, ____)
# Create splits
splits = kf.____(____)
# Print the number of indices
for train_index, val_index in splits:
print("Number of training indices: %s" % len(____))
print("Number of validation indices: %s" % len(____))