[Numpy-discussion] Numpy set_printoptions, silent failure, bug?
jladasky at itu.edu
Tue Jul 19 17:41:04 EDT 2016
I've been using Numpy for several years and appreciate it very much.
The following minimal code has been tried on Python 3.4 and 3.5, with Numpy
1.8 and Numpy 1.11, respectively. I want to temporarily change the way
that a Numpy array is printed, then change it back.
import numpy as np
a = np.random.random((4,3))
opt = np.get_printoptions()
np.set_printoptions(precision = 3, suppress = True)
Here is the traceback:
Traceback (most recent call last):
File "set_printoptions test.py", line 11, in <module>
File "/usr/lib/python3/dist-packages/numpy/core/numeric.py", line 1615,
return array2string(a, max_line_width, precision, suppress_small, ' ',
File "/usr/lib/python3/dist-packages/numpy/core/arrayprint.py", line 454,
separator, prefix, formatter=formatter)
File "/usr/lib/python3/dist-packages/numpy/core/arrayprint.py", line 328,
File "/usr/lib/python3/dist-packages/numpy/core/arrayprint.py", line 523,
File "/usr/lib/python3/dist-packages/numpy/core/arrayprint.py", line 497,
word = format_function(a[-i]) + separator
File "/usr/lib/python3/dist-packages/numpy/core/arrayprint.py", line 616,
s = self.format % x
AttributeError: 'FloatFormat' object has no attribute 'format'
It took me a while to discover the bug, on the second to last line. That
line should read:
This unpacks the dictionary, opt, which was retrieved earlier in the
program with the call to get_printoptions.
I am wondering why set_printoptions would accept the dictionary as a single
argument. Shouldn't that raise a TypeError or something?
The AttributeError that is raised the next time that one attempts to print
a numpy array is remote from the problem, and thus rather uninformative,
which is why I did not immediately diagnose my error.
Should this be considered a Numpy bug, or is there some reason that
set_printoptions would legitimately need to accept a dictionary as a single
*John J. Ladasky Jr., Ph.D.*
*International Technological University*
*2711 N. First St, San Jose, CA 95134 USA*
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion