I am calculating spatial KDE using scipy.stats.kde.gaussian kde. However, its evaluation takes quite a lot of time - 70% of my script time, which is 26s for 10000 rows. I'd like to make it faster. Here is my original code:
from scipy.stats import kde
kernel = kd.gausian_kde(values, bw_method=.05)
result = kernel(positions)
Basing on Speed up sampling of kernel estimate, I've implemented multiprocessing:
SKERNEL = None
# sets global kernel function
# - multiprocessing requires my function to be top-level module function
setKernel()
def calc_kernel(sample):
return SKERNEL(sample)
def genKernel(elements):
cores = mp.cp_count()
torun = np.array_split(elements, cores, axis=1)
pool = mp.Pool(processes = cores)
r = pool.map(calc_kernel, torun)
return np.concatenate(r)
However, on the same dataset this implementation takes 36 seconds to run. using cProfiler, I can see that most of the time takes "wait" process. What am I doing wrong and how this can be modified to work faster?