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.

set_printoptions does not work with numpy structured arrays

See original GitHub issue

I want to set the print precision for a numpy structured array, but it does not seem to work.

H = np.zeros(1, dtype=[('x','float',2),('f','float')])
H['x']= np.random.uniform(0,1,(1,2))
H['f']= np.random.uniform(0,1)
np.set_printoptions(precision=3)
print(H)

leaves

[([0.5928384015048476, 0.6187004067605606], 0.2981617279773586)]

not

[([0.593, 0.619], 0.298)]

as I expected. Printing H[‘x’] works (because that’s not a structured array), but I have many fields in my structured array and I don’t want to have to print each one when monitoring the progress of my code.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:9
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
eric-wiesercommented, Jun 24, 2020

Numpy has no concept of a “mixed” array, only of object arrays containing elements of unknown types. Your array would probably work better as a structured array (what this issue is actually about), because then numpy keeps track of the type of each column separately.

0reactions
michael135commented, Jun 24, 2020

Thank you Eric!

Read more comments on GitHub >

github_iconTop Results From Across the Web

set_printoptions for numpy array doesn't work ... - Stack Overflow
Try numpy.array2string which takes ndarray as input and you can set precision. Scroll down in below documentation link for examples.
Read more >
numpy.set_printoptions — NumPy v1.24 Manual
Set printing options. These options determine the way floating point numbers, arrays and other NumPy objects are displayed. ... If not None, the...
Read more >
numpy.fromregex — NumPy v1.23 Manual
Construct an array from a text file, using regular expression parsing. The returned array is always a structured array, and is constructed from...
Read more >
numpy.loadtxt — NumPy v1.24 Manual
When used with a structured data-type, arrays are returned for each field. Default is False. ndminint, optional. The returned array will have at...
Read more >
NumPy quickstart — NumPy v1.25.dev0 Manual
To work the examples, you'll need matplotlib installed in addition to NumPy. ... Note that numpy.array is not the same as the Standard...
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