我有两个大型2d numpy数组,它们具有相同的行数但具有不同的列数。假设arr1的形状为(num_rows1,num_cols1),而arr2的形状为(num_rows1,num_cols2)。
我预分配了一个大小为(num_rows1,num_cols1 + num_cols2)的numpy数组arr12。
将arr1和arr2复制到arr12中以使arr1与arr2串联的最有效方法是什么?
使用这种预分配方法是否比numpy的串联方法更有效?
我们将跨各种数据集进行基准测试并从中得出结论。
使用benchit
软件包(打包在一起的基准测试工具很少;免责声明:我是它的作者)对建议的解决方案进行基准测试。
基准测试代码:
import numpy as np
import benchit
def numpy_concatenate(a, b):
return np.concatenate((a,b),axis=1)
def numpy_hstack(a, b):
return np.hstack((a,b))
def preallocate(a, b):
m,n = a.shape[1], b.shape[1]
out = np.empty((a.shape[0],m+n), dtype=np.result_type((a.dtype, b.dtype)))
out[:,:m] = a
out[:,m:] = b
return out
funcs = [numpy_concatenate, numpy_hstack, preallocate]
R = np.random.rand
inputs = {n: (R(1000,1000), R(1000,n)) for n in [100, 200, 500, 1000, 200, 5000]}
t = benchit.timings(funcs, inputs, multivar=True, input_name='Col length of b')
t.plot(logy=False, logx=True, savepath='plot_1000rows.png')
结论:它们在时间上是可比的。
在内存方面,np.hstack
应类似于np.concatenate
。因此,我们将使用其中之一。
让我们使用大型2D数组设置输入数据集。我们将做一些内存基准测试。
设置代码:
# Filename : memprof_npconcat_preallocate.py
import numpy as np
from memory_profiler import profile
@profile(precision=10)
def numpy_concatenate(a, b):
return np.concatenate((a,b),axis=1)
@profile(precision=10)
def preallocate(a, b):
m,n = a.shape[1], b.shape[1]
out = np.empty((a.shape[0],m+n), dtype=np.result_type((a.dtype, b.dtype)))
out[:,:m] = a
out[:,m:] = b
return out
R = np.random.rand
a,b = R(1000,1000), R(1000,1000)
if __name__ == '__main__':
numpy_concatenate(a, b)
if __name__ == '__main__':
preallocate(a, b)
因此,a
是1000x1000,与相同b
。
跑 :
$ python3 -m memory_profiler memprof_npconcat_preallocate.py
Filename: memprof_npconcat_preallocate.py
Line # Mem usage Increment Line Contents
================================================
9 69.3281250000 MiB 69.3281250000 MiB @profile(precision=10)
10 def numpy_concatenate(a, b):
11 84.5546875000 MiB 15.2265625000 MiB return np.concatenate((a,b),axis=1)
Filename: memprof_npconcat_preallocate.py
Line # Mem usage Increment Line Contents
================================================
13 69.3554687500 MiB 69.3554687500 MiB @profile(precision=10)
14 def preallocate(a, b):
15 69.3554687500 MiB 0.0000000000 MiB m,n = a.shape[1], b.shape[1]
16 69.3554687500 MiB 0.0000000000 MiB out = np.empty((a.shape[0],m+n), dtype=np.result_type((a.dtype, b.dtype)))
17 83.6484375000 MiB 14.2929687500 MiB out[:,:m] = a
18 84.4218750000 MiB 0.7734375000 MiB out[:,m:] = b
19 84.4218750000 MiB 0.0000000000 MiB return out
因此,对于preallocate
method,mem的总消耗为14.2929687500
+ 0.7734375000
,该消耗略小于15.2265625000
。
将输入数组的大小都更改为5000x5000,a
并且b
-
$ python3 -m memory_profiler memprof_npconcat_preallocate.py
Filename: memprof_npconcat_preallocate.py
Line # Mem usage Increment Line Contents
================================================
9 435.4101562500 MiB 435.4101562500 MiB @profile(precision=10)
10 def numpy_concatenate(a, b):
11 816.8515625000 MiB 381.4414062500 MiB return np.concatenate((a,b),axis=1)
Filename: memprof_npconcat_preallocate.py
Line # Mem usage Increment Line Contents
================================================
13 435.5351562500 MiB 435.5351562500 MiB @profile(precision=10)
14 def preallocate(a, b):
15 435.5351562500 MiB 0.0000000000 MiB m,n = a.shape[1], b.shape[1]
16 435.5351562500 MiB 0.0000000000 MiB out = np.empty((a.shape[0],m+n), dtype=np.result_type((a.dtype, b.dtype)))
17 780.3203125000 MiB 344.7851562500 MiB out[:,:m] = a
18 816.9296875000 MiB 36.6093750000 MiB out[:,m:] = b
19 816.9296875000 MiB 0.0000000000 MiB return out
同样,预分配的总数较少。
结论:预分配方法具有更好的内存优势,这在某种程度上是有意义的。使用串联时,我们有三个涉及src1 + src2-> dst的数组,而使用预分配时,只有src和dst具有较少的内存拥塞,尽管分两个步骤。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句