Question

I have some math operations that produce a numpy array of results with about 8 significant figures. When I use tolist() on my array y_axis, it creates what I assume are 32-bit numbers.

However, I wonder if this is just garbage. I assume it is garbage, but it seems intelligent enough to change the last number so that rounding makes sense.

print "y_axis:",y_axis
y_axis = y_axis.tolist()
print "y_axis:",y_axis

y_axis: [-0.99636686  0.08357361 -0.01638707]
y_axis: [-0.9963668578012771, 0.08357361233570479, -0.01638706796138937]

So my question is: if this is not garbage, does using tolist actually help in accuracy for my calculations, or is Python always using the entire number, but just not displaying it?

Était-ce utile?

La solution

When you call print y_axis on a numpy array, you are getting a truncated version of the numbers that numpy is actually storing internally. The way in which it is truncated depends on how numpy's printing options are set.

>>> arr = np.array([22/7, 1/13])           # init array
>>> arr                                    # np.array default printing
array([ 3.14285714,  0.07692308])
>>> arr[0]                                 # int default printing
3.1428571428571428
>>> np.set_printoptions(precision=24)      # increase np.array print "precision"
>>> arr                                    # np.array high-"precision" print
array([ 3.142857142857142793701541,  0.076923076923076927347012])
>>> float.hex(arr[0])                      # actual underlying representation
'0x1.9249249249249p+1'

The reason it looks like you're "gaining accuracy" when you print out the .tolist()ed form of y_axis is that by default, more digits are printed when you call print on a list than when you call print on a numpy array.

In actuality, the numbers stored internally by either a list or a numpy array should be identical (and should correspond to the last line above, generated with float.hex(arr[0])), since numpy uses numpy.float64 by default, and python float objects are also 64 bits by default.

Autres conseils

My understanding is that numpy is not showing you the full precision to make the matrices lay out consistently. The list shouldn't have any more precision than its numpy.array counterpart:

>>> v = -0.9963668578012771
>>> a = numpy.array([v])
>>> a
array([-0.99636686])
>>> a.tolist()
[-0.9963668578012771]
>>> a[0] == v
True
>>> a.tolist()[0] == v
True
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top