在二维数组查找多个最大值快 [英] Find multiple maximum values in a 2d array fast
问题描述
情况如下:
我有一个二维数组numpy的。它的形状是(1002,1004)。每个元素包含0和INF之间的值。我现在想要做的是确定的第一个1000的最大值和相应的指数存储到一个列表名为x和一个名为ÿ列表。这是因为我要绘制的最大值和实际指数对应于实时x和值y位置。
I have a 2D numpy array. Its shape is (1002, 1004). Each element contains a value between 0 and Inf. What I now want to do is determine the first 1000 maximum values and store the corresponding indices in to a list named x and a list named y. This is because I want to plot the maximum values and the indices actually correspond to real time x and y position of the value.
我至今是:
x = numpy.zeros(500)
y = numpy.zeros(500)
for idx in range(500):
x[idx] = numpy.unravel_index(full.argmax(), full.shape)[0]
y[idx] = numpy.unravel_index(full.argmax(), full.shape)[1]
full[full == full.max()] = 0.
print os.times()
下面全是我的2D numpy的数组。如从可见for循环中,我只此刻确定第一500最大值。然而,这已经大约需要5秒。对于第一个1000的最大值,用户的时间实际上应该是围绕0.5秒。我注意到一个非常耗时的部分是设置了previous最大每次值0。我怎样才能加快速度?
Here full is my 2D numpy array. As can be seen from the for loop, I only determine the first 500 maximum values at the moment. This however already takes about 5 s. For the first 1000 maximum values, the user time should actually be around 0.5 s. I've noticed that a very time consuming part is setting the previous maximum value to 0 each time. How can I speed things up?
感谢你了!
推荐答案
如果您有numpy的1.8,可以使用<$c$c>argpartition$c$c>函数或方法。
下面是计算脚本X
和是
:
If you have numpy 1.8, you can use the argpartition
function or method.
Here's a script that calculates x
and y
:
import numpy as np
# Create an array to work with.
np.random.seed(123)
full = np.random.randint(1, 99, size=(8, 8))
# Get the indices for the largest `num_largest` values.
num_largest = 8
indices = (-full).argpartition(num_largest, axis=None)[:num_largest]
# OR, if you want to avoid the temporary array created by `-full`:
# indices = full.argpartition(full.size - num_largest, axis=None)[-num_largest:]
x, y = np.unravel_index(indices, full.shape)
print "full:"
print full
print "x =", x
print "y =", y
print "Largest values:", full[x, y]
print "Compare to: ", np.sort(full, axis=None)[-num_largest:]
输出:
full:
[[67 93 18 84 58 87 98 97]
[48 74 33 47 97 26 84 79]
[37 97 81 69 50 56 68 3]
[85 40 67 85 48 62 49 8]
[93 53 98 86 95 28 35 98]
[77 41 4 70 65 76 35 59]
[11 23 78 19 16 28 31 53]
[71 27 81 7 15 76 55 72]]
x = [0 2 4 4 0 1 4 0]
y = [6 1 7 2 7 4 4 1]
Largest values: [98 97 98 98 97 97 95 93]
Compare to: [93 95 97 97 97 98 98 98]
这篇关于在二维数组查找多个最大值快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!