2

numpy配列aがありa.shape=(17,90,144)ます。の各列の最大の大きさを見つけたいのですcumsum(a, axis=0)が、元の符号を保持しています。つまり、特定の列a[:,j,i]の最大の大きさがcumsum負の値に対応する場合、マイナス記号を保持したいと思います。

コードnp.amax(np.abs(a.cumsum(axis=0)))は私に大きさを与えますが、符号を保持しません。代わりに使用すると、必要なインデックスが取得され、元の配列np.argmaxにプラグインできます。cumsumしかし、私はそうするための良い方法を見つけることができません。

次のコードは機能しますが、汚くて本当に遅いです:

max_mag_signed = np.zeros((90,144))
indices = np.argmax(np.abs(a.cumsum(axis=0)), axis=0)
for j in range(90):
    for i in range(144):
        max_mag_signed[j,i] = a.cumsum(axis=0)[indices[j,i],j,i]

これを行うには、よりクリーンで高速な方法が必要です。何か案は?

4

1 に答える 1

4

代替案を見つけることはできませんargmaxが、少なくとも、よりベクトル化されたアプローチでそれを固定することができます。

# store the cumsum, since it's used multiple times
cum_a = a.cumsum(axis=0)

# find the indices as before
indices = np.argmax(abs(cum_a), axis=0)

# construct the indices for the second and third dimensions
y, z = np.indices(indices.shape)

# get the values with np indexing
max_mag_signed = cum_a[indices, y, z]
于 2013-02-10T01:04:21.603 に答える