簡體   English   中英

使用Python訓練Arima模型時如何解決LinAlgError和ValueError

[英]how to solve LinAlgError & ValueError when training arima model with Python

我正在嘗試實現時間序列模型,並得到一些奇怪的異常,這些異常對我沒有任何幫助。 我想知道我是在犯錯誤還是完全可以預期。 這是細節...

在訓練模型時,我嘗試進行網格搜索以找到最佳的(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異常,這對我沒有任何幫助。

據我所知,當拋出這些異常時,代碼並不是真正地在真正訓練某些設置,然后只是跳到將要嘗試的下一個設置。

為什么我看到這些例外? 他們可以被忽略嗎? 我需要怎么做才能解決?

首先,回答您的特定問題:我認為“ 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)並且還添加移動平均成分時。 我建議您重新評估您正在考慮的模型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM