開始使用免費開始

以時間為基礎的交叉驗證

最後,來視覺化 scikit-learn 中「時間序列交叉驗證反覆器」的行為。使用這個物件再走訪一次你的資料,並在每次反覆運算時,把用來訓練模型的訓練資料視覺化。

工作區已提供線性迴歸 model 物件的實例。另外,Xy(訓練資料)兩個陣列也已可用。

本練習屬於課程

Python 的時間序列資料機器學習

檢視課程

練習說明

  • sklearn.model_selection 匯入 TimeSeriesSplit
  • 建立一個包含 10 個分割的時間序列交叉驗證反覆器。
  • 逐一迭代每個 CV 分割。在每次迭代時,視覺化該次用來訓練模型的輸入資料值。

動手互動練習

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

# Import TimeSeriesSplit
____

# Create time-series cross-validation object
cv = ____

# Iterate through CV splits
fig, ax = plt.subplots()
for ii, (tr, tt) in enumerate(cv.split(X, y)):
    # Plot the training data on each iteration, to see the behavior of the CV
    ax.plot(tr, ii + y[tr])

ax.set(title='Training data on each CV iteration', ylabel='CV iteration')
plt.show()
編輯並執行程式碼