我对Python还是比较陌生,并且正在努力寻找一种执行以下操作的pythonic方法:
我有一个相对较大的四维数组(最小情况下为1000 x 1000 x 5 x 9)。从每个1000 x 1000矩阵(保持3和4不变)中,我想提取每列的前n个元素。但是n在列之间的变化类似于逐步函数。
因此,例如,我想要一个布尔掩码,看起来像下面的矩阵:
0 1 1 1 1
0 0 1 1 1
0 0 0 0 1
0 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
是否有一种有效且pythonic的方式来解决此问题?
非常感谢!
编辑:这是一个(希望是最少的)可复制示例:
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]