我正在尝试 Numba 来加速计算联合发生的最小条件概率的函数。
import numpy as np
from numba import double
from numba.decorators import jit, autojit
X = np.random.random((100,2))
def cooccurance_probability(X):
P = X.shape[1]
CS = np.sum(X, axis=0) #Column Sums
D = np.empty((P, P), dtype=np.float) #Return Matrix
for i in range(P):
for j in range(P):
D[i, j] = (X[:,i] * X[:,j]).sum() / max(CS[i], CS[j])
return D
cooccurance_probability_numba = autojit(cooccurance_probability)
但是我发现 和 的性能cooccurance_probability
几乎cooccurance_probability_numba
相同。
%timeit cooccurance_probability(X)
1 loops, best of 3: 302 ms per loop
%timeit cooccurance_probability_numba(X)
1 loops, best of 3: 307 ms per loop
为什么是这样?可能是由于元素操作的numpy元素吗?
我以以下为例: http: //nbviewer.ipython.org/github/ellisonbg/talk-sicm2-2013/blob/master/NumbaCython.ipynb
[注意:由于问题的对称性,我可以将执行时间减半——但这不是我主要关心的问题]