3

I am working with masked numpy arrays and trying to print them out in a nice way for debugging purposes. I am setting the print options as below, but the output is not what I expect.

import numpy as np

np.set_printoptions(formatter={'float_kind': lambda x: "{0:0.3f}".format(x)})

x = np.random.uniform(size=(4,4))
xm = np.ma.array(x, mask=np.random.randint(0,2,size=(4,4)))

print(x)
print(xm)

The output:

[[0.877 0.504 0.518 0.156]
 [0.439 0.028 0.863 0.738]
 [0.516 0.614 0.439 0.597]
 [0.164 0.953 0.427 0.923]]
[[0.876728440179007 -- -- 0.1564739272031952]
 [-- 0.028450171766788213 -- --]
 [-- -- -- 0.5972907957825376]
 [-- 0.95334588487527 0.4273250291466033 --]]

The masked array does not get printed to 3dp. Is this feature missing from numpy.ma? If anyone knows a work around (short of writing my own print function) it would be much appreciated!

BenedictWilkins
  • 1,173
  • 8
  • 25
  • I'd look at the `ma` docs, and failing that the actual code. With those dashes, it can't be using the standard array formatter, at least not directly. – hpaulj Nov 21 '19 at 15:32
  • You can even up the lines by fiddling with the `---` display, for example, `np.ma.masked_print_option.set_display('=== ') `. I found this by looking at the `xm.__str__` code (and going down several layers). – hpaulj Nov 21 '19 at 16:53
  • 1
    Looks like the key formatter function is `xm._insert_masked_print()`. This makes an object dtype array with the masked values replaced by the '---' string. – hpaulj Nov 21 '19 at 17:08

0 Answers0