当我将元组转换为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]为什么?
发布于 2022-06-02 04:15:01
一种方法是设置如下:
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')发布于 2022-06-02 04:20:37
我认为您只看到显示中的截断,但内部值仍然保持更高的准确性。下面是我的发现:
>> 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发布于 2022-06-02 04:22:32
这种看似不一致的现象应该只是数字的显示方式,而不是它们是如何被表示/存储的。
您可以检查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]https://stackoverflow.com/questions/72470564
复制相似问题