gpt4 book ai didi

python - NumPy python : Find the highest value from a column for each unique value in another column

转载 作者:太空宇宙 更新时间:2023-11-03 15:58:14 24 4
gpt4 key购买 nike

有人可以建议一种有效的方法来为另一列中的每个唯一值获取一列中的最高值

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]]

我知道如何通过循环来做到这一点,但这不是我要处理的,因为我正在使用的表非常大,我想避免循环

最佳答案

这是一种方法-

# 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]]

sample 运行-

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]] 进行切片以使用相同的内存空间,从而希望也能带来性能改进。让我们验证内存 View -

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

关于python - NumPy python : Find the highest value from a column for each unique value in another column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41990566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com