学习资源:https://github.com/thedataincubator/ds30_3
初次试着把代码调通
- 导入模块
import csv
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
matplotlib.style.use('ggplot')
from datetime import datetime as dt
import holtwinters as hw
from math import sqrt
from sklearn import linear_model
import numpy as np
from operator import add
import sklearn
实例1
Forecasting the percent of uninsured people in the nation using Holt-Winters double exponential smoothing.
- 导入数据
# Import the quarterly uninsured data into a dataframe.
unins_df = pd.read_csv('uninsured_time_series.csv')
unins_df['date'] = pd.to_datetime(unins_df['date'],format='%Y/%m/%d')
raw_ls = list(unins_df['uninsured_proportion'])
- 调参
# Optimized parameters.
hw_fc, alpha, beta, rmse = hw.linear(raw_ls, 3)
print(alpha, beta)
print(rmse, rmse/np.mean(raw_ls))
得出结果:
0.4065834955558603 1.0
0.007195808521353922 0.057952820306206076
- 绘图
plt.plot(raw_ls, color='red', marker='o', markersize=4)
plt.plot([None]*(len(raw_ls)-1) + [raw_ls[-1]] +hw_fc, color='blue', marker='o', alpha=.4)
image.png
5.手动调参
# Tuning parameters by hand.
a, b = (.8, .85)
hw_fc, alpha, beta, rmse = hw.linear(raw_ls, 3, a, b)
print(alpha, beta)
print(rmse, rmse/np.mean(raw_ls))
plt.plot(raw_ls, color='red', marker='o', markersize=4)
plt.plot([None]*(len(raw_ls)-1) + [raw_ls[-1]] + hw_fc, color='blue', marker='o', alpha=.4)
0.8 0.85
0.00831175581559211 0.06694031529335927
image.png
0.1 0.85
0.015760372558038038 0.12692917496406472
image.png
0.1 0.1
0.027223330035764045 0.21924829559004597
image.png
0.1 1
0.014377085180254729 0.11578860547856155
image.png
1 1
0.009219544457292896 0.0742513647567213
image.png
比较各类参数值及图形