成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

時間序列的蒙特卡羅交叉驗證

開發 前端
交叉驗證應用于時間序列需要注意是要防止泄漏和獲得可靠的性能估計本文將介紹蒙特卡洛交叉驗證。這是一種流行的TimeSeriesSplits方法的替代方法。

交叉驗證應用于時間序列需要注意是要防止泄漏和獲得可靠的性能估計本文將介紹蒙特卡洛交叉驗證。這是一種流行的TimeSeriesSplits方法的替代方法。

時間序列交叉驗證

TimeSeriesSplit通常是時間序列數據進行交叉驗證的首選方法。下圖1說明了該方法的操作方式。可用的時間序列被分成幾個大小相等的折疊。然后每一次折首先被用來測試一個模型,然后重新訓練它。除了第一折只用于訓練。

圖片

使用TimeSeriesSplit進行交叉驗證的主要好處如下:

  • 它保持了觀察的順序。這個問題在有序數據集(如時間序列)中非常重要。
  • 它生成了很多拆分 。幾次拆分后可以獲得更穩健的評估。如果數據集不大,這一點尤其重要。

TimeSeriesSplit的主要缺點是跨折疊的訓練樣本量是不一致的。這是什么意思?

假設將該方法應用于圖1所示的5次分折。在第一次迭代中,所有可用觀測值的20%用于訓練。但是,這個數字在上次迭代中是80%。因此,初始迭代可能不能代表完整的時間序列。這個問題會影響性能估計。

那么如何解決這個問題?

蒙特卡羅交叉驗證

蒙特卡羅交叉驗證(MonteCarloCV)是一種可以用于時間序列的方法。這個想法是在不同的隨機起點來獲取一個時間周期的數據,下面是這種方法的可視化描述:

像TimeSeriesSplit一樣,MonteCarloCV也保留了觀測的時間順序。它還會保留多次重復估計過程。

MonteCarloCV與TimeSeriesSplit的區別主要有兩個方面:

  • 對于訓練和驗證樣本量,使用TimeSeriesSplit時訓練集的大小會增加。在MonteCarloCV中,訓練集的大小在每次迭代過程中都是固定的,這樣可以防止訓練規模不能代表整個數據;
  • 隨機的分折,在MonteCarloCV中,驗證原點是隨機選擇的。這個原點標志著訓練集的結束和驗證的開始。在TimeSeriesSplit的情況下,這個點是確定的。它是根據迭代次數預先定義的。

MonteCarloCV最初由Picard和Cook使用。詳細信息可以查看參考文獻。

經過詳細研究MonteCarloCV。這包括與TimeSeriesSplit等其他方法的比較。MonteCarloCV可以獲得更好的估計,所以我一直在使用它。你可以在參考文獻[2]中查看完整的研究。

不幸的是,scikit-learn不提供MonteCarloCV的實現。所以,我們決定自己手動實現它:

 from typing import List, Generator

import numpy as np

from sklearn.model_selection._split import _BaseKFold
from sklearn.utils.validation import indexable, _num_samples


class MonteCarloCV(_BaseKFold):

def __init__(self,
n_splits: int,
train_size: float,
test_size: float,
gap: int = 0):
"""
Monte Carlo Cross-Validation

Holdout applied in multiple testing periods
Testing origin (time-step where testing begins) is randomly chosen according to a monte carlo simulation

:param n_splits: (int) Number of monte carlo repetitions in the procedure
:param train_size: (float) Train size, in terms of ratio of the total length of the series
:param test_size: (float) Test size, in terms of ratio of the total length of the series
:param gap: (int) Number of samples to exclude from the end of each train set before the test set.
"""

self.n_splits = n_splits
self.n_samples = -1
self.gap = gap
self.train_size = train_size
self.test_size = test_size
self.train_n_samples = 0
self.test_n_samples = 0

self.mc_origins = []

def split(self, X, y=None, groups=None) -> Generator:
"""Generate indices to split data into training and test set.
Parameters
----------
X : array-like of shape (n_samples, n_features)
Training data, where `n_samples` is the number of samples
and `n_features` is the number of features.
y : array-like of shape (n_samples,)
Always ignored, exists for compatibility.
groups : array-like of shape (n_samples,)
Always ignored, exists for compatibility.
Yields
------
train : ndarray
The training set indices for that split.
test : ndarray
The testing set indices for that split.
"""

X, y, groups = indexable(X, y, groups)
self.n_samples = _num_samples(X)

self.train_n_samples = int(self.n_samples * self.train_size) - 1
self.test_n_samples = int(self.n_samples * self.test_size) - 1

