Mar*_*oma 9 python formatting numpy gedit
我目前有以下代码段:
#!/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)
Run Code Online (Sandbox Code Playgroud)
产生这个输出:
是否有标准的方法来生成类似于以下的输出?我怎样才能得到这个输出?
[[ -1 -0.33 0 0]
[ 0 1 0 0]
[ 0 -648 4 0]
[ 0 6.67 0 5]]
Run Code Online (Sandbox Code Playgroud)
有什么不同?
i
的第一个字符之间至少有两个空格i+1
,但如果需要更多,则可能更多(NumPy输出生成两个空格)BetterPythonConsole
混乱它)-0
但是0
0.
但是0
编辑:当我从终端启动时,似乎Python控制台(使用gEdits BetterPythonConsole插件启动时)会执行与Python不同的操作.
这是上面脚本的文本输出
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. ]]
Run Code Online (Sandbox Code Playgroud)
使用prettyprint:
S^{-1} * A * S
matrix([[ -1. , -0.33, 0. , -0. ],
[ 0. , -1. , -0. , 0. ],
[ 0. , -648. , 4. , -0. ],
[ 0. , 6.67, 0. , 5. ]])
Run Code Online (Sandbox Code Playgroud)
这是更糟糕的,但值得一试.
如果您使用 numpy 1.8.x,您可以使用参数自定义格式formatter
。例如,设置:
numpy.set_printoptions(formatter={'float': lambda x: 'float: ' + str(x)})
Run Code Online (Sandbox Code Playgroud)
所有浮点数都将打印为float: 3.0
, 或float: 12.6666666666
。
不幸的是,我仍然安装了 numpy 1.6.1 并且未提供此选项,因此我无法使用它来获得您想要的输出。