Machine Learning cho Dữ liệu Chuỗi thời gian bằng Python
Chris Holdgraf
Fellow, Berkeley Institute for Data Science
print(df)
df
0 0.0
1 1.0
2 2.0
3 3.0
4 4.0
# Dịch một DataFrame/Series lùi 3 chỉ số về quá khứ
print(df.shift(3))
df
0 NaN
1 NaN
2 NaN
3 0.0
4 1.0
# data là một pandas Series chứa dữ liệu chuỗi thời gian
data = pd.Series(...)
# Các độ trễ
shifts = [0, 1, 2, 3, 4, 5, 6, 7]
# Tạo một dict dữ liệu dịch theo thời gian
many_shifts = {'lag_{}'.format(ii): data.shift(ii) for ii in shifts}
# Chuyển thành dataframe
many_shifts = pd.DataFrame(many_shifts)
# Fit mô hình với các đặc trưng đầu vào này
model = Ridge()
model.fit(many_shifts, data)
# Trực quan hóa các hệ số của mô hình đã fit
fig, ax = plt.subplots()
ax.bar(many_shifts.columns, model.coef_)
ax.set(xlabel='Tên hệ số', ylabel='Giá trị hệ số')
# Định dạng cho dễ nhìn
plt.setp(ax.get_xticklabels(), rotation=45, horizontalalignment='right')


Machine Learning cho Dữ liệu Chuỗi thời gian bằng Python