【问题标题】:Is there a smart way to parrallelize complex functions on ndarray?有没有一种聪明的方法来并行化数组上的复杂函数?
【发布时间】:2020-11-03 09:32:56
【问题描述】:

python 中存在多种可能性来提升您的代码性能(例如广播、numba 等软件包。但据我所知,这些方法依赖于代码是 基本的 em> 在某种意义上,例如 numpy.ndarraynumpy.linalg 的函数被使用。

在我的特殊情况下,我使用statsmodels ThetaModel 来预测(很多!)时间序列,这些时间序列分组在ndarray 中。

有什么聪明的方法可以提高代码性能/并行化代码吗?

目前我正在使用列表理解。


(简化)工作示例

import numpy as np
from statsmodels.tsa.forecasting.theta import ThetaModel

def thetaForecast(series):
    model = ThetaModel(series, period=50, deseasonalize=True, use_test=False).fit()
    forecast = model.forecast(steps=len(series))
    return forecast
    
data = np.random.randn(500,10) # 10 time series each of length 500 (dimensions reduced here for simplification)
dataForecast = np.array([thetaForecast(col) for col in data.transpose()])

以防万一,我的函数thetaForecast 与这个稍微简化的版本相比,实际上需要多个参数。

PS:我不是经验丰富的 stackoverflow 用户。欢迎提出如何改进我的问题的提示:)

【问题讨论】:

    标签: python performance parallel-processing list-comprehension statsmodels


    【解决方案1】:

    您是否尝试过使用多处理?除非 ThetaModel.forecast() 释放 GIL(如果它是用 C 或 Fortran 实现的,它可以),多处理是您可以并行化它的主要方式。

    当然,您也可以自己在 Numba、C、C++ 或 Fortran 中重新实现 forecast() 并自己发布 GIL——然后您可以在单个进程中使用多个线程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-28
      • 2015-04-07
      • 1970-01-01
      • 2021-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-27
      相关资源
      最近更新 更多