元组到numpy,数据准确性

发布于 2025-02-03 15:09:22 字数 372 浏览 2 评论 0原文

当我将元组转换为Numpy时,数据准确性存在问题。我的代码就是这样:

import numpy as np
a=(0.547693688614422, -0.7854270889025808, 0.6267478456110592)
print(a)
print(type(a))
tmp=np.array(a)
print(tmp)

结果就是这样:

(0.547693688614422, -0.7854270889025808, 0.6267478456110592)
<class 'tuple'>
[ 0.54769369 -0.78542709  0.62674785]

为什么?

When I convert a tuple to numpy, there is a problem with data accuracy. My code is like this:

import numpy as np
a=(0.547693688614422, -0.7854270889025808, 0.6267478456110592)
print(a)
print(type(a))
tmp=np.array(a)
print(tmp)

The result is like this:

(0.547693688614422, -0.7854270889025808, 0.6267478456110592)
<class 'tuple'>
[ 0.54769369 -0.78542709  0.62674785]

Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼眸 2025-02-10 15:09:22

似乎差异应该只是显示数字的方式,而不是它们的表示 /存储方式。

您可以检查dtype以验证它仍然是float64

tmp.dtype  # dtype('float64')

您可以调整np.set_printoptions以查看它们的值以不同的方式显示

print(tmp)  # [ 0.54769369 -0.78542709  0.62674785]
np.set_printoptions(precision=18)  # default precision is 8
print(tmp)  # [ 0.547693688614422  -0.7854270889025808  0.6267478456110592]

This seeming discrepancy should just be how the numbers are being displayed, not how they are being represented / stored.

You can check the dtype to verify it is still float64

tmp.dtype  # dtype('float64')

You can adjust np.set_printoptions to see them values displayed differently

print(tmp)  # [ 0.54769369 -0.78542709  0.62674785]
np.set_printoptions(precision=18)  # default precision is 8
print(tmp)  # [ 0.547693688614422  -0.7854270889025808  0.6267478456110592]
最初的梦 2025-02-10 15:09:22

我认为您仅在 display 中看到截断,但是内部值仍然保留原始精度。这是我发现的:

>> a
(0.547693688614422, -0.7854270889025808, 0.6267478456110592)

>> b=np.array(a)

>> b
array([ 0.54769369, -0.78542709,  0.62674785]) #<-- printed display shows lower accuracy

>> b[0]
0.547693688614422 #<-- print of a single value shows same accuracy as original

因此,没有理由更改任何设置 - 使用这些阵列执行的数学仍将完全准确。

I think you're only seeing a truncation in display only, but the internal value still retains the original accuracy. Here's what I found:

>> a
(0.547693688614422, -0.7854270889025808, 0.6267478456110592)

>> b=np.array(a)

>> b
array([ 0.54769369, -0.78542709,  0.62674785]) #<-- printed display shows lower accuracy

>> b[0]
0.547693688614422 #<-- print of a single value shows same accuracy as original

So there's no reason to change any settings - math performed with these arrays will still be at full accuracy.

娜些时光,永不杰束 2025-02-10 15:09:22

一种方法是设置以下方式:

In [1039]: np.set_printoptions(precision=20)

In [1041]: tmp=np.array(a)

In [1042]: tmp
Out[1042]: array([ 0.547693688614422 , -0.7854270889025808,  0.6267478456110592])

In [1043]: tmp.dtype
Out[1043]: dtype('float64')

One way is to set this:

In [1039]: np.set_printoptions(precision=20)

In [1041]: tmp=np.array(a)

In [1042]: tmp
Out[1042]: array([ 0.547693688614422 , -0.7854270889025808,  0.6267478456110592])

In [1043]: tmp.dtype
Out[1043]: dtype('float64')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文