获取最大或最小正元素出numpy的阵列? (preferably不平坦的) [英] Get max or min n-elements out of numpy array? (preferably not flattened)
问题描述
我知道,我能得到min或以最高值:
I know that I can get min or max values with:
max(matrix)
min(matrix)
出numpy的矩阵/矢量。对于那些瓦莱斯的索引将返回:
out of a numpy matrix/vector. The indices for those vales are returned by:
argmax(matrix)
argmin(matrix)
所以,例如当我有一个5x5的矩阵:
So e.g. when I have a 5x5 matrix:
a = np.arange(5*5).reshape(5, 5) + 10
# array([[10, 11, 12, 13, 14],
# [15, 16, 17, 18, 19],
# [20, 21, 22, 23, 24],
# [25, 26, 27, 28, 29],
# [30, 31, 32, 33, 34]])
我可以通过获得最大值:
I could get the max value via:
In [86]: np.max(a) # getting the max-value out of a
Out[86]: 34
In [87]: np.argmax(a) # index of max-value 34 is 24 if array a were flattened
Out[87]: 24
...但什么是最有效的方式来获得最大或最小正元素?
...but what is the most efficient way to get the max or min n-elements?
所以我们说出来的在的我想有5最高和最低的5要素。这将返回我的 [30,31,32,33,34]
分别为5最高值 [20,21,22,23,24 ]
为他们的指数。同样 [10,11,12,13,14]
的5最低值和 [0,1,2,3,4]
的5最低元素的索引。
So let's say out of a I want to have the 5 highest and 5 lowest elements. This should return me [30, 31, 32, 33, 34]
for the 5 highest values respectively [20, 21, 22, 23, 24]
for their indices. Likewise [10, 11, 12, 13, 14]
for the 5 lowest values and [0, 1, 2, 3, 4]
for the indices of the 5 lowest elements.
什么是一个高效,合理的解决方案,这?
What would be an efficient, reasonable solution for this?
我的第一个想法为扁平化和分选阵列,并采取姓氏和5的值。后来我通过这些值的指数原来的二维矩阵搜索。 虽然此过程适用于扁平化+分拣效率不高......没有人知道一个更快的解决方案?
My first idea was flattening and sorting the array and taking the last and first 5 values. Afterwards I search through the original 2D matrix for the indices of those values. Although this procedure works flattening + sorting isn't very efficient...does anyone know a faster solution?
此外,我想有原来的二维数组,而不是扁平之一的指标。因此,而不是 24
按 np.argmax返回(一)
我想有( 4,4)
。
Additionally I would like to have the indices of the original 2D array and not the flattening one. So instead of 24
returned by np.argmax(a)
I would like to have (4, 4)
.
推荐答案
要获得最大或最小值的索引数组中的标准方法是使用的 np.argpartition
。此函数使用introselect算法和线性复杂运行 - 这比执行完全分拣更大的阵列更好(通常为O(n log n)的)
The standard way to get the indices of the largest or smallest values in an array is to use np.argpartition
. This function uses an introselect algorithm and runs with linear complexity - this performs better than fully sorting for larger arrays (which is typically O(n log n)).
默认该函数沿着阵列的最后一个轴起作用。要考虑整个数组,你需要使用 拉威尔()
。例如,这里有一个随机序列 A
:
By default this function works along the last axis of the array. To consider an entire array, you need to use ravel()
. For example, here's a random array a
:
>>> a = np.random.randint(0, 100, size=(5, 5))
>>> a
array([[60, 68, 86, 66, 9],
[66, 26, 83, 87, 50],
[41, 26, 0, 55, 9],
[57, 80, 71, 50, 22],
[94, 30, 95, 99, 76]])
然后得到最大的五个值的指数的(扁平化)二维数组中,使用:
Then to get the indices of the five largest values in the (flattened) 2D array, use:
>>> i = np.argpartition(a.ravel(), -5)[-5:] # argpartition(a.ravel(), 5)[:5] for smallest
>>> i
array([ 2, 8, 22, 23, 20])
要取回这些位置的对应2D指数在 A
,使用的 unravel_index
:
To get back the corresponding 2D indices of these positions in a
, use unravel_index
:
>>> i2d = np.unravel_index(i, a.shape)
>>> i2d
(array([0, 1, 4, 4, 4]), array([2, 3, 2, 3, 0]))
然后索引 A
与 I2D
还给五大价值观:
>>> a[i2d]
array([86, 87, 95, 99, 94])
这篇关于获取最大或最小正元素出numpy的阵列? (preferably不平坦的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!