In [34]: a
Out[34]: [[1, 15.02], [2, 14.38], [3, 14.6]]
In [35]: b=np.array(a, dtype=float).T
In [36]: b
Out[36]:
array([[ 1. , 2. , 3. ],
[ 15.02, 14.38, 14.6 ]])
In [37]: list(zip(*b))
Out[37]: [(1.0, 15.02), (2.0, 14.380000000000001), (3.0, 14.6)]
However if I first past b
through tolist
:
In [38]: list(zip(*b.tolist()))
Out[38]: [(1.0, 15.02), (2.0, 14.38), (3.0, 14.6)]
In the first case the elements of the tuple still have np.float64
wrapper, while tolist
extracts them all to native Python numbers:
In [39]: type(list(zip(*b))[1][1])
Out[39]: numpy.float64
In [40]: type(list(zip(*b.tolist()))[1][1])
Out[40]: float
item
is another way of extracting the native number:
In [41]: list(zip(*b))[1][1]
Out[41]: 14.380000000000001
In [42]: list(zip(*b))[1][1].item()
Out[42]: 14.38
I can't say why the setprintoptions
doesn't apply in the case of np.float64
, but does with np.array
.
As a general rule, it is better to use tolist()
if you want to convert an array, and all its values, into a native Python list. Operations like list and zip aren't enough. They iterate on the first dimension of the array, but don't recursively convert the elements:
Partial conversion(s):
In [43]: list(b)
Out[43]: [array([ 1., 2., 3.]), array([ 15.02, 14.38, 14.6 ])]
In [44]: list(b[1])
Out[44]: [15.02, 14.380000000000001, 14.6]
Full conversion:
In [45]: b.tolist()
Out[45]: [[1.0, 2.0, 3.0], [15.02, 14.38, 14.6]]
Apparently the formatter for float64
shows all precision, regardless of the set_printoptions
values:
In [58]: 14.380000000000001
Out[58]: 14.38
In [59]: np.array(14.380000000000001)
Out[59]: array(14.38)
In [60]: np.float64(14.380000000000001)
Out[60]: 14.380000000000001
In [61]: np.float32(14.380000000000001)
Out[61]: 14.38
An np.float64(...)
object is, in many ways like a single item array, but different in subtle ways. Usually though we don't create such an object directly.