Python实现指数平滑预测模型:基于国家社科基金立项数据
本文介绍如何使用Python构建指数平滑预测模型,以国家社科基金1995-2015年立项数据为例,预测2016年和2017年的立项数量。文章涵盖一次、二次及三次指数平滑法的原理,提供基于Python3.5、numpy和matplotlib的完整代码实现,并展示预测结果图表。
指数平滑法概述
指数平滑法
本文旨在分享相关技术细节,供有特定需求的读者参考。
熟悉该领域的读者可自行查阅。
若对基础概念尚不熟悉,阅读后可能仍需进一步学习。
与简单移动平均法相比,指数平滑法是一种特殊的加权平均方法。简单移动平均法采用算术平均,虽然近期数据对预测值的影响大于远期数据,且影响随时间递减,但指数平滑法通过按指数递减规律分配权值,使越接近当前的数据拥有越大权重,远离当前的数据权重越小。根据平滑次数,指数平滑法通常分为一次、二次和三次指数平滑法。其中,一次指数平滑法适用于无明显趋势、呈平滑状态的时间序列预测;二次指数平滑法则多用于具有线性变化趋势的时间序列预测。
具体数学公式推导可参考相关学术资料。
实验环境与数据准备
实验材料
Python3.5环境
numpy库
matplotlib库
国家社科基金1995年至2015年的立项数据
预测目标与代码实现
预测目标
预测2016年和2017年国家社科基金项目的立项数量
数据来源说明
#year time_id number
1994 1 10
1995 2 3
1996 3 27
1997 4 13
1998 5 12
1999 6 13
2000 7 14
2001 8 23
2002 9 32
2003 10 30
2004 11 36
2005 12 40
2006 13 58
2007 14 51
2008 15 73
2009 16 80
2010 17 106
2011 18 127
2012 19 135
2013 20 161
2014 21 149
2015 22 142
实现代码
# -*- coding: utf-8 -*-
# @Date : 2017-04-11 21:27:00
# @Author : Alan Lau (rlalan@outlook.com)
# @Language : Python3.5
import numpy as np
from matplotlib import pyplot as plt
#指数平滑公式
def exponential_smoothing(alpha, s):
s2 = np.zeros(s.shape)
s2[0] = s[0]
for i in range(1, len(s2)):
s2[i] = alpha*s[i]+(1-alpha)*s2[i-1]
return s2
#绘制预测曲线
def show_data(new_year, pre_year, data, s_pre_double, s_pre_triple):
year, time_id, number = data.T
plt.figure(figsize=(14, 6), dpi=80)#设置绘图区域的大小和像素
plt.plot(year, number, color='blue', label="actual value")#将实际值的折线设置为蓝色
plt.plot(new_year[1:], s_pre_double[2:],color='red', label="double predicted value")#将二次指数平滑法计算的预测值的折线设置为红色
plt.plot(new_year[1:], s_pre_triple[2:],color='green', label="triple predicted value")#将三次指数平滑法计算的预测值的折线设置为绿色
plt.legend(loc='lower right')#显示图例的位置,这里为右下方
plt.title('Projects')
plt.xlabel('year')#x轴标签
plt.ylabel('number')#y轴标签
plt.xticks(new_year)#设置x轴的刻度线为new_year
plt.show()
def main():
alpha = .70#设置alphe,即平滑系数
pre_year = np.array([2016, 2017])#将需要预测的两年存入numpy的array对象里
data_path = r'data1.txt'#设置数据路径
data = np.loadtxt(data_path)#用numpy读取数据
year, time_id, number = data.T#将数据分别赋值给year, time_id, number
initial_line = np.array([0, 0, number[0]])#初始化,由于平滑指数是根据上一期的数值进行预测的,原始数据中的最早数据为1995,没有1994年的数据,这里定义1994年的数据和1995年数据相同
initial_data = np.insert(data, 0, values=initial_line, axis=0)#插入初始化数据
initial_year, initial_time_id, initial_number = initial_data.T#插入初始化年
s_single = exponential_smoothing(alpha, initial_number)#计算一次指数平滑
s_double = exponential_smoothing(alpha, s_single)#计算二次平滑字数,二次平滑指数是在一次指数平滑的基础上进行的,三次指数平滑以此类推
a_double = 2*s_single-s_double#计算二次指数平滑的a
b_double = (alpha/(1-alpha))*(s_single-s_double)#计算二次指数平滑的b
s_pre_double = np.zeros(s_double.shape)#建立预测轴
for i in range(1, len(initial_time_id)):
s_pre_double[i] = a_double[i-1]+b_double[i-1]#循环计算每一年的二次指数平滑法的预测值,下面三次指数平滑法原理相同
pre_next_year = a_double[-1]+b_double[-1]*1#预测下一年
pre_next_two_year = a_double[-1]+b_double[-1]*2#预测下两年
insert_year = np.array([pre_next_year, pre_next_two_year])
s_pre_double = np.insert(s_pre_double, len(s_pre_double), values=np.array([pre_next_year, pre_next_two_year]), axis=0)#组合预测值
s_triple = exponential_smoothing(alpha, s_double)
a_triple = 3*s_single-3*s_double+s_triple
b_triple = (alpha/(2*((1-alpha)**2)))*((6-5*alpha)*s_single -2*((5-4*alpha)*s_double)+(4-3*alpha)*s_triple)
c_triple = ((alpha**2)/(2*((1-alpha)**2)))*(s_single-2*s_double+s_triple)
s_pre_triple = np.zeros(s_triple.shape)
for i in range(1, len(initial_time_id)):
s_pre_triple[i] = a_triple[i-1]+b_triple[i-1]*1 + c_triple[i-1]*(1**2)
pre_next_year = a_triple[-1]+b_triple[-1]*1 + c_triple[-1]*(1**2)
pre_next_two_year = a_triple[-1]+b_triple[-1]*2 + c_triple[-1]*(2**2)
insert_year = np.array([pre_next_year, pre_next_two_year])
s_pre_triple = np.insert(s_pre_triple, len(s_pre_triple), values=np.array([pre_next_year, pre_next_two_year]), axis=0)
new_year = np.insert(year, len(year), values=pre_year, axis=0)
output = np.array([new_year, s_pre_double, s_pre_triple])
print(output)
show_data(new_year, pre_year, data, s_pre_double, s_pre_triple)#传入预测值和数据
if __name__ == '__main__':
main()
结果分析与总结
预测结果展示


完整代码及数据文件
以上为Python构建指数平滑预测模型的示例内容,供读者参考使用。
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













