python构建指数平滑预测模型_二次指数平滑预测法 Python实现

该博客介绍如何使用Python构建指数平滑预测模型,特别是通过二次指数平滑法来预测时间序列数据。文章提供了读取Excel文件、计算预测值、绘制图表以及输出结果到新Excel文件的完整代码实现。
摘要由CSDN通过智能技术生成

从以往的时间序列值,进行指数平滑,做两次预测出下一个时间的估计值。

目录结构如下:

forecast.py

# -*-coding:utf-8 -*-

# Time:2015.11.25 sangjin

__author__ = 'hunterhug'

import matplotlib

#matplotlib.use("Agg")

#matplotlib.use("TkAgg")

#matplotlib.use("gtk")

import matplotlib.pyplot as plt

from matplotlib.pyplot import savefig

from matplotlib.font_manager import FontProperties

from operator import itemgetter

#读取execel使用(支持07)

from openpyxl import Workbook

#写入excel使用(支持07)

from openpyxl import load_workbook

import os

def judgefile():

path = input("请输入该目录下的excel文件名:") # file path

if os.path.isfile(path):

return path.lower()

else:

print("文件不存在")

return judgefile()

def writeexcel07(path, content, name='Sheet1', sheetnum=0):

wb=Workbook()

#sheet=wb.add_sheet("xlwt3数据测试表")

sheet=wb.create_sheet(sheetnum,name)

# values = [["名称", "Hadoop编程实战", "hbase编程实战", "lucene编程实战"], ["价格", "52.3", "45", "36"], ["出版社", "机械工业出版社", "人民邮电出版社", "华夏人民出版社"], ["中文版式", "中", "英", "英"]]

for i in range(0,len(content)):

for j in range(0,len(content[i])):

sheet.cell(row = i+1,column= j+1).value = content[i][j]

# sheet.cell(row = 1,column= 2).value="温度"

wb.save(path)

print("写入数据成功!")

def read07excel(path):

excelcontent = []

wb2=load_workbook(path)

sheetnames = wb2.get_sheet_names()

ws=wb2.get_sheet_by_name(sheetnames[0])

row=ws.get_highest_row()

col=ws.get_highest_col

下面是使用Python编写的三次指数平滑预测代码: ```python import numpy as np def triple_exponential_smoothing(series, alpha, beta, gamma, n_preds): """ 三次指数平滑预测函数 参数: series:时间序列数据 alpha:平滑系数 beta:趋势系数 gamma:季节性系数 n_preds:预测步数 返回: 预测结果 """ result = [] season_length = len(series) // 4 initial_seasonal_components = np.array([series[i] - series[i - season_length] for i in range(season_length)]) def smooth(series, alpha): smoothed = [series[0]] for i in range(1, len(series)): smoothed.append(alpha * series[i] + (1 - alpha) * smoothed[i - 1]) return smoothed def triple_smooth(series, alpha, beta, gamma, season_length, n_preds): smooth_result = smooth(series, alpha) trend = smooth(smooth_result, beta) seasonal = [initial_seasonal_components[i % season_length] for i in range(len(series))] forecast = [smooth_result[-1] + trend[-1] + seasonal[-season_length]] for _ in range(n_preds - 1): next_smooth = alpha * (series[-1] - seasonal[-1]) + (1 - alpha) * (smooth_result[-1] + trend[-1]) next_trend = beta * (next_smooth - smooth_result[-1]) + (1 - beta) * trend[-1] next_seasonal = gamma * (series[-1] - next_smooth) + (1 - gamma) * seasonal[-season_length] forecast.append(next_smooth + next_trend + next_seasonal) smooth_result.append(next_smooth) trend.append(next_trend) seasonal.append(next_seasonal) series.append(next_smooth + next_trend + next_seasonal) return forecast result = triple_smooth(series, alpha, beta, gamma, season_length, n_preds) return result # 使用示例 series = [10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32] alpha = 0.5 beta = 0.4 gamma = 0.3 n_preds = 4 predicted_values = triple_exponential_smoothing(series, alpha, beta, gamma, n_preds) print("预测结果:", predicted_values) ``` 以上代码实现了三次指数平滑预测功能。在函数`triple_exponential_smoothing`中,通过指定的平滑系数alpha、趋势系数beta和季节性系数gamma,以及预测步数n_preds,对给定的时间序列数据进行预测。代码中的例子是一个简单的序列,可以通过修改series、alpha、beta、gamma和n_preds来适应不同的场景和需求。最后,打印出预测结果predicted_values。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值