国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【Python數(shù)據(jù)分析】實(shí)踐編寫(xiě)篇3:在Python中使用三階指數(shù)平滑模型對(duì)金融數(shù)據(jù)集進(jìn)行擬合與預(yù)測(cè)

這篇具有很好參考價(jià)值的文章主要介紹了【Python數(shù)據(jù)分析】實(shí)踐編寫(xiě)篇3:在Python中使用三階指數(shù)平滑模型對(duì)金融數(shù)據(jù)集進(jìn)行擬合與預(yù)測(cè)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

目錄

一、前期準(zhǔn)備

二、數(shù)據(jù)來(lái)源與樣式

?三、數(shù)據(jù)的預(yù)處理

(一)表格處理

(二)數(shù)據(jù)導(dǎo)入

(三)數(shù)據(jù)處理

?四、模型構(gòu)建(指數(shù)平滑)

(一)數(shù)據(jù)作圖

(二)觀察季節(jié)性與趨勢(shì)

(三)一階指數(shù)平滑

(四)二階指數(shù)平滑

(五)三階指數(shù)平滑

(六)均方誤(MSE)比較

?(七)正態(tài)性檢驗(yàn)

五、數(shù)據(jù)預(yù)測(cè)

六、總結(jié)

七、完整代碼


一、前期準(zhǔn)備

本次模型的構(gòu)建與預(yù)測(cè)都是用的是python進(jìn)行,其中涉及多個(gè)庫(kù):

import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.graphics.api import qqplot
import warnings
import os
from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.tsa.holtwinters import SimpleExpSmoothing,ExponentialSmoothing
from sklearn.metrics import mean_squared_error

以上各個(gè)庫(kù)的作用介紹不是本文主要內(nèi)容不過(guò)多解釋,請(qǐng)自行了解。

本次實(shí)踐通過(guò)三階指數(shù)平滑的方法對(duì)十列金融數(shù)據(jù)集進(jìn)行模型的擬合與預(yù)測(cè)。

二、數(shù)據(jù)來(lái)源與樣式

本次實(shí)驗(yàn)數(shù)據(jù)為十列金融數(shù)據(jù)集,該數(shù)據(jù)集為時(shí)間序列,且相互之間獨(dú)立,因此需要對(duì)十個(gè)時(shí)間序列分別進(jìn)行擬合與預(yù)測(cè)。

python 做數(shù)據(jù)預(yù)測(cè),python,金融,人工智能

圖1 數(shù)據(jù)樣式

?三、數(shù)據(jù)的預(yù)處理

(一)表格處理

由于時(shí)間序列數(shù)據(jù)之間相互獨(dú)立,為了便于建模預(yù)測(cè)在導(dǎo)入之前我在excel表格上對(duì)數(shù)據(jù)進(jìn)行了簡(jiǎn)單的處理。

python 做數(shù)據(jù)預(yù)測(cè),python,金融,人工智能

圖2 處理后表格

我將十個(gè)時(shí)間序列數(shù)據(jù)分別放入十個(gè)sheet里,并且以年份命名,以便于代碼導(dǎo)入數(shù)據(jù)。再者,由于原始時(shí)間序列數(shù)據(jù)沒(méi)有時(shí)間列,因此我按照個(gè)人理解添加了時(shí)間列,每一個(gè)數(shù)據(jù)都來(lái)自每月的一月一日,因此其周期可分為12、6、4、3等。

(二)數(shù)據(jù)導(dǎo)入

通過(guò)pandas的相關(guān)方法導(dǎo)入excel數(shù)據(jù)。設(shè)置好相關(guān)參數(shù),這樣在pycharm里顯示數(shù)據(jù)時(shí)就不會(huì)出現(xiàn)過(guò)多數(shù)據(jù)而省略部分?jǐn)?shù)據(jù)的情況。

#處理warning
warnings.filterwarnings("ignore") #有時(shí)候代碼處于某些原因會(huì)飄紅卻不影響正常的運(yùn)行,為了美觀使用該代碼進(jìn)行忽視處理
#作圖顯示中文字符
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
#展示所有列表文件
pd.set_option('display.max_columns',1000)
pd.set_option("display.width",1000)
pd.set_option('display.max_colwidth',1000)
pd.set_option('display.max_rows',1000)

datax=pd.read_excel(r'D:\雜貨\金融數(shù)據(jù)集合.xlsx',sheet_name=None)

在python中導(dǎo)入excel的方法在基礎(chǔ)操作篇有介紹,不再贅述,詳情可看本篇文章:

【Python處理EXCEL】基礎(chǔ)操作篇:在Python中導(dǎo)入EXCEL數(shù)據(jù)

(三)數(shù)據(jù)處理

為方便后續(xù)選擇數(shù)據(jù)以及代碼參數(shù)的調(diào)整,我將“日期”列設(shè)置為行索引,通過(guò)以下代碼實(shí)現(xiàn)。

#將excel中的“日期”一列設(shè)置為行索引
data=data.set_index('日期')
data.index=pd.to_datetime(data.index)

?四、模型構(gòu)建(指數(shù)平滑)

(一)數(shù)據(jù)作圖

