Python多处理替换.map lambda函数

2024-05-18 07:35:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些工作代码,但需要它处理得更快,因为当前的实现将在整个数据集上运行22个小时。我正在尝试使用多处理,因为我有一个12核的机器,可以移动东西

工作代码如下:

def test_firezone(point):
   for polygon in polys_tier2:
       if polygon.contains(point):
           print("Tier2")
           return 1
   return 0

df2['FIREZONE'] = df2['POINT'].map(lambda x: test_firezone(x))

我一直在尝试让它并行运行,方法是遵循在这个博客上提出的建议http://blog.adeel.io/2016/11/06/parallelize-pandas-map-or-apply/

然而,下面的代码看起来并不是真正的计算机代码,而是没有CPU影响的代码。平台是windows10,我在Anaconda/Jupyter笔记本上运行

import multiprocessing
import pandas as pd
import numpy as np
from multiprocessing import Pool

# arbitrary number of partitions
num_partitions = 4
#only use full core count (currently 8 core machine with 4 virtual)
num_cores = (multiprocessing.cpu_count()-4)

def parallelize_dataframe(df3, func):
    a,b,c,d = np.array_split(df3, num_partitions)
    pool = Pool(num_cores)
    print(num_cores)
    df4 = pd.concat(pool.map(func, [a,b,c,d]))
    pool.close()
    pool.join()
    return df4


def test_func(data):
    print ("Process working on: ",data)
    data["FIREZONE"] = data["POINT"].map(test_firezone)
    return data

if __name__ == '__main__':
    test = parallelize_dataframe(df2, test_func)
    print(test)



Tags: 代码testimportmapdatareturndefmultiprocessing