Python / Numpy - set values ​​within a certain value to a value

If I have an array and I want to set the "close" values ​​for some value as that value, what is the best way to do this? I wonder if for them this is a sterile function. If there is no numpy function, then this is the code below the “best” (ie. Fastest / most efficient) way to do this? It also works for multidimensional arrays.

code:

from numpy import array tol = 1e-5 # Some array with values close to 0 and 1 t = array([1.0e-10, -1.0e-10, 1.0+1.0e-10, 1.0-1.0e-10, 5.0]) print t[0], t[1], t[2], t[3], t[4] # Set values within 'tol' of zero to zero t[abs(t) < tol] = 0. print t[0], t[1], t[2], t[3], t[4] # Set values within 'tol' of some value to that value val = 1. t[abs(t-val) < tol] = val print t[0], t[1], t[2], t[3], t[4] 
+4
source share
2 answers

It’s not entirely clear what you are trying to achieve, but my interpretation is that around is the solution for your case.

+3
source

Source: https://habr.com/ru/post/1342871/


All Articles