Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
numpython:为另一列中的每个唯一值从一列中查找最高值_Python_Numpy - Fatal编程技术网

numpython:为另一列中的每个唯一值从一列中查找最高值

numpython:为另一列中的每个唯一值从一列中查找最高值,python,numpy,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]

有人能提出一种有效的方法,在一列中为另一列中的每个唯一值获取最高值吗

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]]
样本运行-

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

哇,这种方法正是我想要的。再次感谢您@Divakar我真的很感谢您的帮助。你很快就回答了我很多新手的问题