在此以1964年金融數(shù)據(jù)集為例。首先導(dǎo)入數(shù)據(jù)并作出原始數(shù)據(jù)的折線圖。數(shù)據(jù)可視化的最基礎(chǔ)的知識(shí)可以看我先前寫(xiě)的文章:

【Python數(shù)據(jù)分析】實(shí)踐編寫(xiě)篇1:用Python程序完成描述性統(tǒng)計(jì)分析需求https://blog.csdn.net/Deng333333555/article/details/125697176?spm=1001.2014.3001.5501

該篇末尾對(duì)數(shù)據(jù)可視化作了簡(jiǎn)單的教學(xué),但是只是冰山一角,想要深入學(xué)習(xí)數(shù)據(jù)可視化的朋友可以在網(wǎng)上查看其他的教程,也可以關(guān)注本人,后續(xù)會(huì)出使用的可視化教學(xué)篇。

#原始數(shù)據(jù)作圖
fig=plt.figure(figsize=(15,8))#作圖面積的大小
ax1=plt.subplot(1,1,1)#作圖的位置
plt.xticks(fontsize=20)#設(shè)置x軸刻度的字體大小
plt.yticks(fontsize=20)#設(shè)置y軸刻度的字體大小
plt.xlabel('日期',fontsize=35,color='blue')#設(shè)置x軸的標(biāo)簽,藍(lán)色部分字
plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue')#設(shè)置y軸的標(biāo)簽,藍(lán)色部分字
plt.title('原始數(shù)據(jù)1964',fontsize=35)#設(shè)置圖的標(biāo)題
x1=data.index #將先前設(shè)置的日期行索引設(shè)置為坐標(biāo)軸的橫軸
y=data.iloc[:,0] #利用提取excel列的方法,提取了金融數(shù)據(jù)集第一列的數(shù)據(jù)作為y軸
plt.plot(x1,y,linewidth=4.0,label='真實(shí)線',color='orange')#繪制坐標(biāo)及數(shù)據(jù),并設(shè)置一些參數(shù)
plt.legend(fontsize=20)#設(shè)置圖例,即圖中左上角那個(gè)
plt.show()#繪圖

python 做數(shù)據(jù)預(yù)測(cè),python,金融,人工智能

?圖3 1964年金融數(shù)據(jù)折線圖

由上圖可看到該年金融數(shù)據(jù)的基本趨勢(shì)。

(二)觀察季節(jié)性與趨勢(shì)

通過(guò)程序作出以下季節(jié)性及趨勢(shì)圖,我將周期設(shè)置為12,原因在開(kāi)篇提及,不再?gòu)?fù)述。

decomposition=seasonal_decompose(data.iloc[:,0],model='addictive',period=12)
decomposition.plot()
plt.show()
#關(guān)于seasonal_decompose()方法的參數(shù)
seasonal_decompose(x,model='additive',filt=None,period=None,two_sided=True,extrapolate_trend=0)
x

時(shí)間序列。

如果是兩維的,則單個(gè)Series應(yīng)該在一列中。

x 必須包含 2 個(gè)完整的周期。

model

{“additive”, “multiplicative”}

時(shí)間序列分解的類型(加和 or 求乘積)

參數(shù)名稱縮寫(xiě)是允許的

filt

過(guò)濾掉季節(jié)性分量的過(guò)濾系數(shù)。

濾波中使用的具體移動(dòng)平均法(單邊or兩側(cè))由 two_sided確定。

(個(gè)人理解是,計(jì)算滑動(dòng)平均時(shí),滑動(dòng)平均階數(shù)內(nèi)各點(diǎn)所乘的那個(gè)系數(shù))

period

時(shí)間序列的周期。

如果 x 不是 pandas 對(duì)象或 x 的索引沒(méi)有頻率,則必須使用。

如果 x 是具有時(shí)間序列索引的 pandas 對(duì)象,則覆蓋 x 的默認(rèn)周期。

two_sided

濾波中使用的移動(dòng)平均法。

如果為 True(默認(rèn)),則使用 filt 計(jì)算居中移動(dòng)平均值。

如果為 False,則濾波器系數(shù)filt僅適用于過(guò)去的值。

extrapolate_trend

如果設(shè)置為 > 0,考慮到這么多 (+1) 個(gè)最近點(diǎn),移動(dòng)平均(卷積)產(chǎn)生的趨勢(shì)是在兩端外推的線性最小二乘法(如果 two_lateral 為 False,則為單側(cè)外推)。如果設(shè)置為“freq”,則使用最近點(diǎn)。設(shè)置此參數(shù)會(huì)導(dǎo)致趨勢(shì)或殘差組件中沒(méi)有 NaN 值。

?(方法的介紹來(lái)源于網(wǎng)絡(luò))

python 做數(shù)據(jù)預(yù)測(cè),python,金融,人工智能

?圖4 季節(jié)性及趨勢(shì)圖

