我想从现在开始拆分最后 7 天的日期。
这是我的代码。
df = pdr.get_data_yahoo('ibm',
start=datetime.datetime(y-5, m, d),
end=pd.datetime.now().date())
split_date = datetime.datetime(y, m, d-7)
train = df.loc[:split_date, ['Close']]
test = df.loc[split_date:, ['Close']]
print(train)
print("################################")
print(test)
它显示的结果是这样的。
............
2019-01-25 133.970001
2019-01-28 134.270004
2019-01-29 134.330002
2019-01-30 134.380005
2019-01-31 134.419998
2019-02-01 134.100006
##################################################################
Close
Date
2019-02-01 134.100006
2019-02-04 135.190002
2019-02-05 135.550003
2019-02-06 136.320007
2019-02-07 133.000000
print(test) 应该显示过去 7 天的数据,但为什么它只显示 5 天,以及为什么 2019-02-01 134.100006 在训练中显示并测试数据拆分不应该相同。
请您参考如下方法:
过去 7 天的数据集中似乎只有 5 行(工作日),这就是为什么您的测试集只显示 5 行的原因。
“2019-02-01”同时包含在训练集和测试集中的原因是因为
df.iloc[start_ind:end_ind]从start_ind< 检索行到end_ind包括。 (这在文档中作为警告提到。您可以检查 here。)要正确拆分,您可以在火车测试中包含直到 8 天前的所有日期,并在测试集中包含最后 7 天。
