创建时间偏移特征
在用于时间序列的机器学习中,常见做法是利用前几个时间点的信息来预测随后的时间点。
在本练习中,您将"偏移"原始数据并可视化结果。您将使用上一章计算的"百分比变化"(percent change)时间序列,这一次使用"非常短"的窗口。短窗口很重要,因为在真实场景中,您希望预测时间序列的日内或逐日波动,而不是更长时间窗口内的变化。
本练习是课程的一部分
Python 中的时间序列机器学习
练习说明
- 使用字典推导式,基于
shifts中给定的滞后步数,为prices_perc创建多个时间偏移版本。 - 将结果转换为一个 DataFrame。
- 使用给定代码可视化结果。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# These are the "time lags"
shifts = np.arange(1, 11).astype(int)
# Use a dictionary comprehension to create name: value pairs, one pair per shift
shifted_data = {"lag_{}_day".format(day_shift): prices_perc.____(____) for day_shift in shifts}
# Convert into a DataFrame for subsequent use
prices_perc_shifted = ____(shifted_data)
# Plot the first 100 samples of each
ax = prices_perc_shifted.iloc[:100].plot(cmap=plt.cm.viridis)
prices_perc.iloc[:100].plot(color='r', lw=2)
ax.legend(loc='best')
plt.show()