时滞特征与自回归模型

Python 中的时间序列机器学习

Chris Holdgraf

Fellow, Berkeley Institute for Data Science

过去很有用

  • 时间序列通常在各时间点间存在共享信息。
  • 过去的信息有助于预测未来。
  • 预测时间序列的最佳特征往往是该序列的历史值。
Python 中的时间序列机器学习

关于平滑度与自相关

  • 时间序列常见问题:数据有多平滑?
  • 即:某时点与其相邻时点的相关性(称为自相关)。
  • 数据的自相关程度会影响您的模型。
Python 中的时间序列机器学习

创建时滞特征

  • 下面构建一个用过去值作输入特征的模型。
  • 可据此评估信号的自相关性(以及更多)。
Python 中的时间序列机器学习

用 Pandas 进行时间移位

print(df)
         df 
    0   0.0
    1   1.0 
    2   2.0 
    3   3.0 
    4   4.0 
# Shift a DataFrame/Series by 3 index values towards the past
print(df.shift(3))
         df
    0   NaN
    1   NaN
    2   NaN
    3   0.0
    4   1.0
Python 中的时间序列机器学习

创建时移 DataFrame

# data is a pandas Series containing time series data
data = pd.Series(...)

# Shifts
shifts = [0, 1, 2, 3, 4, 5, 6, 7]

# Create a dictionary of time-shifted data
many_shifts = {'lag_{}'.format(ii): data.shift(ii) for ii in shifts}

# Convert them into a dataframe
many_shifts = pd.DataFrame(many_shifts)
Python 中的时间序列机器学习

用时移特征拟合模型

# Fit the model using these input features 
model = Ridge() 
model.fit(many_shifts, data)
Python 中的时间序列机器学习

解读自回归模型系数

# Visualize the fit model coefficients
fig, ax = plt.subplots()
ax.bar(many_shifts.columns, model.coef_)
ax.set(xlabel='Coefficient name', ylabel='Coefficient value')

# Set formatting so it looks nice
plt.setp(ax.get_xticklabels(), rotation=45, horizontalalignment='right')
Python 中的时间序列机器学习

粗糙信号的系数可视化

Python 中的时间序列机器学习

平滑信号的系数可视化

Python 中的时间序列机器学习

开始练习!

Python 中的时间序列机器学习

Preparing Video For Download...