繁体   English   中英

如何从多处理池中返回多个值作为 dataframe?

[英]How to return multiple values from multiprocessing pool as a dataframe?

我有一系列用于arima的参数pqd的顺序,例如:

[(0, 0, 0),
 (0, 1, 0),
 (0, 2, 0),
 (0, 3, 0),
 (1, 0, 0),
 (1, 1, 0),
 (1, 2, 0),
 (1, 3, 0),
 (2, 0, 0),
 (2, 1, 0),
 (2, 2, 0),
 (2, 3, 0)]

我想使用多处理使用该参数顺序来评估 arima 的性能。 但是我有两个问题:

  1. eval function 需要 3 个参数
  2. eval function 返回 2 个结果

如何将错误和 model 的结果作为 dataframe 返回?

这是我尝试过的:

from multiprocessing import Pool
#this computes error and return a model
def eval_model_parameters(a,b,order):
    #use order PARAMETER somehow to compute model 
    error = a*b/2
    model = a
    return [error,model]

p = [0, 1 , 2]
d = [0, 1 , 2 ,3]
q = [0]
pdq = list(itertools.product(p, d, q)) 

p = Pool(7)
func = eval_model_parameters(1, 2, pdq)
res  = p.map(func, pdq)  
p.close()

我尝试这样做将参数传递给 function

func = eval_model_parameters(1, 2, pdq)

这将返回结果

res  = p.map(func, pdq) 

但我明白了

--------------------------------------------------------------------------- RemoteTraceback Traceback (most recent call last) RemoteTraceback: """ Traceback (most recent call last): File "Anaconda3\lib\multiprocessing\pool.py", line 121, in worker result = (True, func(*args, **kwds)) File "Anaconda3\lib\multiprocessing\pool.py", line 44, in mapstar return list(map(*args)) TypeError: 'list' object is not callable """ The above exception was the direct cause of the following exception: TypeError Traceback (most recent call last) <ipython-input-53-7743fd9ca60b> in <module> 13 p = Pool(7) 14 func = eval_model_parameters(1, 2, pdq) ---> 15 res = p.map(func, pdq) 16 p.close() 17 Anaconda3\lib\multiprocessing\pool.py in map(self, func, iterable, chunksize) 266 in a list that is returned. 267 ''' --> 268 return self._map_async(func, iterable, mapstar, chunksize).get() 269 270 def starmap(self, func, iterable, chunksize=None): Anaconda3\lib\multiprocessing\pool.py in get(self, timeout) 655 return self._value 656 else: --> 657 raise self._value 658 659 def _set(self, i, obj): TypeError: 'list' object is not callable

完成此操作的正确方法是什么?

pool.map function 的第一个参数应该是可调用的 object。 在您的情况下,您自己调用了 function,然后将结果传递给 pool.map function。 尝试将 function 本身传递给 pool.map,如下所示:

p.map(eval_model_parameters, pdq)

现在,当您更改上述行并运行代码时,您将看到 pdq 列表的元组作为单个参数传递。 要解决这个问题,请按照这个问题将多个参数添加到 pool.map

希望这可以帮助!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM