How can I produce a nice output of a numpy matrix?
Asked Answered
U

1

10

I currently have the following snippet:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import numpy
from numpy import linalg

A = [[1,2,47,11],[3,2,8,15],[0,0,3,1],[0,0,8,1]]
S = [[113,49,2,283],[-113,0,3,359],[0,5,0,6],[0,20,0,12]]

A = numpy.matrix(A)
S = numpy.matrix(S)

numpy.set_printoptions(precision=2, suppress=True, linewidth=120)
print("S^{-1} * A * S")
print(linalg.inv(S) * A * S)

which produces this output:

python output

Is there a standard way to produce an output similar to the following? How can I get this output?

[[ -1    -0.33  0  0]
 [  0     1     0  0]
 [  0  -648     4  0]
 [  0     6.67  0  5]]

What's different?

  • At least two spaces between the last character of column i and the first character of column i+1, but it might be more if more is needed (NumPy output makes two spaces)
  • the dots are aligned (The are aligned, but the font setting of BetterPythonConsole messes it up)
  • No -0 but 0
  • No 0. but 0

edit: It seems as if the Python Console, which gets started with gEdits BetterPythonConsole plugin does something different than Python, when I start it from terminal.

This is the output as text of the script above

moose@pc07:~/Desktop$ python matrixScript.py 
S^{-1} * A * S
[[  -1.     -0.33    0.     -0.  ]
 [   0.     -1.     -0.      0.  ]
 [   0.   -648.      4.     -0.  ]
 [   0.      6.67    0.      5.  ]]

With prettyprint:

S^{-1} * A * S
matrix([[  -1.  ,   -0.33,    0.  ,   -0.  ],
        [   0.  ,   -1.  ,   -0.  ,    0.  ],
        [   0.  , -648.  ,    4.  ,   -0.  ],
        [   0.  ,    6.67,    0.  ,    5.  ]])

This is defenitely worse, but it was worth a try.

Unmentionable answered 10/9, 2012 at 8:23 Comment(4)
possible duplicate of Pretty-printing of numpy.arraySaleswoman
This is not a duplicate. I have read this question (and the answer), but it doesn't provide enough formatting.Unmentionable
Did you tried pprint? #1524160Keiko
@ZagorulkinDmitry thanks for the hint. Because of your hint I have executed it from terminal. I have edited my post. I didn't think of a wrong font setting of BetterPythonConsole.Unmentionable
V
4

If you use numpy 1.8.x you can customize formatting with the formatter parameter. For example, setting:

numpy.set_printoptions(formatter={'float': lambda x: 'float: ' + str(x)})

All floats would be printed like float: 3.0, or float: 12.6666666666.

Unfortunately I still have numpy 1.6.1 installed and this option is not provided, so I'm not able to use it to get your desired output.

Van answered 10/9, 2012 at 8:56 Comment(2)
I guess updating my system would be the best option as all other problems in the formating seem to be related to BetterPythonConsole.Unmentionable
Yes, probably the other option would be to write a custom format function to be used with numpy arrays. Which may not be too hard to write, but if you want to make it a bit general for possible future uses it's not something you write in 2 minutes. Hopefully the upgrade will give you other benefits other than this one.Van

© 2022 - 2024 — McMap. All rights reserved.