我有以下功能,它需要一个浮点型数组和浮点数作为其参数。数组中的每一行'计数'是某个实验的结果,我想随机绘制一个实验列表并添加它们,然后重复此过程以创建大量样本组。优化功能切片numpy阵列
def my_function(counts,nSamples):
''' Create multiple randomly drawn (with replacement)
samples from the raw data '''
nSat,nRegions = counts.shape
sampleData = np.zeros((nSamples,nRegions))
for i in range(nSamples):
rc = np.random.randint(0,nSat,size=nSat)
sampleData[i] = counts[rc].sum(axis=0)
return sampleData
这个功能似乎很慢,通常计数有大约10万行(4列)和NSAMPLES是在2000年左右我一直在使用numba和隐式for循环,试图加快这个代码没有成功尝试。 有什么其他方法可以尝试提高速度?
我已经在函数上运行cProfile并获得了以下输出。
8005功能60.208秒
调用排序:标准名称
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 60.208 60.208 <string>:1(<module>)
2000 0.010 0.000 13.306 0.007 _methods.py:31(_sum)
1 40.950 40.950 60.208 60.208 optimize_bootstrap.py:25(bootstrap)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
2000 5.938 0.003 5.938 0.003 {method 'randint' of 'mtrand.RandomState' objects}
2000 13.296 0.007 13.296 0.007 {method 'reduce' of 'numpy.ufunc' objects}
2000 0.015 0.000 13.321 0.007 {method 'sum' of 'numpy.ndarray' objects}
1 0.000 0.000 0.000 0.000 {numpy.core.multiarray.zeros}
1 0.000 0.000 0.000 0.000 {range}
是的,我正在做一个引导分析 – Jack
我明白了。上述有帮助吗? –