如何使 set_printoptions(suppress=True) 永久有效?
numpy 中有一个函数可以使数组打印更漂亮。
set_printoptions(suppress = True)
换句话说,而不是这样:
array([[ 0.00000000e+00, -3.55271368e-16, 0.00000000e+00,
1.74443793e-16, 9.68149172e-17],
[ 5.08273978e-17, -4.42527959e-16, 1.57859836e-17,
1.35982590e-16, 5.59918137e-17],
[ 3.00000000e+00, 6.00000000e+00, 9.00000000e+00,
2.73835608e-16, 7.37061982e-17],
[ 2.00000000e+00, 4.00000000e+00, 6.00000000e+00,
4.50218574e-16, 2.87467529e-16],
[ 1.00000000e+00, 2.00000000e+00, 3.00000000e+00,
2.75582605e-16, 1.88929494e-16]])
你会得到这样的:
array([[ 0., -0., 0., 0., 0.],
[ 0., -0., 0., 0., 0.],
[ 3., 6., 9., 0., 0.],
[ 2., 4., 6., 0., 0.],
[ 1., 2., 3., 0., 0.]])
如何使此设置永久生效,以便每当我使用 IPython 时它都会执行此操作?
In numpy there is a function that makes arrays print prettier.
set_printoptions(suppress = True)
In other words, instead of this:
array([[ 0.00000000e+00, -3.55271368e-16, 0.00000000e+00,
1.74443793e-16, 9.68149172e-17],
[ 5.08273978e-17, -4.42527959e-16, 1.57859836e-17,
1.35982590e-16, 5.59918137e-17],
[ 3.00000000e+00, 6.00000000e+00, 9.00000000e+00,
2.73835608e-16, 7.37061982e-17],
[ 2.00000000e+00, 4.00000000e+00, 6.00000000e+00,
4.50218574e-16, 2.87467529e-16],
[ 1.00000000e+00, 2.00000000e+00, 3.00000000e+00,
2.75582605e-16, 1.88929494e-16]])
You get this:
array([[ 0., -0., 0., 0., 0.],
[ 0., -0., 0., 0., 0.],
[ 3., 6., 9., 0., 0.],
[ 2., 4., 6., 0., 0.],
[ 1., 2., 3., 0., 0.]])
How do I make this setting permanent so it does this whenever I'm using IPython?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将其添加到
~/.ipython/ipy_user_conf.py
中的main()
函数中:它似乎有效。
在更高版本的 IPython 中,运行
ipython profile create
,然后打开~\.ipython\profile_default\ipython_config.py
并编辑以下行以添加命令:I added this to the
main()
function in~/.ipython/ipy_user_conf.py
:and it seems to work.
In later IPython versions, run
ipython profile create
, then open~\.ipython\profile_default\ipython_config.py
and edit the following line to add the command:您可以将它们添加到您的
ipythonrc
文件中(在 Unix 上位于~/.ipython
中)。您需要这些行:您还可以将其添加到自定义配置文件或使用其他配置方法:
http://ipython.scipy.org/doc/stable/html/config/customization.html
You can add those to your
ipythonrc
file (located in~/.ipython
on Unix). You'd need the lines:You can also add it to a custom profile or use another configuration method:
http://ipython.scipy.org/doc/stable/html/config/customization.html
好吧,简化它的一种方法是在 $PYTHONPATH 的某个位置创建一个小模块,例如
printopts
,其中包含:然后每当您想要更改打印时导入该模块。您还可以在代码中将 numpy 导入为
from printopts import numpy
。这样你只需要一个 import 语句。干杯。
附录:
仅用于交互式使用的解决方案是将 $PYTHONSTARTUP 环境变量设置为 printopts.py 文件的路径。在交互模式下,解释器会先执行该文件,然后再执行其他操作。当然,那么 python 将总是加载 numpy,从而影响启动时间。考虑更多后,我要做的是创建一个模块,例如
np.py
包含然后总是
import np
来获取修改后的版本,原因在我下面的评论中。如果您不介意黑客攻击,只需将 set_printoptions() 调用添加到 numpy/__init__.py 文件中,但您必须具有对 numpy 安装的写入权限,并记住在以下情况下重复黑客攻击:你更新 python 或 numpy。我认为这不是一个好主意。
Well, one way to streamline it would be to create a wee module somewhere on your $PYTHONPATH,
printopts
say, containing:And then import that whenever you want to change the printing. You could also import numpy in your code as
from printopts import numpy
. That way you'd only need one import statement.Cheers.
ADDENDUM:
A solution for interactive use only is to set the $PYTHONSTARTUP environment variable to the path to the printopts.py file. The interpreter executes that file before anything else, when in interactive mode. Of course, then python will always load numpy, hurting start times.Having considered a little more, what I would do is create a module, say
np.py
containingThen always
import np
to get your modified version, for the reason in my comment below.If you don't mind a hack, just add the set_printoptions() call to
the numpy/__init__.py
file, but you have to have write access to the numpy installation and remember to repeat the hack when you update python or numpy. I don't think this is a good idea.