开始使用免费开始使用

不打乱顺序的交叉验证

现在,请使用块状交叉验证(不打乱所有数据点)重新拟合模型。在这种设置下,相邻的时间点会被保留在一起。您认为在每一次交叉验证循环中,模型的预测会是什么样子?

您的工作区中已提供线性回归 model 实例。同时也提供了数组 Xy(训练数据)。

本练习是课程的一部分

Python 中的时间序列机器学习

查看课程

练习说明

  • 实例化另一个交叉验证对象,这次使用 KFold 交叉验证,分成 10 折且不打乱顺序。
  • 迭代该对象,使用训练索引拟合模型,并使用测试索引生成预测。
  • 使用我们提供的辅助函数(visualize_predictions())可视化各个 CV 拆分下的预测结果。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Create KFold cross-validation object
from sklearn.model_selection import KFold
cv = ____(n_splits=____, shuffle=____)

# Iterate through CV splits
results = []
for tr, tt in cv.split(X, y):
    # Fit the model on training data
    model.fit(____)
    
    # Generate predictions on the test data and collect
    prediction = model.predict(____)
    results.append((prediction, tt))
    
# Custom function to quickly visualize predictions
visualize_predictions(results)
编辑并运行代码