# Make sure we have enough samples for the given split parameters
if self.n_splits > self.n_samples:
raise ValueError(
f'Cannot have number of folds={self.n_splits} greater'
f' than the number of samples={self.n_samples}.'
)
if self.train_n_samples - self.gap <= 0:
raise ValueError(
f'The gap={self.gap} is too big for number of training samples'
f'={self.train_n_samples} with testing samples={self.test_n_samples} and gap={self.gap}.'
)

indices = np.arange(self.n_samples)

selection_range = np.arange(self.train_n_samples + 1, self.n_samples - self.test_n_samples - 1)

self.mc_origins = \
np.random.choice(a=selection_range,
size=self.n_splits,
replace=True)

for origin in self.mc_origins:
if self.gap > 0:
train_end = origin - self.gap + 1
else:
train_end = origin - self.gap
train_start = origin - self.train_n_samples - 1

test_end = origin + self.test_n_samples

yield (
indices[train_start:train_end],
indices[origin:test_end],
)

def get_origins(self) -> List[int]:
return self.mc_origins

MonteCarloCV接受四個參數:

  • n_splitting:分折或迭代的次數。這個值趨向于10;
  • training_size:每次迭代時訓練集的大小與時間序列大小的比值;
  • test_size:類似于training_size,但用于驗證集;
  • gap:分離訓練集和驗證集的觀察數。與TimeSeriesSplits一樣,此參數的值默認為0(無間隙)。

每次迭代的訓練和驗證大小取決于輸入數據。我發現一個0.6/0.1的分區工作得很好。也就是說,在每次迭代中,60%的數據被用于訓練。10%的觀察結果用于驗證。

實際使用的例子

下面是配置的一個例子:

 from sklearn.datasets import make_regression
from src.mccv import MonteCarloCV

X, y = make_regression(n_samples=120)

mccv = MonteCarloCV(n_splits=5,
train_size=0.6,
test_size=0.1,
gap=0)

for train_index, test_index in mccv.split(X):
print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]

該實現也與scikit-learn兼容。以下是如何結合GridSearchCV:

from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import GridSearchCV

model = RandomForestRegressor()
param_search = {'n_estimators': [10, 100]}

gsearch = GridSearchCV(estimator=model, cv=mccv, param_grid=param_search)
gsearch.fit(X, y)

我希望你發現MonteCarloCV有用!


責任編輯:華軒 來源: DeepHub IMBA
相關推薦

2015-09-06 10:34:54

蒙特卡洛阮一峰

2023-11-06 18:32:04

交叉驗證機器學習

2025-01-22 07:59:59

2017-06-26 10:43:22

互聯網

2024-10-30 08:23:07

2025-01-15 11:25:35

2022-03-28 20:59:17

交叉驗證模型

2022-08-14 16:04:15

機器學習數據集算法

2022-11-03 15:18:20

Python組件算法

2023-10-13 15:34:55

時間序列TimesNet

2022-11-14 14:36:59

數據集Python自相關

2022-05-12 11:12:46

MongoDB索引元數據

2024-11-15 15:20:00

模型數據

2017-05-08 14:42:36

2024-05-09 16:23:14

2020-10-27 10:13:06

Python時間序列代碼

2023-01-05 16:36:55

2024-06-03 11:05:11

2020-02-18 16:07:17

物聯網表存儲數據庫

2022-08-16 09:00:00

機器學習人工智能數據庫
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 色婷婷av一区二区三区软件 | 成人在线视频网站 | 国产精品久久久久久久久久久久午夜片 | 亚洲欧美另类在线观看 | 毛片视频免费观看 | 日本一道本视频 | 久久久久久亚洲 | 午夜三区| 国产精品美女一区二区 | 国产 日韩 欧美 在线 | 国产成人综合亚洲欧美94在线 | 亚洲乱码国产乱码精品精98午夜 | 午夜免费视频 | 国产精品久久久久久吹潮 | 热re99久久精品国产99热 | 成人小视频在线观看 | 日韩在线视频网址 | 久久艹免费视频 | aa级毛片毛片免费观看久 | 欧美高清视频在线观看 | 欧美三级电影在线播放 | 国产乱码精品一区二区三区中文 | 狠狠夜夜 | 俺去俺来也www色官网cms | 欧美在线天堂 | 国产在线中文字幕 | 国产69精品久久99不卡免费版 | 羞羞视频网站免费看 | 色男人天堂av | 国产乱码精品一区二区三区中文 | 国产精品日韩欧美一区二区 | 精品免费av | 久久偷人 | 欧美亚洲国产一区二区三区 | 伊人二区 | 成人在线一区二区 | 狠狠躁天天躁夜夜躁婷婷老牛影视 | 午夜丁香视频在线观看 | 色综合久久久久 | 九色视频网站 | 天天干夜夜操 |