3
votes

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 each field does work, 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.

1
print(H[0][0]) works, I can't remember how to get it to work on subelements - user1121588
print(H['x']) also works (since H['x'] is not a structured array), but it would be nice if print(H) used the precision set by set_printoptions(). This might be a numpy bug. You can create an issue here: github.com/numpy/numpy/issues (hit the green "New issue" button). - Warren Weckesser
The issue is now open. I will post updates if I receive any. - jmlarson

1 Answers

0
votes

This has been resolved in recent numpy versions:

https://github.com/numpy/numpy/issues/5606