设为首页 加入收藏

TOP

二次指数平滑预测法 Python实现(一)
2015-12-01 14:11:40 来源: 作者: 【 】 浏览:17
Tags:指数 平滑 预测 Python 实现

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


目录结构如下:



Python代码如下:


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()


? ? # 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_column()
? ? # print("列数: ",ws.get_highest_column())
? ? # print("行数: ",ws.get_highest_row())


? ? for i in range(0,row):
? ? ? ? rowcontent = []
? ? ? ? for j in range(0,col):
? ? ? ? ? ? if ws.rows[i][j].value:
? ? ? ? ? ? ? ? rowcontent.append(ws.rows[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)):
? ? ? ? next = data[i] * a + e1[i] * (1 - a)
? ? ? ? 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]] # second time forecast
? ? for i in range(0,len(data)):
? ? ? ? next = e1[i] * a + e2[i] * (1 - a)
? ? ? ? e2.append(next)


? ? e2e = [] # second 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.setdefault(i, all[i][3])
? ? d1 = sorted(d.items(), key=itemgetter(1))
? ? #print(d1)
? ? topnum = len(d1)
? ? if topnum>=5:
? ? ? ? topnum = 5
? ? else:
? ? ? ? pass
? ? for i in range(0,topnum):
? ? ? ? pos = d1[i][0]
? ? ? ? top5.append(all[pos])
? ? return top5


def judgestep():
? ? 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]))
? ? ? ? date.append(excel[i][0])
? ? # print('/n',data)
? ? # print(title,data,date)


? ? e1 = [data[0]] # one time forecast


? ? for i in range(0,len(data)):
? ? ? ? next = data[i] * a + e1[i] * (1 - a

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇关于Python使用list出现乱码的解决 下一篇JavaScript原型,原型链

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: