二次指数平滑预测法 Python实现

目录结构如下:

二次指数平滑预测法 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()

def writeexcel07(path, content,, 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_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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/ac23b04d1c1d53c1c7c0c2c9e10ad5c1.html