如何用Python训练arima模型时解决LinAlgError和ValueError问题

问题描述 投票:1回答:1

我正在尝试实现一个时间序列模型并获得一些奇怪的异常,这些异常对我没有任何意义。我想知道我是犯了错误还是完全没有预料到的。这里有细节......

在训练我的模型时,我尝试进行网格搜索以找到最佳(p,d,q)设置。这是完整的代码(我将在下面解释这里发生的事情):

下面的可重现代码基本上是来自https://machinelearningmastery.com/grid-search-arima-hyperparameters-with-python/的副本,稍有变化......:

import warnings
from pandas import Series
from statsmodels.tsa.arima_model import ARIMA
from sklearn.metrics import mean_squared_error

# evaluate an ARIMA model for a given order (p,d,q)
def evaluate_arima_model(X, arima_order):
    # prepare training dataset
    train_size = int(len(X) * 0.66)
    train, test = X[0:train_size], X[train_size:]
    history = [x for x in train]
    # make predictions
    predictions = list()
    for t in range(len(test)):
        model = ARIMA(history, order=arima_order)
        model_fit = model.fit(disp=0)
        yhat = model_fit.forecast()[0]
        predictions.append(yhat)
        history.append(test[t])
    # calculate out of sample error
    error = mean_squared_error(test, predictions)
    return error

# evaluate combinations of p, d and q values for an ARIMA model
def evaluate_models(dataset, p_values, d_values, q_values):
    dataset = dataset.astype('float64')
    best_score, best_cfg = float("inf"), None
    for p in p_values:
        for d in d_values:
            for q in q_values:
                order = (p,d,q)
                try:
                    print("Evaluating the settings: ", p, d, q)
                    mse = evaluate_arima_model(dataset, order)
                    if mse < best_score:
                        best_score, best_cfg = mse, order
                    print('ARIMA%s MSE=%.3f' % (order,mse))
                except Exception as exception:
                    print("Exception occured...", type(exception).__name__, "\n", exception)

    print('Best ARIMA%s MSE=%.3f' % (best_cfg, best_score))

# dataset
values = np.array([-1.45, -9.04, -3.64, -10.37, -1.36, -6.83, -6.01, -3.84, -9.92, -5.21,
                   -8.97, -6.19, -4.12, -11.03, -2.27, -4.07, -5.08, -4.57, -7.87, -2.80,
                   -4.29, -4.19, -3.76, -22.54, -5.87, -6.39, -4.19, -2.63, -8.70, -3.52, 
                   -5.76, -1.41, -6.94, -12.95, -8.64, -7.21, -4.05, -3.01])

# evaluate parameters
p_values = [7, 8, 9, 10]
d_values = range(0, 3)
q_values = range(0, 3)
warnings.filterwarnings("ignore")
evaluate_models(values, p_values, d_values, q_values)

这是输出(不是一切,但它提供了足够的信息):

Evaluating the settings:  7 0 0
Exception occured... LinAlgError 
 SVD did not converge
Evaluating the settings:  7 0 1
Exception occured... LinAlgError 
 SVD did not converge
Evaluating the settings:  7 0 2
Exception occured... ValueError 
 The computed initial AR coefficients are not stationary
You should induce stationarity, choose a different model order, or you can
pass your own start_params.
Evaluating the settings:  7 1 0
Exception occured... LinAlgError 
 SVD did not converge
Evaluating the settings:  7 1 1
Exception occured... ValueError 
 The computed initial AR coefficients are not stationary
You should induce stationarity, choose a different model order, or you can
pass your own start_params.
Evaluating the settings:  7 1 2
Exception occured... ValueError 
 The computed initial AR coefficients are not stationary
You should induce stationarity, choose a different model order, or you can
pass your own start_params.
Evaluating the settings:  7 2 0
Exception occured... LinAlgError 
 SVD did not converge
Evaluating the settings:  7 2 1
Exception occured... ValueError 
 The computed initial AR coefficients are not stationary
You should induce stationarity, choose a different model order, or you can
pass your own start_params.
Evaluating the settings:  7 2 2
Exception occured... ValueError 
 The computed initial AR coefficients are not stationary
You should induce stationarity, choose a different model order, or you can
pass your own start_params.

代码只是尝试所有不同的给定设置,训练模型,计算每个给定设置的MSE(均方误差),然后选择最佳设置(基于最小MSE)。

但是在训练过程中,代码不断抛出LinAlgErrorValueError异常,这对我没有任何意义。

并且据我所知,当抛出这些异常时,代码并不是真正训练某些设置,然后只是跳转到将要尝试的下一个设置。

为什么我会看到这些例外情况?他们会被忽视吗?我需要做些什么来解决它?

python time-series statsmodels arima
1个回答
2
投票

首先,回答你的具体问题:我认为“SVD没有收敛”是Statsmodels的ARIMA模型中的一个错误。这些天SARIMAX模型得到了更好的支持(并完成了ARIMA模型所做的所有工作+更多),所以我建议使用它。为此,请使用以下命令替换模型创建:

model = sm.tsa.SARIMAX(history, trend='c', order=arima_order, enforce_stationarity=False, enforce_invertibility=False)

话虽如此,我认为考虑到你的时间序列和你正在尝试的规范,你仍然不可能取得好成绩。

特别是,您的时间序列非常短,您只考虑极长的自回归滞后长度(p> 6)。用很少的数据点来估计很多参数是很困难的,特别是当你也有积分(d = 1或d = 2)和你也加入移动平均分量时。我建议您重新评估您正在考虑的模型。

© www.soinside.com 2019 - 2024. All rights reserved.