多线程SARIMAX模型出错

6

我第一次使用线程库来加快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个回答

10
我使用lbfgs时也遇到了同样的错误,我不确定为什么lbfgs无法进行梯度评估,但我尝试更改了优化器。你也可以尝试这个,从以下任何优化器中选择:

'newton'表示牛顿-拉夫逊方法,'nm'表示Nelder-Mead

'bfgs'表示Broyden-Fletcher-Goldfarb-Shanno(BFGS),

'lbfgs'表示带有可选框约束的有限内存BFGS

'powell'表示修改后的Powell方法

'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')

虽然这是一个旧问题,但如果将来有人遇到同样的问题,我还是会回答的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接