首页 > 解决方案 > 尝试使用 ARIMA 预测能耗时出现“ValueError:估计的自由度不足”

问题描述

我正在尝试使用 ARIMA 模型来预测能耗,以对深度学习进行基准测试和比较准确性。尝试在 Google 的 Colab 中运行我的代码时,我得到“ValueError Insufficient degree of freedom to estimate”。

我尝试了不同的 P、D、Q 值,但老实说,我并不完全理解应该如何选择它们。

抱歉,代码混乱 - 这是我在 Colab 中逐块运行的内容。

此外,一开始就排除了 GDrive 身份验证过程。

# Get the csv file from Drive
import pandas as pd
downloaded = drive.CreateFile({'id':id}) 
downloaded.GetContentFile('11124.csv')  
df = pd.read_csv('11124.csv', sep = ';', index_col = 1)
df = df.drop(['building'], axis = 1)
df.index = pd.to_datetime(df.index)
df.dtypes
df = df[(df.T != 0).any()]
import matplotlib as plt
df.plot()
df.shape
from matplotlib import pyplot
from statsmodels.tsa.arima_model import ARIMA
#Function that calls ARIMA model to fit and forecast the data
def StartARIMAForecasting(Actual, P, D, Q):
    model = ARIMA(Actual, order=(P, D, Q))
    model_fit = model.fit(disp=0)
    prediction = model_fit.forecast()[0]
    return prediction
#Get exchange rates
ActualData = df
#Size of exchange rates
NumberOfElements = len(ActualData)
#Use 70% of data as training, rest 30% to Test model
TrainingSize = int(NumberOfElements * 0.7)
TrainingData = ActualData[0:TrainingSize]
TestData = ActualData[TrainingSize:NumberOfElements]
#New arrays to store actual and predictions
Actual = [x for x in TrainingData]
Predictions = list()
#In a for loop, predict values using ARIMA model
for timepoint in range(len(TestData)):
    ActualValue =  TestData.iloc[timepoint]
    #forcast value
    Prediction = StartARIMAForecasting(Actual,3,1,0)    
    print('Actual=%f, Predicted=%f' % (ActualValue, Prediction))
    #add it in the list
    Predictions.append(Prediction)
    Actual.append(ActualValue)

ValueError                                Traceback (most recent call 
last)
<ipython-input-133-8cddd294ae3f> in <module>()
  2         ActualValue =  TestData.iloc[timepoint]
  3         #forcast value
----> 4         Prediction = StartARIMAForecasting(Actual,3,1,0)
  5         print('Actual=%f, Predicted=%f' % (ActualValue, Prediction))
  6         #add it in the list

<ipython-input-122-b2acac5bc03e> in StartARIMAForecasting(Actual, P, D, 
Q)
  1 def StartARIMAForecasting(Actual, P, D, Q):
----> 2         model = ARIMA(Actual, order=(P, D, Q))
  3         model_fit = model.fit(disp=0)
  4         prediction = model_fit.forecast()[0]
  5         return prediction

/usr/local/lib/python3.6/dist-packages/statsmodels/tsa/arima_model.py in     
__new__(cls, endog, order, exog, dates, freq, missing)
998         else:
999             mod = super(ARIMA, cls).__new__(cls)
-> 1000             mod.__init__(endog, order, exog, dates, freq, 
missing)
1001             return mod
1002 

/usr/local/lib/python3.6/dist-packages/statsmodels/tsa/arima_model.py in 
__init__(self, endog, order, exog, dates, freq, missing)
1013             # in the predict method
1014             raise ValueError("d > 2 is not supported")
-> 1015         super(ARIMA, self).__init__(endog, (p, q), exog, dates, 
freq, missing)
1016         self.k_diff = d
1017         self._first_unintegrate = unintegrate_levels(self.endog[:d], 
d)

/usr/local/lib/python3.6/dist-packages/statsmodels/tsa/arima_model.py in 
__init__(self, endog, order, exog, dates, freq, missing)
452         super(ARMA, self).__init__(endog, exog, dates, freq, 
missing=missing)
453         exog = self.data.exog  # get it after it's gone through 
processing
--> 454         _check_estimable(len(self.endog), sum(order))
455         self.k_ar = k_ar = order[0]
456         self.k_ma = k_ma = order[1]

/usr/local/lib/python3.6/dist-packages/statsmodels/tsa/arima_model.py in 
_check_estimable(nobs, n_params)
438 def _check_estimable(nobs, n_params):
439     if nobs <= n_params:
--> 440         raise ValueError("Insufficient degrees of freedom to 
estimate")
441 
442 
ValueError: Insufficient degrees of freedom to estimate

标签: pythonarima

解决方案


推荐阅读