I want to truncate the float values within the numpy array, for .e.g.
2.34341232 --> 2.34
I read the post truncate floating point but i
Try out this modified version of numpy.trunc().
import numpy as np
def trunc(values, decs=0):
return np.trunc(values*10**decs)/(10**decs)
Sadly, numpy.trunc
function doesn't allow decimal truncation. Luckily, multiplying the argument and dividing it's result by a power of ten give the expected results.
vec = np.array([-4.79, -0.38, -0.001, 0.011, 0.4444, 2.34341232, 6.999])
trunc(vec, decs=2)
which returns:
>>> array([-4.79, -0.38, -0. , 0.01, 0.44, 2.34, 6.99])
Use numpy.round:
import numpy as np
a = np.arange(4) ** np.pi
a
=> array([ 0. , 1. , 8.82497783, 31.5442807 ])
a.round(decimals=2)
=> array([ 0. , 1. , 8.82, 31.54])