简体   繁体   English

使用索引数组进行有效循环

[英]Efficient looping using index array

If I have an index array with all unique sequential values, such as: 如果我有一个具有所有唯一顺序值的索引数组,例如:

index_array = array([0, 4, 2, 5, 6, 1, 3, 7, 8])

with a corresponding value array: 带有对应的值数组:

value_array = array([0, 400, 200, 500 600, 100, 300, 700, 800])

is it possible to loop through the index array in sequential order, such that I get 是否有可能按顺序循环遍历索引数组,所以我得到

array([0, 100, 200, 300, 400, 500, 600, 700, 800])

I need to loop through the index array in order (ie 0, 1, 2, 3, 4...) with the corresponding values (ie 0, 100, 200, 300, 400). 我需要按顺序(即0、1、2、3、4 ...)与相应的值(即0、100、200、300、400)循环遍历索引数组。 The reason these values aren't in order is because I'm subdividing edges, which means the new edges are added at the end of the index array (using vstack), rather than inserted into the index array at the appropriate point. 这些值不按顺序排列的原因是因为我要细分边缘,这意味着新边缘将添加到索引数组的末尾(使用vstack),而不是在适当的位置插入索引数组。

The pseudocode (if I was printing out the values), would be something like this: 伪代码(如果我要打印出这些值)将是这样的:

for point in sorted(index_array):

    print sorted(point(value_array))

producing: 生产:

0

100

200

300

How memory sensitive is this (I'm guessing I would need to use numpy.where) Is it just better practice to reorder before looping, or is there a performance cost to looping out of order? 这对内存有多敏感(我猜我将需要使用numpy.where)是在循环之前重新排序只是更好的做法,还是会导致性能问题而导致混乱?

Approach #1 方法1

Get the argsort for index array and index into values array - 获取索引数组的argsort并索引到values数组中-

value_array[index_array.argsort()]

Sample run - 样品运行-

In [129]: value_array
Out[129]: array([   0,  400,  200,  500,  600,  100,  300,  700, 800])

In [130]: index_array
Out[130]: array([0, 4, 2, 5, 6, 1, 3, 7, 8])

In [131]: value_array[index_array.argsort()]
Out[131]: array([   0,  100,  200,  300,  400,  500,  600,  700, 800])

Approach #2 Abusing the fact that all elements in index_array are unique and sequential, a much faster way would be to initialize an output array and use those indices to index and assign those values from value_array into it, like so - 方法#2利用index_array中所有元素都是唯一且顺序的事实,一种更快的方法是初始化输出数组,并使用这些索引来索引并将value_array那些值分配给它,如下所示-

def assign_unique_seq(value_array, index_array):
    out = np.empty_like(value_array)
    out[index_array] = value_array
    return out

Runtime test - 运行时测试-

In [152]: value_array = np.random.randint(0,1000000,(100000))

# Create unique and sequential indices array
In [153]: index_array = np.random.permutation(len(value_array))

In [154]: %timeit value_array[index_array.argsort()]
100 loops, best of 3: 7.84 ms per loop

In [155]: %timeit assign_unique_seq(value_array, index_array)
1000 loops, best of 3: 240 µs per loop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM