我搜索了sklearn文档 TimeSeriesSplit和交叉验证文档,但我找不到一个有效的例子.
我正在使用sklearn版本0.19.
这是我的设置
import xgboost as xgb
from sklearn.model_selection import TimeSeriesSplit
from sklearn.grid_search import GridSearchCV
import numpy as np
X = np.array([[4, 5, 6, 1, 0, 2], [3.1, 3.5, 1.0, 2.1, 8.3, 1.1]]).T
y = np.array([1, 6, 7, 1, 2, 3])
tscv = TimeSeriesSplit(n_splits=2)
for train, test in tscv.split(X):
print(train, test)
Run Code Online (Sandbox Code Playgroud)
得到:
[0 1] [2 3]
[0 1 2 3] [4 5]
Run Code Online (Sandbox Code Playgroud)
如果我尝试:
model = xgb.XGBRegressor()
param_search = {'max_depth' : [3, 5]}
my_cv = TimeSeriesSplit(n_splits=2).split(X) …Run Code Online (Sandbox Code Playgroud) 我想根据组(grp 列)进行时间序列交叉验证。在下面的示例数据中,温度是我的目标变量
import numpy as np
import pandas as pd
timeS=pd.date_range(start='1980-01-01 00:00:00', end='1980-01-01 00:00:05',
freq='S')
df = pd.DataFrame(dict(time=timeS, grp=['A']*3 + ['B']*3, material=[1,2,3]*2,
temperature=['2.4','5','9.9']*2))
grp material temperature time
0 A 1 2.4 1980-01-01 00:00:00
1 A 2 5 1980-01-01 00:00:01
2 A 3 9.9 1980-01-01 00:00:02
3 B 1 2.4 1980-01-01 00:00:03
4 B 2 5 1980-01-01 00:00:04
5 B 3 9.9 1980-01-01 00:00:05
Run Code Online (Sandbox Code Playgroud)
我打算使用此代码添加一些基于 grp 的滞后功能。
df.groupby("grp")['temperature'].shift(-1)
0 5
1 9.9
2 NaN
3 5
4 9.9
5 NaN
Name: …Run Code Online (Sandbox Code Playgroud)