我正在处理一些非常大的数组。我正在处理的一个问题当然是内存不足,但即使在此之前我的代码运行缓慢,所以即使我有无限的内存,它仍然需要太长时间。我将给出一些代码来展示我正在尝试做的事情:
#samplez is a 3 million element 1-D array
#zfit is a 10,000 x 500 2-D array
b = np.arange((len(zfit))
for x in samplez:
a = x-zfit
mask = np.ma.masked_array(a)
mask[a <= 0] = np.ma.masked
index = mask.argmin(axis=1)
# These past 4 lines give me an index array of the smallest positive number
# in x - zift
d = zfit[b,index]
e = zfit[b,index+1]
f = (x-d)/(e-d)
# f is the calculation I am after
if x == samplez[0]:
g = f
index_stack = index
else:
g = np.vstack((g,f))
index_stack = np.vstack((index_stack,index))
在进一步计算中,我需要使用 g 和 index_stack,每个都是 300 万 x 10,000 二维数组。这个循环的每次迭代几乎需要 1 秒,所以总共需要 300 万秒,这太长了。
我能做些什么让这个计算运行得更快吗?我试图思考如果没有这个 for 循环我该怎么办,但我能想象的唯一方法是制作 300 万份 zfit,这是不可行的。
有没有办法通过不将所有内容都保存在 RAM 中来处理这些数组?我是初学者,我搜索的所有内容要么无关紧要,要么我无法理解。提前致谢。