查找SARIMAX AIC和pdq值,statsmodels

时间:2018-03-14 17:28:04

标签: python-3.x pandas time-series statsmodels

我试图使用statsmodels作为" sm"来找到p,d,q的值以及P,D,Q的季节值。在python中。

我使用的数据集是一个csv文件,其中包含记录能耗的三年时间序列数据。该文件被拆分为一个较小的数据框,以便使用它。这就是df_test.head()的样子。

                      time  total_consumption
122400 2015-05-01 00:01:00            106.391
122401 2015-05-01 00:11:00            120.371
122402 2015-05-01 00:21:00            109.292
122403 2015-05-01 00:31:00             99.838
122404 2015-05-01 00:41:00             97.387

到目前为止,这是我的代码。

#Importing the timeserie data set from local file
df = pd.read_csv(r"C:\Users\path\Name of the file.csv")

#Rename the columns, put time as index and assign datetime to the column time
df.columns = ["time","total_consumption"]
df['time'] = pd.to_datetime(df.time)
df.set_index('time')

#Select test df (there is data from the 2015-05-01 2015-06-01)
df_test = df.loc[(df['time'] >= '2015-05-01') & (df['time'] <= '2015-05-14')]

#Find minimal AIC value for the ARIMA model integers
p = range(0,2)
d = range(0,2)
q = range(0,2)

pdq = list(itertools.product(p,d,q))

seasonal_pdq = [(x[0], x[1], x[2], 12) for x in list(itertools.product(p,d,q))]

warnings.filterwarnings("ignore")

for param in pdq:
    for param_seasonal in seasonal_pdq:
        try:
            mod = sm.tsa.statespace.SARIMAX(df_test,
                                            order=param,
                                            seasonal_order=param_seasonal,
                                            enforce_stationarity=False,
                                            enforce_invertibility=False)

            results = mod.fit()
            print('ARIMA{}x{}12 - AIC:{}'.format(param, param_seasonal, results.aic))

        except:
            continue

当我尝试按原样运行代码时,程序甚至不会确认&#34; for&#34;环。但当我拿出

try:
except:
    continue

程序给我这个错误信息

ValueError: Pandas data cast to numpy dtype of object. Check input data with np.asarray(data).

我怎样才能解决这个问题?是否有办法自动化流程直接输出具有最低AIC值的参数(无需通过所有可能性查找)。

谢谢!

0 个答案:

没有答案