有人能建议一种有效的方法来为另一列中的每一个唯一值求出最高的值吗?
np.array看起来像column0,column1,column2,column3
[[ 37367 421 231385 93]
[ 37368 428 235156 93]
[ 37369 408 234251 93]
[ 37372 403 196292 93]
[ 55523 400 247141 139]
[ 55575 415 215818 139]
[ 55576 402 204404 139]
[ 69940 402 62244 175]
[ 69941 402 38274 175]
[ 69942 404 55171 175]
[ 69943 416 55495 175]
[ 69944 407 90231 175]
[ 69945 411 75382 175]
[ 69948 405 119129 175]]
其中,我希望根据第3列的唯一值返回第1列的最高值。
[[ 37368 428 235156 93]
[ 55575 415 215818 139]
[ 69943 416 55495 175]]
我知道如何通过循环来做这件事,但这不是我要处理的,因为我正在工作的桌子很大,我想避免循环。
发布于 2017-02-01 14:11:04
这里有一个方法-
# Lex-sort combining cols-1,3 with col-3 setting the primary order
sidx = np.lexsort(a[:,[1,3]].T)
# Indices at intervals change for column-3. These would essentially
# tell us the last indices for each group in a lex-sorted array
idx = np.append(np.flatnonzero(a[1:,3] > a[:-1,3]), a.shape[0]-1)
# Finally, index into idx with lex-sorted indices to give us
# the last indices in a lex-sorted version, which is equivalent
# of picking up the highest of each group
out = a[sidx[idx]]
样本运行-
In [234]: a # Input array
Out[234]:
array([[ 25, 29, 19, 93],
[ 27, 59, 14, 93],
[ 24, 46, 15, 93],
[ 79, 87, 50, 139],
[ 13, 86, 32, 139],
[ 56, 25, 85, 142],
[ 62, 62, 68, 142],
[ 27, 25, 20, 150],
[ 29, 53, 71, 150],
[ 64, 67, 21, 150],
[ 96, 57, 73, 150]])
In [235]: out # Output array
Out[235]:
array([[ 27, 59, 14, 93],
[ 79, 87, 50, 139],
[ 62, 62, 68, 142],
[ 64, 67, 21, 150]])
使用视图提高性能
我们可以使用a[:,1::2]
而不是a[:,[1,3]]
来使用相同的内存空间,从而也有望带来性能的提高。让我们验证一下内存视图-
In [240]: np.may_share_memory(a,a[:,[1,3]])
Out[240]: False
In [241]: np.may_share_memory(a,a[:,1::2])
Out[241]: True
https://stackoverflow.com/questions/41990566
复制相似问题