设为首页 加入收藏

TOP

二次指数平滑预测法 Python实现(二)
2015-12-01 14:11:40 来源: 作者: 【 】 浏览:16
Tags:指数 平滑 预测 Python 实现
)
? ? ? ? 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')



def begin():
? ? sangjin = '''
? ? ? ? -----------------------------------------
? ? ? ? | 欢迎使用二次指数平滑法预测未来值? ? |
? ? ? ? |? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
? ? ? ? | 使用方法:? ? ? ? ? ? ? ? ? ? ? ? |
? ? ? ? | 1.根据提示进行操作? ? ? ? ? ? ? ? |
? ? ? ? | 2.输出为预测走势图,以及处理后的excel? ? |
? ? ? ? -----------------------------------------
? ? ? ? | 青木公司花名:桑槿? ? ? ? ? ? |
? ? ? ? | 新浪微博:一只尼玛? ? ? ? ? ? ? |
? ? ? ? | 微信/QQ:569929309? ? ? ? ? ? |
? ? ? ? -----------------------------------------
? ? '''
? ? print(sangjin)


def loop(path):
? ? choice1 = input("自动计算变化系数请选择y,手动请选择n\n")
? ? if choice1 == 'y':
? ? ? ? step = judgestep()
? ? ? ? p5 = calvaluetop5(read07excel(path), step)
? ? ? ? print('总误差最小的前五个是')
? ? ? ? for i in p5:
? ? ? ? ? ? print('变化系数:{0:3f}\t预测值:{1:3f}\t总误差值:{2:3f}'.format(i[0],i[2],i[4]))
? ? ? ? single(p5[0][0],path)
? ? else:
? ? ? ? a = judge()
? ? ? ? single(a,path)



def loop3(path):
? ? choice2 = input("如果想操作其他文件请选择y,退出选择n,其他操作按任意键\n")
? ? if choice2 == 'y':
? ? ? ? loop1()
? ? elif choice2 == 'n':
? ? ? ? print("正在退出中...\n"*6)
? ? ? ? print("正在退出中...谢谢")
? ? ? ? exit(1)
? ? else:
? ? ? ? loop(path)
? ? ? ? loop3(path)


def loop1():
? ? path = judgefile()
? ? loop(path)
? ? loop3(path)


begin()
loop1()


输入excel格式如下:



输出结果:




代码参考:


下载


--------------------------------------

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

评论

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