当前位置: 首页 > news >正文

python-arima模型statsmodels库实现-有数据集(续)-statsmodels-0.9.0版本

python-arima模型statsmodels库实现-有数据集(续)

这篇博客是上一篇python-arima模型statsmodels库实现的续集,上一篇采用的statsmodels版本应该要高一点,如果使用低版本的statsmodels代码会有bug,这一篇则是针对statsmodels-0.9.0版本的代码。

代码如下:

#coding=gbk
import  numpy  as np
import pandas as pd
import os
from numpy import NaN
from numpy import nan
import matplotlib.pyplot as plt
import statsmodels.api as sm     #acf,pacf图
from statsmodels.tsa.stattools import adfuller  #adf检验
from pandas.plotting import autocorrelation_plot
from statsmodels.tsa.arima_model import ARIMA
from statsmodels.stats.diagnostic import acorr_ljungboximport statsmodels.api as sm
import matplotlib as mpl
path="E:/data/china_data.xlsx"
# 为了控制计算量,我们限制AR最大阶不超过6,MA最大阶不超过4。plt.style.use('fivethirtyeight')
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['font.serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
df=pd.read_excel(path)
#print(df)
#help(df)#for index, row in df.iterrows():df=df.replace(NaN, "null")
#  print(index, row)
print(df)
def  f(column):r=0inde1=0index2=len(column)-1for i in range(len(column)):#   print(column[len(column)-i-1])if   column[len(column)-i-1] is "null" and r==1:index2=ireturn index1,index2if   column[len(column)-i-1]!= "null" and r==0:index1=ir=1return index1,index2#df['时间(年)']=pd.to_datetime(df['时间(年)'])print(df.columns)
print(df[df.columns[0]])
indexz=df.columns[0]def adf_test(data):#小于0.05则是平稳序列# print("data:",data.values)data_z=np.array(list(data.values))#print(data_z.reshape(-1,))t = adfuller(data_z.reshape(-1,))print("p-value:",t[1])
def  box_pierce_test(data):#小于0.05,不是白噪声序列print(acorr_ljungbox(data, lags=1)) def  stability_judgment(data):fig = plt.figure(figsize=(12,8))ax1=fig.add_subplot(211)fig = sm.graphics.tsa.plot_acf(data,lags=5,ax=ax1)ax2 = fig.add_subplot(212)fig = sm.graphics.tsa.plot_pacf(data,lags=5,ax=ax2)plt.show()def  model_fit(data,df,index,length,index1,index2):data_diff=df[["时间(年)",index]][length-index2:length-index1]#  sm.tsa.arma_order_select_ic(data_diff,max_ar=6,max_ma=4,ic='aic')['aic_min_order']  # AIC#对模型进行定阶pmax = int(len(data) / 10)    #一般阶数不超过 length /10qmax = int(len(data) / 10)if  pmax>4:pmax=6if  qmax>4:qmax=4bic_matrix = []print("data",data)# help(sm.tsa.arima.ARIMA)for p in range(pmax +1):temp= []for q in range(qmax+1):try:#  ARIMA(train_data, order=(1,1,1))# print(sm.tsa.arima.ARIMA(data,order=(p,1,q)).fit())temp.append(sm.tsa.ARIMA(data,order=(p,1,q)).fit().bic)#  print(temp)except:temp.append(None)# temp.append(sm.tsa.arima.ARIMA(data,order=(p,1,q)).fit().bic)bic_matrix.append(temp)bic_matrix = pd.DataFrame(bic_matrix)   #将其转换成Dataframe 数据结构print("bic_matrix",bic_matrix)p,q = bic_matrix.stack().astype(float).idxmin()   #先使用stack 展平, 然后使用 idxmin 找出最小值的位置print(u'BIC 最小的p值 和 q 值:%s,%s' %(p,q))  #  BIC 最小的p值 和 q 值:0,1model = sm.tsa.ARIMA(data, order=(p,1,q)).fit()model.summary()        #生成一份模型报告predictions_ARIMA_diff = pd.Series(model.fittedvalues, copy=True)print(predictions_ARIMA_diff)model.forecast(5)   #为未来5天进行预测, 返回预测结果, 标准误差, 和置信区间for index, column in df.iteritems():if index==indexz:continueindex1,index2 =f(column)length=len(column)# print("index1 index2:",index1,index2)#  print(column[length-index2-1:length-index1])print(index)df[index]=df[index].replace( "null",0)df[index].astype('float')df[str(index)+"diff1"]=df[index].diff(1)df[str(index)+"diff2"]=df[index+"diff1"].diff(1)# 一阶差分还原# tmpdata2:原数据# pred:一阶差分后的预测数据#df_shift = tmpdata2['ecpm_tomorrow'].shift(1)#predict = pred.add(df_shift)# predict = pred + df_shift# print(index2-index1)#print(df[["时间(年)",index]][length-index2:length-index1])adf_test(df[[index]][length-index2:length-index1])box_pierce_test(df[[index]][length-index2:length-index1])model_fit(df[[index]][length-index2:length-index1],df,index,length,index1,index2)## model_fit(data,p,q)stability_judgment(df[[index]][length-index2:length-index1])stability_judgment(df[[str(index)+"diff1"]][length-index2:length-index1])#  stability_judgment(df[[str(index)+"diff2"]][length-index2:length-index1])plt.plot(df[["时间(年)"]][length-index2:length-index1],df[[index]][length-index2:length-index1],label="diff0")plt.plot(df[["时间(年)"]][length-index2:length-index1],df[[str(index)+"diff1"]][length-index2:length-index1],label="diff1")#   plt.plot(df[["时间(年)"]][length-index2:length-index1],df[[str(index)+"diff2"]][length-index2:length-index1],label="diff2")# df[["时间(年)",index]][length-index2:length-index1].plot(x=indexz,y=index,figsize=(9,9))plt.xlabel("时间(年)")plt.ylabel(index)plt.legend()plt.show()os.system("pause")

运行结果如下:
在这里插入图片描述
大家可在这里插入图片描述

大家可以学习一下哈。

http://www.lryc.cn/news/190211.html

相关文章:

  • JVM源码剖析之线程的创建过程
  • ansible的介绍安装与模块
  • el-form简单封装一个列表页中的搜索栏
  • 【Python 2】列表 模式匹配 循环 dict set 可变对象与不可变对象
  • 深度学习—cv动物/植物数据集
  • 高效团队协作软件推荐:提升工作效率的优选方案!
  • Mac中使用virtualenv和virtualenvwrapper
  • wpf webBrowser控件 常用的函数和内存泄漏问题
  • AI游戏设计的半年度复盘;大模型+智能音箱再起波澜;昇思大模型技术公开课第2期;出海注册经验分享;如何使用LoRA微调Llama 2 | ShowMeAI日报
  • 多线程 - 锁策略 CAS
  • VP记录——The 2021 CCPC Weihai Onsite
  • JavaWeb---Servlet
  • 英语——方法篇——单词——谐音法+拼音法——50个单词记忆
  • 35道Rust面试题
  • 01 时钟配置初始化,debug
  • Halcon我的基础教程(一)(我的菜鸟教程笔记)-halcon仿射变换(Affine Transformation)的探究与学习
  • c++视觉---中值滤波处理
  • Edge使用猴油脚本实战(实验室安全考试系统刷在线时长——网站永久自动刷新)
  • Vue 中 KeepAlive 内置缓存使用
  • 语言模型编码中/英文句子格式详解
  • 【Node.js】路由
  • matlab 2ask 4ask 信号调制
  • Python利用jieba分词提取字符串中的省市区(字符串无规则)
  • MuLogin防关联浏览器帮您一键实现Facebook账号多开
  • 【C语言】每日一题(半月斩)——day4
  • Are you sure you want to continue connecting (yes/no) 每次ssh进
  • 网络与信息系统安全设计规范
  • 在Linux怎么用vim实现把一个文件里面的文本复制到另一个文件里面
  • CCAK—云审计知识证书学习
  • 3.springcloudalibaba gateway项目搭建