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

标签 python numpy

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

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/

相关文章:

python - 无法读取使用不同方法写入的文件的内容

python - 在 Atom 上运行 Python 代码时获取 "EOFError"

python - 将具有张量特征的 tf.train.Dataset 序列化到 tfrecord 文件中?

python - 如何获取特定列表元素的平均值

python - 如何在 Python 中对列表或元组进行 'unpack'

python - 矩阵的次要

python - CherryPy 如何读取分块编码的请求正文

python - Plist一直加载不结束,满足条件也不退出

python - 将 OCamCalib 工具箱 Matlab 脚本转换为 Python

python - 使用已知函数 numpy 进行曲线拟合