我正在寻找通过间隔函数组装numpy数组的快速方法。这是我现在的代码(简化),它耗费了大量时间来创建子数组:
import numpy as np
def func():
a = np.random.random(10000) # avarage size of my arrays
b = np.random.random(10000)
r = np.zeros_like(a)
ind = np.where(a > 0.1)[0] # some data close to limits is always cut
ind = ind[a[ind] < 0.9]
ind2 = ind[a[ind] <= 0.3] # first interval
r[ind2] = a[ind2] * b[ind2] # exemplary calculation
ind = ind[ind != ind2][0]
ind2 = ind[a[ind] <= 0.8] # second interval
r[ind2] = a[ind2] * b[ind2] * 5
ind = ind[ind != ind2][0]
ind2 = ind[a[ind] > 0.8] # even up to 20 intervals (can be done in loop,
# here shown separated for clarity)
r[ind2] = a[ind2] * b[ind2] * 10
return r
我会感激每一个提示。