從趨勢(shì)圖中看到,自1965到1967年期間數(shù)據(jù)有較大幅度的上漲,波動(dòng)較大。而在1967到1971年間趨勢(shì)波動(dòng)小,變動(dòng)較為平。1971年至1972年又出現(xiàn)了較大的波動(dòng)。從季節(jié)性圖來(lái)看,數(shù)據(jù)的變化在12期的情況下出現(xiàn)了周期波動(dòng)的情況,說(shuō)明該金融數(shù)據(jù)存在季節(jié)性趨。殘差圖則說(shuō)明了數(shù)據(jù)中隨機(jī)誤差的產(chǎn)生是呈現(xiàn)正態(tài)分布的,并且期分布隨機(jī)、不可預(yù)測(cè)。因此以該數(shù)據(jù)來(lái)構(gòu)建預(yù)測(cè)模型是具有可行性的。

(三)一階指數(shù)平滑

以下是一階模型擬合數(shù)據(jù)的代碼:需要注意的是,擬合的時(shí)候需要在最后加上? .fittedvalues 。

datasmooth1= SimpleExpSmoothing(data.iloc[:,0]).fit().fittedvalues
print(datasmooth1)

?以下是一階平滑擬合的可視化代碼及圖:

plt.figure(figsize=(15,8))
plt.title('一階平滑1964',fontsize=40)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.xlabel('日期',fontsize=35,color='blue')
plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue')
datasmooth1.plot(color='green',linewidth=3.0,label='擬合線') #將一階平滑的結(jié)果畫(huà)折線圖
data.iloc[:,0].plot(color='black',linewidth=3.0,label='真實(shí)線')#將原始數(shù)據(jù)畫(huà)折線圖
plt.legend(fontsize=20)
plt.show()

python 做數(shù)據(jù)預(yù)測(cè),python,金融,人工智能

?圖5 一階平滑結(jié)果可視化

由圖可以看出一階指數(shù)平滑下模型的擬合效果很差,沒(méi)有擬合出原始數(shù)據(jù)的波動(dòng)與趨勢(shì),因此一階指數(shù)平滑不可用。

(四)二階指數(shù)平滑

以下是二階模型擬合數(shù)據(jù)的代碼:

datasmooth2= ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal=None).fit().fittedvalues#添加trend效果
datasmooth2_2 = ExponentialSmoothing(data.iloc[:,0], trend="mul", seasonal=None).fit().fittedvalues#非添加trend效果
print(datasmooth2)
print(datasmooth2_2)

??以下是二階平滑擬合的可視化代碼及圖:

plt.figure(figsize=(15,8))
plt.title('二階平滑1964',fontsize=40)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.xlabel('日期',fontsize=35,color='blue')
plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue')
datasmooth2.plot(color='green',linewidth=3.0,label='line_add')#添加trend效果折線圖
datasmooth2_2.plot(color='red',linewidth=3.0,label='line_mul')#非添加trend效果折線圖
data.iloc[:,0].plot(color='black',linewidth=3.0,label='line_real')#真實(shí)線圖
plt.legend(fontsize=20)
plt.show()

python 做數(shù)據(jù)預(yù)測(cè),python,金融,人工智能

?圖6 二階平滑結(jié)果可視化

由圖看出,二階平滑區(qū)分為真實(shí)線、加入趨勢(shì)效應(yīng)、未加入趨勢(shì)效應(yīng)三條線,其中加入趨勢(shì)效應(yīng)的曲線的擬合效果更加貼近真實(shí)線。但總的來(lái)看,二階指數(shù)平滑的擬合效果仍然很差,無(wú)法擬合出原始數(shù)據(jù)的波動(dòng)與趨勢(shì)。

(五)三階指數(shù)平滑

以下是三階模型擬合數(shù)據(jù)的代碼:

datasmooth3 = ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal="add", seasonal_periods=12).fit().fittedvalues#添加seasonal效果
datasmooth3_2 = ExponentialSmoothing(data.iloc[:,0], trend="mul", seasonal="mul", seasonal_periods=12).fit().fittedvalues#非添加seasonal效果
print(datasmooth3)
print(datasmooth3_2)

?以下是二階平滑擬合的可視化代碼及圖:

plt.figure(figsize=(15,8))
plt.title('三階平滑'+num,fontsize=40)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.xlabel('日期',fontsize=35,color='blue')
plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue')
datasmooth3.plot(color='green',linewidth=3.0,label='line_add')#添加seasonal效果折線圖
datasmooth3_2.plot(color='red',linewidth=3.0,label='line_mul')#非添加seasonal效果折線圖
data.iloc[:,0].plot(color='black',linewidth=3.0,label='line_real')#原始數(shù)據(jù)折線圖
plt.legend(fontsize=20)
plt.show()

python 做數(shù)據(jù)預(yù)測(cè),python,金融,人工智能

?圖7 三階平滑結(jié)果可視化

由圖可以看出,三階指數(shù)平滑后的結(jié)果與原始數(shù)據(jù)更為貼近,反映出了原始數(shù)據(jù)的波動(dòng)及趨勢(shì)。圖中的三條線分別為原始數(shù)據(jù)線、加入季節(jié)效應(yīng)線、未加入季節(jié)效應(yīng)線,而加入了季節(jié)效應(yīng)的曲線效果與原始數(shù)據(jù)更加接近。綜合以上,三階指數(shù)平滑的效果相對(duì)一階指數(shù)平滑以及二階指數(shù)平滑的表現(xiàn)更良好。

