有人可以建议一种有效的方法,在另一个
中为每个唯一值获取一列中的最高值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]]
我知道如何通过循环来做到这一点,但这不是我正在照顾的,因为我正在工作的表非常大,我想避免循环
答案 0 :(得分:3)
这是一种方法 -
# 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