溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python構(gòu)建指數(shù)平滑預(yù)測(cè)模型示例

發(fā)布時(shí)間:2020-10-02 06:27:20 來(lái)源:腳本之家 閱讀:637 作者:AlanConstantineLau 欄目:開發(fā)技術(shù)

指數(shù)平滑法

其實(shí)我想說(shuō)自己百度的…

只有懂的人才會(huì)找到這篇文章…

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

指數(shù)平滑法相比于移動(dòng)平均法,它是一種特殊的加權(quán)平均方法。簡(jiǎn)單移動(dòng)平均法用的是算術(shù)平均數(shù),近期數(shù)據(jù)對(duì)預(yù)測(cè)值的影響比遠(yuǎn)期數(shù)據(jù)要大一些,而且越近的數(shù)據(jù)影響越大。指數(shù)平滑法正是考慮了這一點(diǎn),并將其權(quán)值按指數(shù)遞減的規(guī)律進(jìn)行分配,越接近當(dāng)前的數(shù)據(jù),權(quán)重越大;反之,遠(yuǎn)離當(dāng)前的數(shù)據(jù),其權(quán)重越小。指數(shù)平滑法按照平滑的次數(shù),一般可分為一次指數(shù)平滑法、二次指數(shù)平滑法和三次指數(shù)平滑法等。然而一次指數(shù)平滑法適用于無(wú)趨勢(shì)效應(yīng)、呈平滑趨勢(shì)的時(shí)間序列的預(yù)測(cè)和分析,二次指數(shù)平滑法多適用于呈線性變化的時(shí)間序列預(yù)測(cè)。

具體公式還是百度吧…

材料

1.python3.5

2.numpy

3.matplotlib

4.國(guó)家社科基金1995-2015年立項(xiàng)數(shù)據(jù)

需求

預(yù)測(cè)2016年和2017年國(guó)家社科基金項(xiàng)目立項(xiàng)數(shù)量

數(shù)據(jù)

#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

#指數(shù)平滑公式
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

#繪制預(yù)測(cè)曲線
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)#設(shè)置繪圖區(qū)域的大小和像素
 plt.plot(year, number, color='blue', label="actual value")#將實(shí)際值的折線設(shè)置為藍(lán)色
 plt.plot(new_year[1:], s_pre_double[2:],color='red', label="double predicted value")#將二次指數(shù)平滑法計(jì)算的預(yù)測(cè)值的折線設(shè)置為紅色
 plt.plot(new_year[1:], s_pre_triple[2:],color='green', label="triple predicted value")#將三次指數(shù)平滑法計(jì)算的預(yù)測(cè)值的折線設(shè)置為綠色
 plt.legend(loc='lower right')#顯示圖例的位置,這里為右下方
 plt.title('Projects')
 plt.xlabel('year')#x軸標(biāo)簽
 plt.ylabel('number')#y軸標(biāo)簽
 plt.xticks(new_year)#設(shè)置x軸的刻度線為new_year
 plt.show()


def main():
 alpha = .70#設(shè)置alphe,即平滑系數(shù)
 pre_year = np.array([2016, 2017])#將需要預(yù)測(cè)的兩年存入numpy的array對(duì)象里
 data_path = r'data1.txt'#設(shè)置數(shù)據(jù)路徑
 data = np.loadtxt(data_path)#用numpy讀取數(shù)據(jù)
 year, time_id, number = data.T#將數(shù)據(jù)分別賦值給year, time_id, number
 initial_line = np.array([0, 0, number[0]])#初始化,由于平滑指數(shù)是根據(jù)上一期的數(shù)值進(jìn)行預(yù)測(cè)的,原始數(shù)據(jù)中的最早數(shù)據(jù)為1995,沒(méi)有1994年的數(shù)據(jù),這里定義1994年的數(shù)據(jù)和1995年數(shù)據(jù)相同
 initial_data = np.insert(data, 0, values=initial_line, axis=0)#插入初始化數(shù)據(jù)
 initial_year, initial_time_id, initial_number = initial_data.T#插入初始化年

 s_single = exponential_smoothing(alpha, initial_number)#計(jì)算一次指數(shù)平滑
 s_double = exponential_smoothing(alpha, s_single)#計(jì)算二次平滑字?jǐn)?shù),二次平滑指數(shù)是在一次指數(shù)平滑的基礎(chǔ)上進(jìn)行的,三次指數(shù)平滑以此類推

 a_double = 2*s_single-s_double#計(jì)算二次指數(shù)平滑的a
 b_double = (alpha/(1-alpha))*(s_single-s_double)#計(jì)算二次指數(shù)平滑的b
 s_pre_double = np.zeros(s_double.shape)#建立預(yù)測(cè)軸
 for i in range(1, len(initial_time_id)):
  s_pre_double[i] = a_double[i-1]+b_double[i-1]#循環(huán)計(jì)算每一年的二次指數(shù)平滑法的預(yù)測(cè)值,下面三次指數(shù)平滑法原理相同
 pre_next_year = a_double[-1]+b_double[-1]*1#預(yù)測(cè)下一年
 pre_next_two_year = a_double[-1]+b_double[-1]*2#預(yù)測(cè)下兩年
 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)#組合預(yù)測(cè)值

 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)#傳入預(yù)測(cè)值和數(shù)據(jù)


if __name__ == '__main__':
 main()

預(yù)測(cè)結(jié)果

python構(gòu)建指數(shù)平滑預(yù)測(cè)模型示例

python構(gòu)建指數(shù)平滑預(yù)測(cè)模型示例

代碼及數(shù)據(jù)

以上這篇python構(gòu)建指數(shù)平滑預(yù)測(cè)模型示例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI