当前位置:   article > 正文

SARIMA的python实现,包括季节性分解和自动识别参数_sarima python 实现

sarima python 实现
  1. import matplotlib.pyplot as plt
  2. from statsmodels.tsa.statespace.sarimax import SARIMAX
  3. import numpy as np
  4. from pmdarima.arima import auto_arima
  5. import statsmodels.api as sm
  6. import pandas as pd
  7. raw_data=pd.read_excel(r"C:\Users\附件4、土壤蒸发量2012—2022年.xls")
  8. data=raw_data["土壤蒸发量(mm)"].values
  9. index = pd.date_range(start='2012-01-01', periods=len(data), freq='M')
  10. series=pd.Series(data,index=index)
  11. diff1=series.diff(1)
  12. diff2=series.diff(2)
  13. print(u'原始序列的ADF检验结果为:', ADF(series))
  14. print(u'一阶差分序列的ADF检验结果为:', ADF(diff1[1:]))
  15. # 原始序列的ADF检验结果为: (-1.5045801917212067, 0.5312443790121629, 11, 111, {'1%': -3.490683082754047, '5%': -2.8879516565798817, '10%': -2.5808574442009578}, 642.5500224639288)
  16. # 一阶差分序列的ADF检验结果为: (-10.061376456523105, 1.3307651257364255e-17, 10, 111, {'1%': -3.490683082754047, '5%': -2.8879516565798817, '10%': -2.5808574442009578}, 637.9083146867483)
  17. #看输出的第二项,<=0.05则符合要求
  18. #分解周期信息,得到周期
  19. # decomposition = sm.tsa.seasonal_decompose(series, model='additive', extrapolate_trend='freq')
  20. # fig = decomposition.plot()
  21. # plt.show()
  22. #自动求解参数
  23. # model = auto_arima(series, seasonal=True, m=12, stepwise=True, trace=True,
  24. # suppress_warnings=True, error_action="ignore", max_order=None)
  25. # print(model.summary())
  26. #SARIMAX(0, 0, 3)x(1, 0, [1], 12)
  27. model = SARIMAX(series, order=(1,1,3), seasonal_order=(1, 0, 1, 12))
  28. ar_result = model.fit()
  29. # 打印模型的回归系数
  30. # print("AR 模型的回归系数:")
  31. # print(ar_result.params)
  32. forecast_steps = 12 # 预测未来 3 个时间步
  33. forecast = ar_result.get_prediction(start=len(data), end=len(data) + forecast_steps - 1)
  34. forecast_mean = forecast.predicted_mean
  35. forecast_ci = forecast.conf_int()
  36. plt.figure(figsize=(10, 6))
  37. plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
  38. plt.plot(series,label="观测值")
  39. plt.plot(forecast_mean,linestyle='--',label="预测值")
  40. plt.fill_between(forecast_ci.index, forecast_ci.iloc[:, 0], forecast_ci.iloc[:, 1], color='orange', alpha=0.2)
  41. plt.title("时序预测图")
  42. plt.legend()
  43. plt.show()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/511360
推荐阅读
相关标签
  

闽ICP备14008679号