我对Python比较陌生,正在努力寻找一种Python方式来完成以下工作:
我有一个相对较大的四维数组(最小情况下为1000 x 1000 x 5 x 9)。在每1000 x 1000矩阵中(保持dim 3和4不变),我想提取每列的前n个元素。但n在列之间的变化类似于一个逐步函数
因此,例如,我想要一个布尔掩码,它看起来像以下矩阵:
01
011
01
0 0 0 0
然而,我读到索引应该比布尔掩蔽更快。因此,我当前的方法是使用大量for循环:
def getElements(index,L):
"""
index - a (T x T x K x A)-array, smallest case is T = 1,000, K = 5, A = 9,
goes up to T = 50,000, K = 20, A = 10
L - a list used to compute the fraction of column elements extracted, containing
numbers from 1 to ~10
"""
T = index.shape[0]
K = index.shape[2]
A = index.shape[3]
dimL = max(L)
elem = np.empty((T,T,K*dimL,A))
elem[:] = np.nan
m = -1
for kk in range(K):
for ll in L:
m +=1
p = ll/(dimL+1)
for aa in range(A):
for tt in range(T):
n = min(math.floor(tt*p),T-kk) # Floor function leads to stepwise mask/ indexing
elem[:n, tt, m, aa] = index[:n, tt, kk, aa]
elem[:, :, m, aa] += (kk+1)
return elem
有没有一种有效的方法来解决这个问题
非常感谢
编辑:下面是一个(希望最小)可复制的示例:
index = np.arange(100000)
index = np.reshape(index,(100,100,10))
T = index.shape[0]
K = index.shape[2]
L = 2
elem = np.full((T,T,K*L), np.nan)
m = -1
for kk in range(K):
for ll in range(L):
m +=1
p = (ll+1)/(L+1)
for tt in range(T):
n = min(math.floor((tt+1)*p),T-kk) # tt+1 as python counts from zero
elem[:n, tt, m] = index[:n, tt, kk]
目前没有回答
相关问题 更多 >
编程相关推荐