-2

I have a numpy.ndarray X. I want to find all the places in X that X>threshold, and then do X=threshold.

What is the cheapest way (in terms of time-complexity) to do this? I need to run this procedure millions of times. Thanks!

user_3.14
  • 61
  • 1
  • 4

2 Answers2

0

As far as I know, you can use the numpy indexing in order to replace all elements that bigger than some threshold.

Although, I'm not sure it's the fastest way.

threshold = 10 # for example
some_array[some_array > threshold] = threshold
omri_saadon
  • 10,193
  • 7
  • 33
  • 58
0

try numpy.where:

    from numpy import where

    Y  = where( X> treshold, threshold,X)

where applies and if statement in the ufunc-manner with where( condition, if True, else )