【问题标题】:Difference in precision in np.float64 and python float (64)np.float64 和 python float (64) 中的精度差异
【发布时间】:2020-08-12 17:02:55
【问题描述】:

为什么(或看起来)python float 比numpy.float64 具有更高的精度。这里我举个例子:

import numpy as np

ann = 72.595895691
detections = np.array([0, 71.91000009999999], dtype=np.float64)
group_det = [1]
dist1 = [ann - detections[det] for det in group_det]
dist = np.zeros(len(group_det), dtype=np.float64)
for idx, det in enumerate(group_det):
    dist[idx] = ann - detections[det]
print(f'dist1: {dist1}')
print(f'dist {dist}')

这段代码的输出是:

dist1: [0.6858955910000049]
dist [0.68589559]

我的问题是为什么 dist1 的精度更高?

注意:代码似乎以一种非常复杂的方式做事,但我不得不以相同的行为剪切我的代码的一小部分。

【问题讨论】:

  • 这是显示差异

标签: python numpy printing precision


【解决方案1】:

正如@hpaulj 在 cmets 中提到的,它不是计算精度。它正在打印。将此行添加到您的代码中,以查看您在 numpy 中打印时想要的精度:

np.set_printoptions(precision=16)

在上面一行之后的代码输出:

dist1: [0.6858955910000049]
dist [0.6858955910000049]

【讨论】:

    猜你喜欢
    • 2012-07-18
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 2015-08-31
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    相关资源
    最近更新 更多