我希望对我的时间序列数据进行交叉验证,并按时间戳的年份进行拆分。
以下是pandas数据框中的数据:
以下是pandas数据框中的数据:
mock_data
timestamp counts
'2015-01-01 03:45:14' 4
.
.
.
'2016-01-01 13:02:14' 12
.
.
.
'2017-01-01 09:56:54' 6
.
.
.
'2018-01-01 13:02:14' 8
.
.
.
'2019-01-01 11:39:40' 24
.
.
.
'2020-01-01 04:02:03' 30
mock_data.dtypes
timestamp object
counts int64
查看scikit-learn的TimeSeriesSplit()
函数,似乎无法按年份指定n_split
部分。是否有其他方法可以创建连续的训练集,从而得到以下的训练测试拆分?
tscv = newTimeSeriesSplit(n_splits=5, by='year')
>>> print(tscv)
newTimeSeriesSplit(max_train_size=None, n_splits=5, by='year')
>>> for train_index, test_index in tscv.split(mock_data):
... 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]
TRAIN: [2015] TEST: [2016]
TRAIN: [2015 2016] TEST: [2017]
TRAIN: [2015 2016 2017] TEST: [2018]
TRAIN: [2015 2016 2017 2018] TEST: [2019]
TRAIN: [2015 2016 2017 2018 2019] TEST: [2020]
感谢您的浏览!
random.randint
总是生成一个具有相等数据量的数据框架,而“真实”的数据每年的数据量都不同。我的输出最终看起来像: 训练集:[2015] 测试集:[2016] 训练集:[2016] 测试集:[2016 2017] 训练集:[2016 2017] 测试集:[2017 2018] 训练集:[2016 2017 2018] 测试集:[2018 2019 2020] - Sepan-1
来收集测试和训练集,在df中使用df.loc[df.years < test_year,:]
索引您的训练数据集。这样讲清楚了吗?还是需要修改我先前的回答? - mayosten