Fastest way to zero out low values in array?

后端 未结 9 1553
忘掉有多难
忘掉有多难 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

    Settings elements below some threshold to zero is easy:

    array = [ x if x > threshold else 0.0 for x in array ]
    

    (plus the occasional abs() if needed.)

    The requirement of the N highest numbers is a bit vague, however. What if there are e.g. N+1 equal numbers above the threshold? Which one to truncate?

    You could sort the array first, then set the threshold to the value of the Nth element:

    threshold = sorted(array, reverse=True)[N]
    array = [ x if x >= threshold else 0.0 for x in array ]
    

    Note: this solution is optimized for readability not performance.

提交回复
热议问题