Fastest way to zero out low values in array?

后端 未结 9 1555
忘掉有多难
忘掉有多难 2020-12-24 07:07

So, lets say I have 100,000 float arrays with 100 elements each. I need the highest X number of values, BUT only if they are greater than Y. Any element not matching this

9条回答
  •  醉酒成梦
    2020-12-24 07:33

    This is a typical job for NumPy, which is very fast for these kinds of operations:

    array_np = numpy.asarray(array)
    low_values_flags = array_np < lowValY  # Where values are low
    array_np[low_values_flags] = 0  # All low values set to 0
    

    Now, if you only need the highCountX largest elements, you can even "forget" the small elements (instead of setting them to 0 and sorting them) and only sort the list of large elements:

    array_np = numpy.asarray(array)
    print numpy.sort(array_np[array_np >= lowValY])[-highCountX:]
    

    Of course, sorting the whole array if you only need a few elements might not be optimal. Depending on your needs, you might want to consider the standard heapq module.

提交回复
热议问题