简体   繁体   English

Python:沿着特定维度查找最大数组索引,该索引大于阈值

[英]Python: Find largest array index along a specific dimension which is greater than a threshold

Lets say I have a 4-D numpy array (ex: np.rand((x,y,z,t)) ) of data with dimensions corresponding to X,Y,Z, and time. 可以说我有一个4-D numpy数组(例如: np.rand((x,y,z,t)) ),其维度对应于X,Y,Z和时间。

For each X and Y point, and at each time step, I want to find the largest index in Z for which the data is larger than some threshold n . 对于每个X和Y点,以及每个时间步长,我想在Z中找到数据大于某个阈值n的最大索引。

So my end result should be an X-by-Y-by-t array. 所以我的最终结果应该是X-by-Y-by-t数组。 Instances where there are no values in the Z-column greater than the threshold should be represented by a 0. Z列中没有大于阈值的实例应以0表示。

I can loop through element-by-element and construct a new array as I go, however I am operating on a very large array and it takes too long. 我可以逐个元素地循环遍历并构造一个新的数组,但是我在一个非常大的数组上进行操作,它花费的时间太长。

Unfortunately, following the example of Python builtins, numpy doesn't make it easy to get the last index, although the first is trivial. 不幸的是,按照Python内置函数的示例,尽管 一个索引很琐碎,但numpy并不容易获得最后一个索引。 Still, something like 还是这样

def slow(arr, axis, threshold):
    return (arr > threshold).cumsum(axis=axis).argmax(axis=axis)

def fast(arr, axis, threshold):
    compare = (arr > threshold)
    reordered = compare.swapaxes(axis, -1)
    flipped = reordered[..., ::-1]
    first_above = flipped.argmax(axis=-1)
    last_above = flipped.shape[-1] - first_above - 1
    are_any_above = compare.any(axis=axis)
    # patch the no-matching-element found values
    patched = np.where(are_any_above, last_above, 0)
    return patched

gives me 给我

In [14]: arr = np.random.random((100,100,30,50))

In [15]: %timeit a = slow(arr, axis=2, threshold=0.75)
1 loop, best of 3: 248 ms per loop

In [16]: %timeit b = fast(arr, axis=2, threshold=0.75)
10 loops, best of 3: 50.9 ms per loop

In [17]: (slow(arr, axis=2, threshold=0.75) == fast(arr, axis=2, threshold=0.75)).all()
Out[17]: True

(There's probably a slicker way to do the flipping but it's the end of day here and my brain is shutting down. :-) (可能有一种比较轻松的方式来进行翻转,但这已经是一天的结束了,我的大脑正在关闭。:-)

Here's a faster approach - 这是一种更快的方法-

def faster(a,n,invalid_specifier):
    mask = a>n    
    idx = a.shape[2] - (mask[:,:,::-1]).argmax(2) - 1
    idx[~mask[:,:,-1] & (idx == a.shape[2]-1)] = invalid_specifier  
    return idx

Runtime test - 运行时测试-

# Using @DSM's benchmarking setup

In [553]: a = np.random.random((100,100,30,50))
     ...: n = 0.75
     ...: 

In [554]: out1 = faster(a,n,invalid_specifier=0)
     ...: out2 = fast(a, axis=2, threshold=n) # @DSM's soln
     ...: 

In [555]: np.allclose(out1,out2)
Out[555]: True

In [556]: %timeit fast(a, axis=2, threshold=n)  # @DSM's soln
10 loops, best of 3: 64.6 ms per loop

In [557]: %timeit faster(a,n,invalid_specifier=0)
10 loops, best of 3: 43.7 ms per loop

暂无
暂无

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

相关问题 检查 python 数组的值是否大于阈值 - Check python array for values greater than a threshold 如何找到具有大于阈值的一定数量值的窗口索引? - How to find index of the window which has certain number of values greater than threshold? 在Python中,如何在排序列表中找到大于阈值的第一个值的索引? - In Python, how do you find the index of the first value greater than a threshold in a sorted list? 查找数组位置大于阈值的位置,而下一个位置小于阈值(numpy) - Find array position greater than a threshold where the next position is less than the threshold (numpy) Python中的生命线布尔索引与维度0上的索引数组不匹配; 维度为88,但相应的布尔维度为76 - Lifelines boolean index in Python did not match indexed array along dimension 0; dimension is 88 but corresponding boolean dimension is 76 python IndexError: boolean 索引与维度 0 上的索引数组不匹配; 尺寸为 32,但对应的 boolean 尺寸为 112 - python IndexError: boolean index did not match indexed array along dimension 0; dimension is 32 but corresponding boolean dimension is 112 在pandas中,如何找到累积和大于阈值的行/索引? - In pandas, how to find the row/index where the cumulative sum is greater than a threshold? 在 +ve 斜率中找到大于阈值的值的索引的最佳方法 - best way to find the index of value greater than threshold value in +ve slope Python,如何找到numpy数组中大于指定值的最低元素的列索引 - Python, how to find the column index of the lowest element greater than a specified value in a numpy array 如何找到总和大于目标值的数组的索引? - How to find the index of an array where summation is greater than a target value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM