说明:加权滑动预测法是时间序列平滑预测模型之一,通过对各个时期的历史数据赋予不同的权值,来反映对将要发生的数据所起的作用。一般来说,距预测期较劲的数据,对预测值的影响也较大,因而其权值也较大;据预测期较远的数据,对预测值的影响也较小,因而其权值也较小。加权滑动预测法可用与物流系统规划前的预测分析。本文通过Python,对含大量数据,需要使用加权滑动预测法进行预测的进行了相应设计。

环境

windows 10 企业版LTSC
python 3.7.2
第三方库:xlrd,xlwt

项目地址

https://github.com/ishelo/Logistics-Demand-Forecasting-By-Python

项目核心代码

# -*- coding: utf-8 -*-
# @Author   : Doratree (doratree@aliyun.com)
# @Language : Python3.7

import xlrd,xlwt

def printIntro():
    print("这个程序可用于加权滑动预测法")
    print("请将相关数据及权重按要求填写到填写到input.xlsx")
    print('''请注意:权值由小到大,从左到右依次填写在第一行;
重新运行文件请关闭excel的文件,防止文件被其他应用占用。''')
    input("请输入任意字符继续")
    print("-" * 30)

def datainput():    #导入数据
    path = 'input.xlsx'
    rb = xlrd.open_workbook(path)
    data_sheet = rb.sheets()[0]
    rowNum = data_sheet.nrows
    m = rowNum - 2    #m为数据的个数
    data = []
    for i in range(2, rowNum):
        data.append(data_sheet.cell_value(i, 0))
    colNum = data_sheet.ncols
    n = colNum - 1    #n为权值的个数
    weights = []
    for j in range(1, colNum):
        weights.append(data_sheet.cell_value(0, j))
    print("数据导入成功")
    print("导入的实际值为:",data)
    print("导入的权值为:", weights)
    print("正在进行n={n}的加权滑动预测计算...".format(n=n))
    print("-"*30)
    return data, weights, n, m

def action(data, weights, n, m):
    forecast = []
    dvalue = []
    for i in range(n,m+1):    #求预测值
        y = 0
        a = 0
        for j in range(n):
            y += weights[j]*data[i-n+a]
            a +=1
        y = round(int(y*1000)/1000,2)
        forecast.append(y)
    for i in range(m-n):    #求绝对误差值
        x = abs(data[i+n]-forecast[i])
        x = round(int(x * 1000) / 1000, 2)
        dvalue.append(x)
    s = 0
    for i in range(m-n):    #求平均绝对误差值
        s +=dvalue[i]
    average = s/(m-n)
    average = round(int(average * 1000) / 1000, 2)
    return forecast, dvalue, average

def print_Summary(forecast, dvalue, average, n):    #输出结果并保存在表格
    print("从第{n}个时期起,预测值为".format(n=n+1), forecast)
    print("从第{n}个时期起,绝对误差值为".format(n=n+1), dvalue)
    print("其平均绝对误差值为", average)

def set_style(name, height, bold=False):
    style = xlwt.XFStyle()   # 初始化样式
    font = xlwt.Font()       # 为样式创建字体
    font.name = name
    font.bold = bold
    font.color_index = 4
    font.height = height
    style.font = font
    return style

def write_excel(forecast, dvalue, average, m, n, data, weights):    #写入文件
    f = xlwt.Workbook()
    sheet1 = f.add_sheet(u'sheet1', cell_overwrite_ok=True)
    row1 = [u'时间', u'实际值', u'预测值', u'绝对误差', u'平均绝对误差']
    sheet1.write(0, 0, '权值', set_style('Times New Roman', 220, True))
    for i in range(n):
        sheet1.write(0, i+1, weights[i], set_style('Times New Roman', 220, True))
    for i in range(0, len(row1)):
        sheet1.write(1, i, row1[i], set_style('Times New Roman', 220, True))
    for i in range(m+1):
        sheet1.write(i+2, 0, i+1, set_style('Times New Roman', 220, True))
    for i in range(len(data)):
        sheet1.write(i+2, 1, data[i], set_style('Times New Roman', 220, True))
    for i in range(n,m+1):
        sheet1.write(i+2, 2, forecast[i-n], set_style('Times New Roman', 220, True))
    for i in range(n, m):
        sheet1.write(i+2, 3, dvalue[i-n], set_style('Times New Roman', 220, True))
    sheet1.write(2, 4, average, set_style('Times New Roman', 220, True))
    f.save('out.xls')
    print("已经将数据写入out.xls")

def mian():
    printIntro()
    data, weights, n, m= datainput()
    forecast, dvalue,average = action(data, weights, n, m)
    print_Summary(forecast, dvalue, average, n)
    write_excel(forecast, dvalue, average, m, n, data, weights)
mian()

使用

确保电脑上安装了python3,并安装相关第三方库。
如未安装第三方库,可打开电脑的cmd,执行

pip install xlrd
pip install xlwt

将项目下载到本地,将数据导入到input.xlsx文件中。

运行在main.py所在的文件夹运行main.py(注意input.xlsx也要在文件夹里!)

回车继续

打开文件夹输出的out.xls

注意:重新运行程序请关闭excel,否则会报错!

结语

加权滑动预测法同简单滑动预测法一样,只能预测最近一期的数据;而且由于权值系数的选择有较大的随机性,使得预测的精度也较差。

Last modification:September 18th, 2019 at 11:46 pm
If you think my article is useful to you, please feel free to appreciate