問題描述
我有以下 numpy 數組:
I have the following numpy array:
import numpy as np
arr = np.array([[1,2,3,4,2000],
[5,6,7,8,2000],
[9,0,1,2,2001],
[3,4,5,6,2001],
[7,8,9,0,2002],
[1,2,3,4,2002],
[5,6,7,8,2003],
[9,0,1,2,2003]
])
我理解 np.sum(arr, axis=0)
提供結果:
array([ 40, 28, 36, 34, 16012])
我想做的(沒有for循環)是根據最后一列的值對列求和,以便提供的結果是:
what I would like to do (without a for loop) is sum the columns based on the value of the last column so that the result provided is:
array([[ 6, 8, 10, 12, 4000],
[ 12, 4, 6, 8, 4002],
[ 8, 10, 12, 4, 4004],
[ 14, 6, 8, 10, 4006]])
我意識到如果沒有循環可能會有些牽強,但希望能做到最好……
I realize that it may be a stretch to do without a loop, but hoping for the best...
如果必須使用 for 循環,那將如何工作?
If a for loop must be used, then how would that work?
我試過 np.sum(arr[:, 4]==2000, axis=0)
(我會用 for 循環中的變量替換 2000
),但是它給出了 2
I tried np.sum(arr[:, 4]==2000, axis=0)
(where I would substitute 2000
with the variable from the for loop), however it gave a result of 2
推薦答案
你可以在純 numpy 中使用 np.diff
和 np.add.reduceat
.np.diff
將為您提供最右側列更改的索引:
You can do this in pure numpy using a clever application of np.diff
and np.add.reduceat
. np.diff
will give you the indices where the rightmost column changes:
d = np.diff(arr[:, -1])
np.where
會將您的布爾索引 d
轉換為 np.add.reduceat
期望的整數索引:
np.where
will convert your boolean index d
into the integer indices that np.add.reduceat
expects:
d = np.where(d)[0]
reduceat
也期望看到零索引,并且所有內容都需要移動一:
reduceat
will also expect to see a zero index, and everything needs to be shifted by one:
indices = np.r_[0, e + 1]
使用 np.r_
這里比 方便一點np.concatenate
因為它允許標量.然后總和變為:
Using np.r_
here is a bit more convenient than np.concatenate
because it allows scalars. The sum then becomes:
result = np.add.reduceat(arr, indices, axis=0)
這當然可以組合成一條線:
This can be combined into a one-liner of course:
>>> result = np.add.reduceat(arr, np.r_[0, np.where(np.diff(arr[:, -1]))[0] + 1], axis=0)
>>> result
array([[ 6, 8, 10, 12, 4000],
[ 12, 4, 6, 8, 4002],
[ 8, 10, 12, 4, 4004],
[ 14, 6, 8, 10, 4006]])
這篇關于Numpy:條件總和的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!