If you want the output to be
array([1.6e-01, 9.9e-01, 3.6e-04])
the problem is not with the missing NumPy function, but with the fact that this kind of rounding is not standard. You can make your own rounding function, which achieves this as follows:
def my_round(value, N): exponent = np.ceil(np.log10(value)) return 10**exponent*np.round(value*10**(-exponent), N)
For general handling of solution 0 and negative values, you can do something like this:
def my_round(value, N): value = np.asarray(value).copy() zero_mask = (value == 0) value[zero_mask] = 1.0 sign_mask = (value < 0) value[sign_mask] *= -1 exponent = np.ceil(np.log10(value)) result = 10**exponent*np.round(value*10**(-exponent), N) result[sign_mask] *= -1 result[zero_mask] = 0.0 return result
source share