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

更新时间:2023-06-14 10:00:37 阅读: 评论:0

python构建指数平滑预测模型_⼆次指数平滑预测法Python实
从以往的时间序列值,进⾏指数平滑,做两次预测出下⼀个时间的估计值。
⽬录结构如下:
forecast.py
# -*-coding:utf-8 -*-
# Time:2015.11.25 sangjin
__author__ = 'hunterhug'
import matplotlib
#matplotlib.u("Agg")
#matplotlib.u("TkAgg")
#matplotlib.u("gtk")
import matplotlib.pyplot as plt
from matplotlib.pyplot import savefig
from matplotlib.font_manager import FontProperties
from operator import itemgetter
#读取execel使⽤(⽀持07)
swimfrom 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()
el:
print("⽂件不存在")
return judgefile()
def writeexcel07(path, content, name='Sheet1', sheetnum=0):
wb=Workbook()
#sheet=wb.add_sheet("xlwt3数据测试表")
ate_sheet(sheetnum,name)
# values = [["名称", "Hadoop编程实战", "hba编程实战", "lucene编程实战"], ["价格", "52.3", "45", "36
ani"], ["出版社", "机械⼯业出版社", "⼈民邮电出版社", "华夏⼈民出版社"], ["中⽂版式", "中", "英", "英"]]
for i in range(0,len(content)):
for j in range(0,len(content[i])):
# ll(row = 1,column= 2).value="温度"
wb.save(path)
print("写⼊数据成功!")
def read07excel(path):
excelcontent = []
wb2=load_workbook(path)
sheetnames = _sheet_names()
_sheet_by_name(sheetnames[0])
_highest_row()
_highest_column()
# print("列数: ",ws.get_highest_column())
# print("⾏数: ",ws.get_highest_row())
for i in range(0,row):
rowcontent = []
for j in range(0,col):
ws[i][j].value:
滴水之恩当涌泉相报的意思rowcontent.ws[i][j].value)
excelcontent.append(rowcontent)
print("读取数据成功!")
return excelcontent优缺点英文
def calvalue(excel, a):
date = [] # x label date
data = [] # y label data
for i in range(2,len(excel)-1):
data.append(float(excel[i][1]))
date.append(excel[i][0])
e1 = [data[0]] # one time forecast
for i in range(0,len(data)):
e1.append(next)
e1e = [] # one time absoultion error
for i in range(0,len(data)):
e1e.append(abs(data[i]-e1[i]))
e1e2 = sum(e1e)
e2 = [data[0]] # cond time forecast
for i in range(0,len(data)):
next = e1[i] * a + e2[i] * (1 - a)
e2.append(next)
e2e = [] # cond time absoultion error
for i in range(0,len(data)):
e2e.append(abs(data[i]-e2[i]))
e2e2 = sum(e2e)
e1y = e1[len(e1)-1] # one time forecast value e2y = e2[len(e2)-1] # two time forecast value return [a, e1y, e2y, e1e2, e2e2]
def calvaluetop5(excel, step = 0.01):
initvalue = 1.0
all = []
top5 =[]
while initvalue <= 1.0 and initvalue >= 0:
all.append(calvalue(excel, initvalue))
initvalue = initvalue -step
d = {}
for i in range(0, len(all)):
d.tdefault(i, all[i][3])
d1 = sorted(d.items(), key=itemgetter(1))
#print(d1)
topnum = len(d1)
if topnum>=5:
topnum = 5
el:
pass
pos = d1[i][0]
top5.append(all[pos])
return top5
def judgestep():however什么意思
try:
a = float(input("请选择系数变化步长(范围0~1):")) # change var except:
print("请输⼊数字好么...")
return judgestep()
while a > 1 or a < 0:
print('输⼊的步长范围在0-1之间')
return judgestep()
return a
def judge():
try:
a = float(input("请输⼊变化系数a:")) # change var
except:
print("请输⼊数字好么...")
return judge()
while a > 1 or a < 0:
print('输⼊的变化系数范围在0-1之间')
return judge()
return a
def single(a,path):
excel = read07excel(path)
title1 = excel[0][0]
title2 = excel[1]
# print(excel)
title = ':'.join(excel[0])
date = [] # x label date
data = [] # y label data
for i in range(2,len(excel)-1):
data.append(float(excel[i][1]))
# print('/n',data)
# print(title,data,date)
contenteditablee1 = [data[0]] # one time forecast
for i in range(0,len(data)):
next = data[i] * a + e1[i] * (1 - a)
e1.append(next)
# print('/n',e1)
e1e = [] # one time absoultion error
for i in range(0,len(data)):
e1e.append(abs(data[i]-e1[i]))
# print('/n',e1e)
ele2 = sum(e1e)
# print(ele2)
e2 = [data[0]] # cond time forecast
for i in range(0,len(data)):
next = e1[i] * a + e2[i] * (1 - a)
e2.append(next)
# print('/n',e2)
平静的意思e2e = [] # cond time absoultion error
聪明的一休主题曲
for i in range(0,len(data)):
四六级成绩查询 身份证e2e.append(abs(data[i]-e2[i]))
safehou# print('/n',e2e)
e2e2 = sum(e2e)
# print(e2e2)
e1y = e1[len(e1)-1] # one time forecast value
e2y = e2[len(e2)-1] # two time forecast value
content = [[title1,'可变系数a=',a]]
content.append([title2[0],title2[1],'⼀次指数平滑预测值','绝对误差','⼆次指数平滑','绝对误差'])
datas = [date, data, e1[:len(e1)-1], e1e, e2[:len(e2)-1], e2e]
datast = [[r[col] for r in datas] for col in range(len(datas[0]))]
content[len(content):] = datast
yu1 = ['', e2y, e1y, ele2, e2y, e2e2]
yu2 = ['', '最终预测值', '⼀次指数平滑预测值', '⼀次指数平滑绝对误差累加', '⼆次指数平滑预测值', '⼀次指数平滑绝对误差累加']

本文发布于:2023-06-14 10:00:37,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/144600.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:指数   时间   系数   出版社   变化
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图