尝试使用ARIMA预测能耗时出现“ ValueError:估计自由度不足”

时间:2019-04-09 22:54:32

标签: python arima

我正在尝试使用ARIMA模型预测能耗,以进行基准测试并比较深度学习的准确性。尝试在Google的Colab中运行代码时,出现“ ValueError估计的自由度不足”的情况。

我尝试了不同的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

0 个答案:

没有答案
相关问题