(六)均方誤(MSE)比較

以上我做了三個(gè)模型,分別為一階指數(shù)平滑、二階指數(shù)平滑和三階指數(shù)平滑,為了從這三者之間選擇哪一個(gè)模型效果最優(yōu),那么需要有一個(gè)標(biāo)準(zhǔn)來(lái)進(jìn)行比較,而通常會(huì)選擇比較三個(gè)模型的均方誤(MSE)來(lái)進(jìn)行選擇。代碼如下:

from sklearn.metrics import mean_squared_error
datasmooth1= SimpleExpSmoothing(data.iloc[:,0]).fit().fittedvalues#一階指數(shù)平滑擬合結(jié)果
datasmooth2= ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal=None).fit().fittedvalues#二階指數(shù)平滑擬合結(jié)果
datasmooth3 = ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal="add", seasonal_periods=12).fit().fittedvalues#三階指數(shù)平滑擬合結(jié)果
mse_1 = mean_squared_error(datasmooth1,data.iloc[:,0])#一階指數(shù)平滑的均方誤
mse_2 = mean_squared_error(datasmooth2,data.iloc[:,0])#二階指數(shù)平滑的均方誤
mse_3 = mean_squared_error(datasmooth3,data.iloc[:,0])#三階指數(shù)平滑的均方誤
print(mse_1)
print(mse_2)
print(mse_3)

表1?均方誤計(jì)算結(jié)果

均方誤(MSE)

一階指數(shù)平滑

二階指數(shù)平滑

三階指數(shù)平滑

140960.991

155824.614

47311.015

由上表可知,三界指數(shù)平滑的均方誤是最小的,且都遠(yuǎn)小于一階與二階情況下的均方誤,說(shuō)明三界指數(shù)平滑的擬合效果要優(yōu)于一階指數(shù)平滑與二階指數(shù)平滑。總體來(lái)看,三者的均方誤都很大,在模型的擬合與預(yù)測(cè)中仍然存在較大的偏差,但相對(duì)于ARMA模型來(lái)說(shuō)其擬合效果已經(jīng)有很大的提升。

?(七)正態(tài)性檢驗(yàn)

正態(tài)性檢驗(yàn)代碼與圖如下:

resid=model.resid#先計(jì)算出數(shù)據(jù)的殘差
#以下再進(jìn)行作圖
fig=plt.figure(figsize=(8,6))
ax=fig.add_subplot(1,1,1)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.xlabel('Theoretical Quantiles',fontsize=35,color='blue')
plt.ylabel('Sample Quantiles',fontsize=35,color='blue')
plt.title('正態(tài)性檢驗(yàn)1964',fontsize=20)
fig=qqplot(resid,line='q',ax=ax,fit=True)
plt.show()

python 做數(shù)據(jù)預(yù)測(cè),python,金融,人工智能

?圖8 正態(tài)性檢驗(yàn)qq圖

由qq圖來(lái)看,該模型數(shù)據(jù)通過(guò)正態(tài)性檢驗(yàn),符合正態(tài)性分布,說(shuō)明我先前的判斷合理。綜合以上考慮,我決定使用三階指數(shù)平滑方法來(lái)構(gòu)建模型并預(yù)測(cè)未來(lái)18期的金融數(shù)據(jù)。

五、數(shù)據(jù)預(yù)測(cè)

預(yù)測(cè)數(shù)據(jù)的代碼如下:

(需要注意的是:在進(jìn)行預(yù)測(cè)時(shí),第一行代碼與擬合時(shí)不一樣,最后部分沒(méi)有? .fittedvalues? 。)

通過(guò)forecast()方法進(jìn)行未來(lái)十八期數(shù)據(jù)的預(yù)測(cè),其中該方法中的數(shù)字18則是說(shuō)明要預(yù)測(cè)未來(lái)18期的數(shù)據(jù)。

model = ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal="add", seasonal_periods=12).fit()
pred = model.forecast(18)
print(pred)

表2?未來(lái)18期預(yù)測(cè)數(shù)據(jù)

日期

預(yù)測(cè)數(shù)據(jù)

1973-01-01

7285.228568

1973-02-01

6915.147716

1973-03-01

7793.479060

1973-04-01

7143.713913

1973-05-01

7379.143971

1973-06-01

7104.064988

1973-07-01

6545.559722

1973-08-01

6991.558418

1973-09-01

6986.912414

1973-10-01

7799.023518

1973-11-01

7221.405211

1973-12-01

6921.526282

1974-01-01

7199.852791

1974-02-01

6829.771939

1974-03-01

7708.103282

1974-04-01

7058.338135

1974-05-01

7293.768194

1974-06-01

7018.689211

上表是由模型預(yù)測(cè)出的未來(lái)18期的金融數(shù)據(jù)。

預(yù)測(cè)圖代碼如下:

