numpy数组如何将数组中的元素批量保留小数(或精确到指定位)?np.around()函数(与np.round()函数等价?)
生活随笔
收集整理的这篇文章主要介绍了
numpy数组如何将数组中的元素批量保留小数(或精确到指定位)?np.around()函数(与np.round()函数等价?)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
From numpy\core\fromnumeric.py
def around(a, decimals=0, out=None):"""Evenly round to the given number of decimals.平等地舍入到给定的小数位数。Parameters----------a : array_likeInput data.decimals : int, optionalNumber of decimal places to round to (default: 0). Ifdecimals is negative, it specifies the number of positions tothe left of the decimal point.要舍入的小数位数(默认值:0)。 如果 decimals 值为负数,则指定小数点左侧的位数。out : ndarray, optionalAlternative output array in which to place the result. It must havethe same shape as the expected output, but the type of the outputvalues will be cast if necessary. See `doc.ufuncs` (Section"Output arguments") for details.放置结果的替代输出数组。 它必须具有与预期输出相同的形状,但是如有必要,将强制转换输出值的类型。 有关详细信息,请参见`doc.ufuncs`(“输出参数”部分)。Returns-------rounded_array : ndarrayAn array of the same type as `a`, containing the rounded values.Unless `out` was specified, a new array is created. A reference tothe result is returned.The real and imaginary parts of complex numbers are roundedseparately. The result of rounding a float is a float.与“ a”类型相同的数组,其中包含四舍五入的值。 除非指定了`out`,否则将创建一个新数组。 返回对结果的引用。复数的实部和虚部分别取整。 将浮点数舍入的结果是一个浮点数。See Also--------ndarray.round : equivalent methodceil, fix, floor, rint, truncNotes-----For values exactly halfway between rounded decimal values, NumPyrounds to the nearest even value. Thus 1.5 and 2.5 round to 2.0,-0.5 and 0.5 round to 0.0, etc. Results may also be surprising dueto the inexact representation of decimal fractions in the IEEEfloating point standard [1]_ and errors introduced when scalingby powers of ten.对于恰好介于四舍五入的十进制值之间的值,NumPy会四舍五入为最接近的偶数。 因此,将1.5和2.5四舍五入为2.0,将-0.5和0.5四舍五入为0.0,等等。由于IEEE浮点标准[1] _中小数部分的不精确表示以及以10的幂进行缩放时引入的误差,结果也可能令人惊讶。 。References----------.. [1] "Lecture Notes on the Status of IEEE 754", William Kahan,http://www.cs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF.. [2] "How Futile are Mindless Assessments ofRoundoff in Floating-Point Computation?", William Kahan,http://www.cs.berkeley.edu/~wkahan/Mindless.pdfExamples-------->>> np.around([0.37, 1.64])array([ 0., 2.])>>> np.around([0.37, 1.64], decimals=1)array([ 0.4, 1.6])>>> np.around([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value 四舍五入到最接近的偶数值array([ 0., 2., 2., 4., 4.])>>> np.around([1,2,3,11], decimals=1) # ndarray of ints is returned 返回int的ndarrayarray([ 1, 2, 3, 11])>>> np.around([1,2,3,11], decimals=-1)array([ 0, 0, 0, 10])"""return _wrapfunc(a, 'round', decimals=decimals, out=out)参考文章1:【numpy】【ndarray】指定每个元素保留小数点后多少位【around】【round】
参考文章2:numpy数组中round, around, rint, ceil, floor, modf, trunc, fix
总结
以上是生活随笔为你收集整理的numpy数组如何将数组中的元素批量保留小数(或精确到指定位)?np.around()函数(与np.round()函数等价?)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 【程序员の英文听写】Trump’s To
- 下一篇: Numpy中np.dot()与np.ma