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

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)
        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]] # second time forecast
    for i in range(0,len(data)):
        next = e1[i] * a + e2[i] * (1 - a)
        e2.append(next)
    # print('/n',e2)

e2e = [] # second time absoultion error
    for i in range(0,len(data)):
        e2e.append(abs(data[i]-e2[i]))
    # 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 = ['', '最终预测值', '一次指数平滑预测值', '一次指数平滑绝对误差累加', '二次指数平滑预测值', '一次指数平滑绝对误差累加']
    content.append(yu1)
    content.append(yu2)
    content.append(['说明:请手动插入走势图。此文件为自动计算生成'])
    # print(content)

path1 =path.replace('.xlsx', '(结果生成).xlsx')
    writeexcel07(path1, content, '生成表')
    print("请打开所在目录生成的excel文件(结果生成)")
    plt.close('all')
    font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14)
    plt.figure(figsize=(10,7))
    num = range(0,len(date))
    plt.plot(num, data, 'b-*', label='原始数据')
    plt.plot(num, e1[:len(e1)-1], 'r*-', label='一次指数预测')
    plt.plot(num, e2[:len(e2)-1], 'g*-', label='二次指数预测')
    bottomtitle1 = '\n一次预测值:'+str(e1y)+"\t误差和:"+str(ele2)
    bottomtitle = bottomtitle1 + '\n二次预测值:'+str(e2y)+"\t误差和:"+str(e2e2)
    plt.title('指数平滑法预测走势图(时间序列)变化系数a={0:3f}'.format(a)+bottomtitle, fontproperties=font) # simfang.ttf
    # plt.text(0, 0, bottomtitle, fontproperties=font)
    plt.xlabel('时间间隔', fontproperties=font)
    plt.ylabel('成交额', fontproperties=font)
    legend = plt.legend(loc='upper right', prop=font)
    # legend = plt.legend(loc='upper right', shadow=True, prop=font)
    legend.get_frame().set_facecolor('white')
    plt.grid(True)

# Tweak spacing to prevent clipping of ylabel
    plt.subplots_adjust(left=0.15)
    plt.show()
    savefig('Fig.png')

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

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