plt.figure(figsize=(15,8))
plt.title('最終預(yù)測(cè)結(jié)果1964',fontsize=40)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.xlabel('日期',fontsize=35,color='blue')
plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue')
pred.plot(color='red',linewidth=3.0,label='預(yù)測(cè)線')#預(yù)測(cè)數(shù)據(jù)折線圖
data.iloc[:,0].plot(color='black',linewidth=3.0,label='真實(shí)線')#實(shí)際數(shù)據(jù)折線圖
plt.legend(fontsize=20)
plt.show()

python 做數(shù)據(jù)預(yù)測(cè),python,金融,人工智能

?圖9 預(yù)測(cè)結(jié)果可視化

?由上圖看出,預(yù)測(cè)數(shù)據(jù)的波動(dòng)以及變化趨勢(shì)與原始數(shù)據(jù)較為吻合,認(rèn)為其具有一定的合理性。

六、總結(jié)

?以上的建模與預(yù)測(cè)只以1964年金融數(shù)據(jù)集為例,而其他數(shù)據(jù)集在我進(jìn)行建模過(guò)程中表現(xiàn)出的特性與該例子的數(shù)據(jù)集相似,因此使用三階指數(shù)平滑的方法在十個(gè)數(shù)據(jù)集中都能夠行得通。

七、完整代碼

由于總體需要預(yù)測(cè)的數(shù)據(jù)有10列,而我在數(shù)據(jù)處理時(shí)將十列數(shù)據(jù)分別放入同一個(gè)表格中的不同Sheet,在本文開(kāi)頭已有說(shuō)明,因此完整代碼中我加入了一個(gè)簡(jiǎn)單的循環(huán)與函數(shù)來(lái)遍歷不同的sheet來(lái)分別預(yù)測(cè)不同的數(shù)據(jù)集·。同時(shí)也出于個(gè)人的需要,我要將代碼輸出的數(shù)據(jù)寫(xiě)入一個(gè)word文件中,因此在完整代碼中可以看到print()函數(shù)里面會(huì)接一個(gè)file=wordfile參數(shù),這個(gè)就是將輸出print到我指定的word文件中,同時(shí)我還需要將作出的圖片輸出到指定的文件夾當(dāng)中,因此在作圖的最后我會(huì)添加一行plt.savefig()樣式的代碼,這個(gè)對(duì)于作圖可有可無(wú),純看個(gè)人的需求。

其次,三階指數(shù)平滑并不是該數(shù)據(jù)集的最優(yōu)的擬合與預(yù)測(cè)方法,只是相對(duì)其他模型而言實(shí)現(xiàn)相對(duì)簡(jiǎn)單,若有更高的預(yù)測(cè)精度需求可自行嘗試構(gòu)建其他模型。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-582350.html

import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.graphics.api import qqplot
import warnings
import os

#處理warning
warnings.filterwarnings("ignore")
#作圖顯示中文字符
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
#展示所有列表文件
pd.set_option('display.max_columns',1000)
pd.set_option("display.width",1000)
pd.set_option('display.max_colwidth',1000)
pd.set_option('display.max_rows',1000)

datax=pd.read_excel(r'D:\雜貨\金融數(shù)據(jù)集合.xlsx',sheet_name=None)#導(dǎo)入表格中的所有sheet
datasets=['1998','2001','1996','1981','1982','1974','1976','1972','1984','1964']#待循環(huán)的十個(gè)數(shù)據(jù)


