首页 > sklearn 阅读数:2

Sklearn时间序列交叉验证

Scikit 中还提供了专门用于时间序列模型的交叉验证器。

1) 导入相关模块

In [1]: from sklearn.model_selection import TimeSeriesSplit

2) 导入相关数据

In [2]: X = ["a", "b", "c", "d", "e", "f"]

3) 导入相关数据

In [3]: y = [0, 0, 1, 1, 1, 1,]

4) 创建分组对象

In [4]: tscv = TimeSeriesSplit(n_splits=3) 

5) 查看分组结果

In [5]: for train, test in tscv.split(X):
   ...:     print("%s-%s" % (train, test))
[0 1 2]-[3]
[0 1 2 3]-[4]
[0 1 2 3 4]-[5]