线程SARIMAX模型出错

时间:2017-09-10 23:40:57

标签: python parallel-processing arima

我第一次使用线程库,以加快我的SARIMAX模型的训练时间。但代码仍然失败,出现以下错误

Bad direction in the line search; refresh the lbfgs memory and restart the iteration.
This problem is unconstrained.
This problem is unconstrained.
This problem is unconstrained.

以下是我的代码:

import numpy as np
import pandas as pd
from statsmodels.tsa.arima_model import ARIMA
import statsmodels.tsa.api as smt
from threading import Thread

def process_id(ndata):
   train = ndata[0:-7]
   test = ndata[len(train):]
   try:
       model = smt.SARIMAX(train.asfreq(freq='1d'), exog=None, order=(0, 1, 1), seasonal_order=(0, 1, 1, 7)).fit()
       pred = model.get_forecast(len(test))
       fcst = pred.predicted_mean
       fcst.index = test.index
       mapelist = []
       for i in range(len(fcst)):
            mapelist.insert(i, (np.absolute(test[i] - fcst[i])) / test[i])
       mape = np.mean(mapelist) * 100
       print(mape)
    except:
       mape = 0
       pass
return mape

def process_range(ndata, store=None):
   if store is None:
      store = {}
   for id in ndata:
      store[id] = process_id(ndata[id])
   return store


def threaded_process_range(nthreads,ndata):
    store = {}
    threads = []
    # create the threads
    k = 0
    tk = ndata.columns
    for i in range(nthreads):
        dk  = tk[k:len(tk)/nthreads+k]
        k = k+len(tk)/nthreads
        t = Thread(target=process_range, args=(ndata[dk],store))
        threads.append(t)
    [ t.start() for t in threads ]
    [ t.join() for t in threads ]
    return store

outdata = threaded_process_range(4,ndata)

我想提几件事:

  • 数据是数据框中的每日库存时间序列
  • 线程适用于ARIMA模型
  • SARIMAX模型在for循环中完成

非常感谢任何见解,谢谢!

1 个答案:

答案 0 :(得分:1)

我在使用lbfgs时遇到了同样的错误,我不确定为什么lbfgs不能进行梯度评估,但是我尝试更改优化器。您也可以尝试此方法,从这些优化器中选择

“牛顿”代表Newton-Raphson,“ nm”代表Nelder-Mead

“ Bfgs”代表Broyden-Fletcher-Goldfarb-Shanno(BFGS)

“ lbfgs”用于具有可选框约束的有限内存BFGS

“鲍威尔”以修改鲍威尔的方法

共轭梯度的“ cg”

“ ncg”表示牛顿共轭梯度

全球盆地跳跃求解器的“ basinhopping”

在您的代码中更改

model = smt.SARIMAX(train.asfreq(freq='1d'), exog=None, order=(0, 1, 1), seasonal_order=(0, 1, 1, 7)).fit(method='cg')

这是一个古老的问题,但我仍在回答,以防将来有人遇到相同的问题。