def timeda_3(num):
    data=pd.read_excel(r'D:\雜貨\金融數(shù)據(jù)集合.xlsx',sheet_name=num)
    data=data.set_index('日期')
    data.index=pd.to_datetime(data.index)
    #原始數(shù)據(jù)作圖
    fig=plt.figure(figsize=(15,8))
    ax1=plt.subplot(1,1,1)
    plt.xticks(fontsize=20)
    plt.yticks(fontsize=20)
    plt.xlabel('日期',fontsize=35,color='blue')
    plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue')
    plt.title('原始數(shù)據(jù)'+num,fontsize=35)
    x1=data.index
    y=data.iloc[:,0]
    plt.plot(x1,y,linewidth=4.0,label='真實(shí)線',color='orange')
    plt.legend(fontsize=20)
    plt.savefig(os.path.join(r'C:\Users\Lenovo\Desktop\商業(yè)數(shù)據(jù)挖掘大作業(yè)\金融數(shù)據(jù)集圖片', '原始數(shù)據(jù)圖'+num))#用于將圖片保存到指定的文件夾中
    plt.show()
    #作季節(jié)性圖
    from statsmodels.tsa.seasonal import seasonal_decompose
    decomposition=seasonal_decompose(data.iloc[:,0],model='addictive',period=12)
    decomposition.plot()
    plt.savefig(os.path.join(r'C:\Users\Lenovo\Desktop\商業(yè)數(shù)據(jù)挖掘大作業(yè)\金融數(shù)據(jù)集圖片', '季節(jié)性圖' + num))
    plt.show()
    #一階平滑
    from statsmodels.tsa.holtwinters import SimpleExpSmoothing,ExponentialSmoothing
    datasmooth1= SimpleExpSmoothing(data.iloc[:,0]).fit().fittedvalues
    print('一階平滑結(jié)果:\n',datasmooth1,file=wordfile)
    plt.figure(figsize=(15,8))
    plt.title('一階平滑'+num,fontsize=40)
    plt.xticks(fontsize=20)
    plt.yticks(fontsize=20)
    plt.xlabel('日期',fontsize=35,color='blue')
    plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue')
    datasmooth1.plot(color='green',linewidth=3.0,label='擬合線')
    data.iloc[:,0].plot(color='black',linewidth=3.0,label='真實(shí)線')
    plt.legend(fontsize=20)
    plt.savefig(os.path.join(r'C:\Users\Lenovo\Desktop\商業(yè)數(shù)據(jù)挖掘大作業(yè)\金融數(shù)據(jù)集圖片', '一階平滑圖' + num))
    plt.show()
    #一階平滑均方誤
    datasmooth1= SimpleExpSmoothing(data.iloc[:,0]).fit().fittedvalues
    from sklearn.metrics import mean_squared_error
    mse_1 = mean_squared_error(datasmooth1,data.iloc[:,0])
    print('一階平滑均方誤:',mse_1,file=wordfile)
    #二階平滑
    datasmooth2= ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal=None).fit().fittedvalues
    datasmooth2_2 = ExponentialSmoothing(data.iloc[:,0], trend="mul", seasonal=None).fit().fittedvalues
    print('二階平滑結(jié)果(add):\n',datasmooth2,file=wordfile)
    print('二階平滑結(jié)果(mul):\n',datasmooth2_2,file=wordfile)
    plt.figure(figsize=(15,8))
    plt.title('二階平滑'+num,fontsize=40)
    plt.xticks(fontsize=20)
    plt.yticks(fontsize=20)
    plt.xlabel('日期',fontsize=35,color='blue')
    plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue')
    datasmooth2.plot(color='green',linewidth=3.0,label='line_add')
    datasmooth2_2.plot(color='red',linewidth=3.0,label='line_mul')
    data.iloc[:,0].plot(color='black',linewidth=3.0,label='line_real')
    plt.legend(fontsize=20)
    plt.savefig(os.path.join(r'C:\Users\Lenovo\Desktop\商業(yè)數(shù)據(jù)挖掘大作業(yè)\金融數(shù)據(jù)集圖片', '二階平滑圖' + num))
    plt.show()
    #二階平滑均方誤
    datasmooth2= ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal=None).fit().fittedvalues
    from sklearn.metrics import mean_squared_error
    mse_2 = mean_squared_error(datasmooth2,data.iloc[:,0])
    print('二階平滑均方誤:',mse_2,file=wordfile)
    #三階平滑
    datasmooth3 = ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal="add", seasonal_periods=12).fit().fittedvalues
    datasmooth3_2 = ExponentialSmoothing(data.iloc[:,0], trend="mul", seasonal="mul", seasonal_periods=12).fit().fittedvalues
    print('三階平滑結(jié)果(add):\n',datasmooth3,file=wordfile)
    print('三階平滑結(jié)果(mul):\n',datasmooth3_2,file=wordfile)
    plt.figure(figsize=(15,8))
    plt.title('三階平滑'+num,fontsize=40)
    plt.xticks(fontsize=20)
    plt.yticks(fontsize=20)
    plt.xlabel('日期',fontsize=35,color='blue')
    plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue')
    datasmooth3.plot(color='green',linewidth=3.0,label='line_add')
    datasmooth3_2.plot(color='red',linewidth=3.0,label='line_mul')
    data.iloc[:,0].plot(color='black',linewidth=3.0,label='line_real')
    plt.legend(fontsize=20)
    plt.savefig(os.path.join(r'C:\Users\Lenovo\Desktop\商業(yè)數(shù)據(jù)挖掘大作業(yè)\金融數(shù)據(jù)集圖片', '三階平滑圖' + num))
    plt.show()
    #三階平滑均方誤
    datasmooth3 = ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal="add", seasonal_periods=12).fit().fittedvalues
    from sklearn.metrics import mean_squared_error
    mse_3 = mean_squared_error(datasmooth3,data.iloc[:,0])
    print('三階平滑均方誤:',mse_3,file=wordfile)
    #預(yù)測(cè)三階平滑模型數(shù)據(jù)
    model = ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal="add", seasonal_periods=12).fit()
    pred = model.forecast(18)
    print('三階平滑預(yù)測(cè)結(jié)果數(shù)據(jù):\n',pred,file=wordfile)
    #qq圖正態(tài)性檢驗(yàn)
    resid=model.resid
    fig=plt.figure(figsize=(8,6))
    ax=fig.add_subplot(1,1,1)
    plt.xticks(fontsize=20)
    plt.yticks(fontsize=20)
    plt.xlabel('Theoretical Quantiles',fontsize=35,color='blue')
    plt.ylabel('Sample Quantiles',fontsize=35,color='blue')
    plt.title('正態(tài)性檢驗(yàn)'+num,fontsize=20)
    fig=qqplot(resid,line='q',ax=ax,fit=True)
    plt.savefig(os.path.join(r'C:\Users\Lenovo\Desktop\商業(yè)數(shù)據(jù)挖掘大作業(yè)\金融數(shù)據(jù)集圖片', '正態(tài)性檢驗(yàn)qq圖' + num))
    plt.show()
    #預(yù)測(cè)圖
    plt.figure(figsize=(15,8))
    plt.title('最終預(yù)測(cè)結(jié)果'+num,fontsize=40)
    plt.xticks(fontsize=20)
    plt.yticks(fontsize=20)
    plt.xlabel('日期',fontsize=35,color='blue')
    plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue')
    pred.plot(color='red',linewidth=3.0,label='預(yù)測(cè)線')
    data.iloc[:,0].plot(color='black',linewidth=3.0,label='真實(shí)線')
    plt.legend(fontsize=20)
    plt.savefig(os.path.join(r'C:\Users\Lenovo\Desktop\商業(yè)數(shù)據(jù)挖掘大作業(yè)\金融數(shù)據(jù)集圖片', '預(yù)測(cè)數(shù)據(jù)圖' + num))
    plt.show()
    print('-------------','以上為',num,'年的數(shù)據(jù)','------------',file=wordfile)

