600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > python构建指数平滑预测模型

python构建指数平滑预测模型

时间:2022-01-17 00:06:37

相关推荐

python构建指数平滑预测模型

指数平滑法

其实我想说自己百度的…

只有懂的人才会找到这篇文章…

不懂的人…看了我的文章…还是不懂哈哈哈

指数平滑法相比于移动平均法,它是一种特殊的加权平均方法。简单移动平均法用的是算术平均数,近期数据对预测值的影响比远期数据要大一些,而且越近的数据影响越大。指数平滑法正是考虑了这一点,并将其权值按指数递减的规律进行分配,越接近当前的数据,权重越大;反之,远离当前的数据,其权重越小。指数平滑法按照平滑的次数,一般可分为一次指数平滑法、二次指数平滑法和三次指数平滑法等。然而一次指数平滑法适用于无趋势效应、呈平滑趋势的时间序列的预测和分析,二次指数平滑法多适用于呈线性变化的时间序列预测。

具体公式还是百度吧…

材料

1.python3.5

2.numpy

3.matplotlib

4.国家社科基金1995-立项数据

需求

预测和国家社科基金项目立项数量

数据

#year time_id number1994 1 101995 2 31996 3 271997 4 131998 5 121999 6 132000 7 142001 8 232002 9 32 10 30 11 36 12 40 13 58 14 51 15 73 16 80 17 106 18 127 19 135 20 161 21 149 22 142

代码

# -*- coding: utf-8 -*-# @Date: -04-11 21:27:00# @Author : Alan Lau (rlalan@)# @Language : Python3.5import numpy as npfrom 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.Tplt.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_yearplt.show()def main():alpha = .70#设置alphe,即平滑系数pre_year = np.array([, ])#将需要预测的两年存入numpy的array对象里data_path = r'data1.txt'#设置数据路径data = np.loadtxt(data_path)#用numpy读取数据year, time_id, number = data.T#将数据分别赋值给year, time_id, numberinitial_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#计算二次指数平滑的ab_double = (alpha/(1-alpha))*(s_single-s_double)#计算二次指数平滑的bs_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_tripleb_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()

预测结果

代码及数据

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。