question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

np.set_printoptions doesn't affect floats

See original GitHub issue
>>> import numpy as np
>>> '%s' % np.array(1/3)
'0.3333333333333333'
>>> '%s' % np.array([1/3])
'[0.33333333]'
>>> np.set_printoptions(formatter={"float_kind": lambda x: "%.1f" % x})
>>> '%s' % np.array(1/3)
'0.3333333333333333'
>>> '%s' % np.array([1/3])
'[0.3]'

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
eric-wiesercommented, May 7, 2018

A simpler reproduction is:

>>> np.set_printoptions(formatter={"float_kind": lambda x: "%.1f" % x})
>>> str(np.float64(1/3))
str(np.float64(1/3))

@ahaldane, do you think this is a feature (full precision is always shown for scalars) or an omission?

0reactions
ahaldanecommented, Oct 23, 2018

For the record, this behavior goes back as far as numpy 1.0.1, and probably before.

#9201 shows our attempts to update the str/repr of 0d arrays and why we had to keep this behavior. As I recall it was due to people expecting that str applied to a 0d array of S or U dtype would always give back the string itself.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - printoptions formatter not working for a single element ...
set_printoptions control how NumPy prints arrays. They do not affect how NumPy scalars (such as numpy.complex128 , numpy.float32 , etc) are ...
Read more >
numpy.set_printoptions — NumPy v1.24 Manual
If True, always print floating point numbers using fixed point notation, in which case numbers equal to zero in the current precision will...
Read more >
[Numpy-discussion] set_printoptions precision and single floats
doesn't affect single floats, even >> if they are numpy floats rather than Python floats. Is this a bug or is >> there...
Read more >
numpy.set_printoptions — NumPy v1.10 Manual
formatter is always reset with a call to set_printoptions. Examples. Floating point precision can be set: >>> >>> np.
Read more >
Numpy Random - angela1c.com
Explain the use of the numpy.random package in Python including detailed ... __version__ ) # '1.16.2' #np.set_printoptions(formatter={'float': lambda x: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found