I have a 2D numpy array like
x = np.array([[3,3],[3,1]])
Out[107]:
array([[3, 3],
[3, 1]])
I would like to format it to print percentages of the total:
array([['33.3%', '33.3%'],
['33.3%', '10.0%']]
I've tried some solutions from here and here but have yet to get these to work:
pct_formatter = lambda x: "{:.2%}".format(x/x.sum() * 100 )
pct_formatter(x)
TypeError: non-empty format string passed to object.__format__
another attempt:
with np.set_printoptions(formatter=pct_formatter):
print(pct_formatter(x))
AttributeError: __exit__