How can I produce good numpy matrix output?

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 conclusion:

python output

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

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

What is the difference?

  • At least two spaces between the last character of column i and the first character of column i+1 , but this can be more if more is required (NumPy output makes two spaces)
  • points are aligned (they are aligned, but setting the BetterPythonConsole font messed it up)
  • No -0 but 0
  • No 0. but 0

edit . It seems that the Python console, which is launched using gEdits BetterPythonConsole , does something different from Python when I start it from the terminal.

This is the result as the script text 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 a beautiful print:

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

It is impeccably worse, but worth a try.

+8
python numpy formatting gedit
source share
1 answer

If you are using numpy 1.8.x, you can configure formatting using the formatter parameter. For example, installation:

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

All floats will be printed as float: 3.0 or float: 12.6666666666 .

Unfortunately, I still have numpy 1.6.1 installed, and this option is not provided, so I can not use it to get the desired result.

+4
source share

All Articles