[英]Improving performance by avoiding python for cycle when using counts of numpy unique
我有兩個 numpy 數組, A
的形狀為(N,3)
,B 的形狀為(N,)
,我從向量 A 生成具有唯一條目的向量,例如:
A = np.array([[1.,2.,3.],
[4.,5.,6.],
[1.,2.,3.],
[7.,8.,9.]])
B = np.array([10.,33.,15.,17.])
AUnique, directInd, inverseInd, counts = np.unique(A,
return_index = True,
return_inverse = True,
return_counts = True,
axis = 0)
所以AUnique
將是array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]])
然后我得到simil矢量B
關聯到AUnique
,並且對於每個非唯一行中A
我總結的相關聯的值B
在該載體中,即:
BNew = B[directInd]
# here BNew is [10., 33.,17]
for Id in np.asarray(counts>1).nonzero()[0]:
BNew[Id] = np.sum(B[inverseInd == Id])
# here BNew is [25., 33.,17]
問題是對於大 N 個向量(數百萬或數千萬行),for 循環變得非常慢,我想知道是否有辦法避免循環和/或使代碼更快。
提前致謝!
我認為你可以用np.bincount
做你想做的np.bincount
BNew = np.bincount(inverseInd, weights = B)
BNew
Out[]: array([25., 33., 17.])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.