np.round 或 np.around 不起作用

ann*_*nne 7 python arrays numpy

我有这个数组:

arr = np.array([66.5, 89.4000015, 57.2000008, 86.9000015, 64.5999985,
      92.3000031, 74.1999969, 76.0999985, 92.0999985, 81.6999969,
      72.0999985, 78.8000031, 81.4000015, 95.4000015, 73.5      ,
      58.5999985, 68.3000031, 68.9000015, 68.6999969, 92.       ])
Run Code Online (Sandbox Code Playgroud)

我尝试对每个数字进行四舍五入,并使用np.around

[in]  np.around(arr, 2)

[out] array([66.5, 89.4, 57.2, 86.9, 64.6, 92.3, 74.2, 76.1, 92.1, 81.7, 72.1,
   78.8, 81.4, 95.4, 73.5, 58.6, 68.3, 68.9, 68.7, 92. ])

[in]  np.around(arr, 4)

[out] array([66.5, 89.4, 57.2, 86.9, 64.6, 92.3, 74.2, 76.1, 92.1, 81.7, 72.1,
   78.8, 81.4, 95.4, 73.5, 58.6, 68.3, 68.9, 68.7, 92. ])

[in] np.around(arr, 5)
[out] array([66.5, 89.4, 57.2, 86.9, 64.6, 92.3, 74.2, 76.1, 92.1, 81.7, 72.1,
   78.8, 81.4, 95.4, 73.5, 58.6, 68.3, 68.9, 68.7, 92. ])

[in]  np.around(arr, 6)

[out] array([66.5     , 89.400002, 57.200001, 86.900002, 64.599998, 92.300003,
   74.199997, 76.099998, 92.099998, 81.699997, 72.099998, 78.800003,
   81.400002, 95.400002, 73.5     , 58.599998, 68.300003, 68.900002,
   68.699997, 92.      ])
Run Code Online (Sandbox Code Playgroud)

当小数位数小于 时5,则np.around()不起作用。当它大于 时6,则np.around运行良好。

任何帮助表示赞赏。

安妮

Luc*_*tti 5

方法:圆形

根据您描述的“意外行为”,我相信您还不清楚哪一轮操作或它们对数字的影响或如何将浮点数格式化为字符串。

n=4让我们探讨一下舍入为不同数字值时的差异。

基线

我们定义了一个包含 13 个值的数组(13 个值只是为了获取一些数字),从 0 到 1。

values = np.linspace(0, 1, 13)
Run Code Online (Sandbox Code Playgroud)

该数组包含:

array([0.        , 0.08333333, 0.16666667, 0.25      , 0.33333333,
       0.41666667, 0.5       , 0.58333333, 0.66666667, 0.75      ,
       0.83333333, 0.91666667, 1.        ])
Run Code Online (Sandbox Code Playgroud)

numpy.around

numpy.around当第n+1大于或等于时,将增加第 n个数字 的值,否则不执行任何操作。它与 相同。5numpy.round

np.round(values, n)
>>> array([0.    , 0.0833, 0.1667, 0.25  , 0.3333, 0.4167, 0.5   , 0.5833,
   0.6667, 0.75  , 0.8333, 0.9167, 1.    ])
Run Code Online (Sandbox Code Playgroud)

numpy.ceil

numpy.ceil当存在数字时,将增加整数部分的值并删除数字。

np.ceil(r)
>>> array([0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
Run Code Online (Sandbox Code Playgroud)

numpy.floor

numpy.floor只会删除数字。

np.floor(r)
>>> array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.])
Run Code Online (Sandbox Code Playgroud)

数字字符串格式

将数字格式化为字符串的方法有很多种:我们将探讨一些更常用的方法。

格式化为浮点数

要格式化浮点数,您可以使用符号.nf,其中n是您要保留的位数。它将删除所有后续数字,并在需要时添加零作为填充。

[ "{:0.4f}".format(v) for v in r]
>>> ['0.0000', '0.0833', '0.1667', '0.2500', '0.3333', '0.4167', '0.5000',
     '0.5833', '0.6667', '0.7500', '0.8333', '0.9167', '1.0000']
Run Code Online (Sandbox Code Playgroud)

格式化为百分比

要将浮点数格式化为百分比,请使用符号.n%,其中n是要保留的位数,考虑到该数字将乘以100。它将删除所有后续数字,并在需要时添加零作为填充。

[ "{:0.4%}".format(v) for v in r]
>>> ['0.0000%', '8.3333%', '16.6667%', '25.0000%', '33.3333%', '41.6667%',
'50.0000%', '58.3333%', '66.6667%', '75.0000%', '83.3333%', '91.6667%',
'100.0000%']
Run Code Online (Sandbox Code Playgroud)

以科学记数法格式化

要将浮点数格式化为百分比,请使用符号.ne,其中n是要保留的位数,考虑到该数字将转换为科学记数法。它将删除后面的所有数字,并在需要时添加零作为填充,并在末尾添加科学记数法中数字的指数。

[ "{:e}".format(v) for v in r]
>>> [
    '0.0000e+00', '8.3333e-02', '1.6667e-01', '2.5000e-01', '3.3333e-01',
    '4.1667e-01', '5.0000e-01', '5.8333e-01', '6.6667e-01', '7.5000e-01',
    '8.3333e-01', '9.1667e-01', '1.0000e+00'
]
Run Code Online (Sandbox Code Playgroud)

奖励:格式化复数

假设您有一个复数a = 3j+2:要打印其组成部分,您可以通过访问其属性来继续:

"The real component is {0.real} and the imaginary one is {0.imag}".format(a)
>>> 'The real component is 2.0 and the imaginary one is 3.0'
Run Code Online (Sandbox Code Playgroud)