多处理函数,用于根据分布列表测试给定数据集。通过列表从每次迭代返回函数值

Multiprocessing a function that tests a given dataset against a list of distributions. Returning function values from each iteration through list

我正在处理包含密集 GPS 数据的数据集。我的目标是使用并行处理来针对所有可能的分布测试我的数据集,并且 return 具有为所述分布生成的参数的最佳分布。

目前,由于这个答案 ,我有连续执行此操作的代码。当然,处理我的完整数据集将花费太长时间。我一直在玩多处理,但似乎无法让它正常工作。我希望它并行测试多个分布,跟踪误差平方和。然后我想 select 具有最低 SSE 的发行版和 return 它的名称以及为其生成的参数。

def fit_dist(distribution, data=data, bins=200, ax=None):

    #Block of code that tests the distribution and generates params

    return(distribution.name, best_params, sse)

if __name__ == '__main__':

    p = Pool()

    result = p.map(fit_dist, DISTRIBUTIONS)

    p.close()
    p.join()

我需要一些帮助,了解如何在多处理中的每个迭代中实际使用 return 值来比较这些值。我是 python 的新手,尤其是多处理,所以请耐心等待并尽可能多地解释。

我遇到的问题是它给了我一个 "UnboundLocalError" 关于我试图从我的 fit_dist 函数 return 的变量。 DISTRIBUTIONS 列表是 89 个对象。这可能与并行处理有关,还是与 fit_dist?

的定义有关

在 Tomerikoo 的评论和一些进一步的努力的帮助下,我让代码按照我想要的方式工作。 UnboundLocalError 是由于我没有将 return 语句放在我的 fit_dist 函数中的正确代码块中。为了回答这个问题,我做了以下事情。

from multiprocessing import Pool

def fit_dist:
    #put this return under the right section of this method
    return[distribution.name, params, sse]

if __name__ == '__main__':

    p = Pool()

    result = p.map(fit_dist, DISTRIBUTIONS)

    p.close()
    p.join()

    '''filter out the None object results. Due to the nature of the distribution fitting, 
    some distributions are so far off that they result in None objects'''
    res = list(filter(None, result))

    #iterates over nested list storing the lowest sum of squared errors in best_sse

    for dist in res:
        if best_sse > dist[2] > 0:
            best_sse = dis[2]
        else:
            continue
    '''iterates over list pulling out sublist of distribution with best sse. 
    The sublists are made up of a string, tuple with parameters, 
    and float value for sse so that's why sse is always index 2.'''   

    for dist in res:
        if dist[2]==best_sse:
            best_dist_list = dist
        else:
            continue

其余代码仅包括我使用该列表构建图表和绘图,并在我的原始数据的直方图上实现最佳分布。