if __name__=='__main__':
    wordfile = open(r'C:\Users\Lenovo\Desktop\商業(yè)數(shù)據(jù)挖掘大作業(yè)\數(shù)據(jù)輸出.docx', 'w')#打開(kāi)一個(gè)word文檔,只有打開(kāi)了才能寫(xiě)入
    for num in datasets:
        timeda_3(num)
    wordfile.close()#寫(xiě)完word文檔后要關(guān)閉,才能保存。
    print('運(yùn)行完畢')

到了這里,關(guān)于【Python數(shù)據(jù)分析】實(shí)踐編寫(xiě)篇3:在Python中使用三階指數(shù)平滑模型對(duì)金融數(shù)據(jù)集進(jìn)行擬合與預(yù)測(cè)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【頭歌】——數(shù)據(jù)分析與實(shí)踐-python-網(wǎng)絡(luò)爬蟲(chóng)-Scrapy爬蟲(chóng)基礎(chǔ)-網(wǎng)頁(yè)數(shù)據(jù)解析-requests 爬蟲(chóng)-JSON基礎(chǔ)

    第1關(guān) 爬取網(wǎng)頁(yè)的表格信息 第2關(guān) 爬取表格中指定單元格的信息 第3關(guān) 將單元格的信息保存到列表并排序 第4關(guān) 爬取div標(biāo)簽的信息 第5關(guān) 爬取單頁(yè)多個(gè)div標(biāo)簽的信息 第6關(guān) 爬取多個(gè)網(wǎng)頁(yè)的多個(gè)div標(biāo)簽的信息 第1關(guān) Scarpy安裝與項(xiàng)目創(chuàng)建 第2關(guān) Scrapy核心原理 第1關(guān) XPath解析網(wǎng)頁(yè) 第

    2024年01月22日
    瀏覽(29)
  • 【頭歌】——數(shù)據(jù)分析與實(shí)踐-python-Pandas 初體驗(yàn)-Pandas數(shù)據(jù)取值與選擇-Pandas進(jìn)階

    第1關(guān) 了解數(shù)據(jù)處理對(duì)象–Series 第2關(guān) 了解數(shù)據(jù)處理對(duì)象-DataFrame 第3關(guān) 讀取 CSV 格式數(shù)據(jù) 第4關(guān) 數(shù)據(jù)的基本操作——排序 第5關(guān) 數(shù)據(jù)的基本操作——?jiǎng)h除 第6關(guān) 數(shù)據(jù)的基本操作——算術(shù)運(yùn)算 第7關(guān) 數(shù)據(jù)的基本操作——去重 第8關(guān) 數(shù)據(jù)重塑 第1關(guān) Series數(shù)據(jù)選擇 第2關(guān) DataFrame數(shù)據(jù)

    2024年01月22日
    瀏覽(155)
  • 從單細(xì)胞數(shù)據(jù)分析的最佳實(shí)踐看R與Python兩個(gè)陣營(yíng)的博弈

    從單細(xì)胞數(shù)據(jù)分析的最佳實(shí)踐看R與Python兩個(gè)陣營(yíng)的博弈

    R與Python,在生物信息學(xué)領(lǐng)域的博弈異常激烈。許多生信分析,兩個(gè)陣營(yíng)都發(fā)展出了自己的方法,比如單細(xì)胞數(shù)據(jù)分析,R有Seurat,Python就有Scanpy。這些層出不窮的方法不斷地吸引著吃瓜群眾的眼球,同時(shí)也讓人患上了選擇困難癥。 到底誰(shuí)優(yōu)誰(shuí)劣?一時(shí)竟難分高下。今天我們就

    2024年01月25日
    瀏覽(38)
  • GPT模型支持下的Python-GEE遙感云大數(shù)據(jù)分析、管理與可視化技術(shù)及多領(lǐng)域案例實(shí)踐

    GPT模型支持下的Python-GEE遙感云大數(shù)據(jù)分析、管理與可視化技術(shù)及多領(lǐng)域案例實(shí)踐

    隨著航空、航天、近地空間等多個(gè)遙感平臺(tái)的不斷發(fā)展,近年來(lái)遙感技術(shù)突飛猛進(jìn)。由此,遙感數(shù)據(jù)的空間、時(shí)間、光譜分辨率不斷提高,數(shù)據(jù)量也大幅增長(zhǎng),使其越來(lái)越具有大數(shù)據(jù)特征。對(duì)于相關(guān)研究而言,遙感大數(shù)據(jù)的出現(xiàn)為其提供了前所未有的機(jī)遇,但同時(shí)也提出了巨

    2024年02月09日
    瀏覽(31)
  • 使用Python進(jìn)行數(shù)據(jù)分析——方差分析

    使用Python進(jìn)行數(shù)據(jù)分析——方差分析

    大家好,方差分析可以用來(lái)判斷幾組觀察到的數(shù)據(jù)或者處理的結(jié)果是否存在顯著差異。本文介紹的方差分析(Analysis of Variance,簡(jiǎn)稱ANOVA)就是用于檢驗(yàn)兩組或者兩組以上樣本的均值是否具備顯著性差異的一種數(shù)理統(tǒng)計(jì)方法。 根據(jù)影響試驗(yàn)條件的因素個(gè)數(shù)可以將方差分析分為

    2024年02月15日
    瀏覽(25)
  • 使用Python進(jìn)行數(shù)據(jù)分析——線性回歸分析

    使用Python進(jìn)行數(shù)據(jù)分析——線性回歸分析

    大家好,線性回歸是確定兩種或兩種以上變量之間互相依賴的定量關(guān)系的一種統(tǒng)計(jì)分析方法。根據(jù)自變量的個(gè)數(shù),可以將線性回歸分為一元線性回歸和多元線性回歸分析。 一元線性回歸:就是只包含一個(gè)自變量,且該自變量與因變量之間的關(guān)系是線性關(guān)系。例如通過(guò)廣告費(fèi)這

    2023年04月10日
    瀏覽(41)
  • 【頭歌】——數(shù)據(jù)分析與實(shí)踐-基于Python語(yǔ)言的文件與文件夾管理-文本 文件處理-利用csv模塊進(jìn)行csv文件的讀寫(xiě)操作

    第1關(guān) 創(chuàng)建子文件夾 第2關(guān) 刪除帶有只讀屬性的文件 第3關(guān) 批量復(fù)制文件夾中的所有文件 未通過(guò)本題,如果您通過(guò)了本題歡迎補(bǔ)充到評(píng)論區(qū),有時(shí)間我會(huì)整理進(jìn)來(lái) 第1關(guān) 讀取宋詞文件,根據(jù)詞人建立多個(gè)文件 第2關(guān) 讀取宋詞文件,并根據(jù)詞人建立多個(gè)文件夾 第3關(guān) 讀取宋詞文

    2024年01月25日
    瀏覽(104)
  • 使用Python批量進(jìn)行數(shù)據(jù)分析

    使用Python批量進(jìn)行數(shù)據(jù)分析

    知識(shí)延伸 1、sort_value()是pandas模塊中DataFrame對(duì)象的函數(shù),用于將數(shù)據(jù)區(qū)域按照某個(gè)字段的數(shù)據(jù)進(jìn)行排序,這個(gè)字段可以是行字段,也可以是列字段。 語(yǔ)法格式: sort_value(by=\\\'##\\\',axis=0,ascending=True,inplace=False,na_position=\\\'last\\\') 參數(shù) 說(shuō)明 by 要排序的列名或索引值 axis 如果省略或者為

    2024年02月10日
    瀏覽(25)
  • 如何使用Python進(jìn)行數(shù)據(jù)分析?

    要使用Python進(jìn)行數(shù)據(jù)分析,可以按照以下步驟進(jìn)行: 安裝Python:首先,你需要安裝Python解釋器??梢詮腜ython官方網(wǎng)站下載并安裝適合你操作系統(tǒng)的Python版本。 安裝數(shù)據(jù)分析庫(kù):Python有許多強(qiáng)大的數(shù)據(jù)分析庫(kù),如NumPy、Pandas和Matplotlib。使用pip命令或包管理工具安裝這些庫(kù)。 導(dǎo)

    2024年02月10日
    瀏覽(21)
  • 使用Python進(jìn)行數(shù)據(jù)分析——描述性統(tǒng)計(jì)分析

    使用Python進(jìn)行數(shù)據(jù)分析——描述性統(tǒng)計(jì)分析

    大家好,描述性統(tǒng)計(jì)分析主要是指求一組數(shù)據(jù)的平均值、中位數(shù)、眾數(shù)、極差、方差和標(biāo)準(zhǔn)差等指標(biāo),通過(guò)這些指標(biāo)來(lái)發(fā)現(xiàn)這組數(shù)據(jù)的分布狀態(tài)、數(shù)字特征等內(nèi)在規(guī)律。在Python中進(jìn)行描述性統(tǒng)計(jì)分析,可以借助Numpy、Pandas、SciPy等科學(xué)計(jì)算模塊計(jì)算出指標(biāo),然后用繪圖模塊Ma

    2024年02月07日
    瀏覽(25)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包