tuple to numpy, data accuracy
Question:
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?
Answers:
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')
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.
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]
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?
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')
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.
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]