时间序列预测:三次指数平滑(Holt-Winters)

statsmodels是一个Python模块,它提供对许多不同统计模型估计的类和函数,并且可以进行统计测试和统计数据的探索。

# -*- encoding:utf-8 -*-

import pandas as pd
import matplotlib.pyplot as plt

from statsmodels.tsa.holtwinters import ExponentialSmoothing

# 1、对数据的预处理
input_data = open("ftproot.txt", mode='r').read().split("\n")
time_data = []
for i in range(len(input_data)):
    time_data.append(input_data[i].split(","))
# 全部数据
all_data = []
for i in range(len(time_data)):
    all_data.append(float(time_data[i][1]))
# 分一部分出来作为train数据
train_data = []
test_data = []
train_data.extend([all_data[i] for i in range(0, 1334)])
test_data.extend([all_data[i] for i in range(1334, len(all_data))])

# 2、模型参数
ets3 = ExponentialSmoothing(train_data, trend='add', seasonal='add', seasonal_periods=24)
# 3、拟合模型
r3 = ets3.fit()
# 4、预测
pred3 = r3.predict(start=len(train_data), end=len(all_data)-1)
# 5、画图,可以忽略
pd.DataFrame({
    'origin': test_data,
    'pred': pred3
}).plot(legend=True)
plt.show()
print(pred3)

参数:

    Holt Winter's Exponential Smoothing

    Parameters
    ----------
    endog : array-like
        Time series
    trend : {"add", "mul", "additive", "multiplicative", None}, optional
        Type of trend component.
    damped : bool, optional
        Should the trend component be damped.
    seasonal : {"add", "mul", "additive", "multiplicative", None}, optional
        Type of seasonal component.
    seasonal_periods : int, optional
        The number of seasons to consider for the holt winters.

    Returns
    -------
    results : ExponentialSmoothing class        

    Notes
    -----
    This is a full implementation of the holt winters exponential smoothing as
    per [1]. This includes all the unstable methods as well as the stable methods.
    The implementation of the library covers the functionality of the R 
    library as much as possible whilst still being pythonic.

第一个endog,时间序列数据,array-like的形式。
第二个trend是趋势,有三种可选项,就是加法趋势、乘法趋势还有None。
第三个damped是衰减,Boolean决定是否对趋势进行衰减。
第四个seasonal是季节性(周期),也是三种选项,加法、乘法还有None。
第五个seasonal_periods,季节性周期,int型,holt-winter要考虑的季节的数量。简单来说,多少点是一个周期?你可以设定为一天,一星期,一个月,一年等等

你可能感兴趣的:(python)