日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
時間序列的蒙特卡羅交叉驗證

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

成都創(chuàng)新互聯(lián)公司專注于通州企業(yè)網(wǎng)站建設(shè),成都響應式網(wǎng)站建設(shè)公司,商城網(wǎng)站開發(fā)。通州網(wǎng)站建設(shè)公司,為通州等地區(qū)提供建站服務。全流程定制開發(fā),專業(yè)設(shè)計,全程項目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務

時間序列交叉驗證

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

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

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

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

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

那么如何解決這個問題?

蒙特卡羅交叉驗證

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

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

MonteCarloCV與TimeSeriesSplit的區(qū)別主要有兩個方面:

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

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

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

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

 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接受四個參數(shù):

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

每次迭代的訓練和驗證大小取決于輸入數(shù)據(jù)。我發(fā)現(xiàn)一個0.6/0.1的分區(qū)工作得很好。也就是說,在每次迭代中,60%的數(shù)據(jù)被用于訓練。10%的觀察結(jié)果用于驗證。

實際使用的例子

下面是配置的一個例子:

 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]

該實現(xiàn)也與scikit-learn兼容。以下是如何結(jié)合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)

我希望你發(fā)現(xiàn)MonteCarloCV有用!


網(wǎng)站欄目:時間序列的蒙特卡羅交叉驗證
當前路徑:http://m.5511xx.com/article/dpspeoo.html