如何使 set_printoptions(suppress=True) 永久有效?

发布于 2024-08-12 04:43:20 字数 932 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半寸时光 2024-08-19 04:43:20

我将其添加到 ~/.ipython/ipy_user_conf.py 中的 main() 函数中:

from numpy import set_printoptions
set_printoptions(suppress = True)

它似乎有效。

在更高版本的 IPython 中,运行 ipython profile create,然后打开 ~\.ipython\profile_default\ipython_config.py 并编辑以下行以添加命令:

c.InteractiveShellApp.exec_lines = [
        ...
        'import numpy as np',
        'np.set_printoptions(suppress=True)',
        ...
        ]

I added this to the main() function in ~/.ipython/ipy_user_conf.py:

from numpy import set_printoptions
set_printoptions(suppress = True)

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:

c.InteractiveShellApp.exec_lines = [
        ...
        'import numpy as np',
        'np.set_printoptions(suppress=True)',
        ...
        ]
ま昔日黯然 2024-08-19 04:43:20

您可以将它们添加到您的 ipythonrc 文件中(在 Unix 上位于 ~/.ipython 中)。您需要这些行:

import_mod numpy
execute numpy.set_printoptions(suppress = True)

您还可以将其添加到自定义配置文件或使用其他配置方法:

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:

import_mod numpy
execute numpy.set_printoptions(suppress = True)

You can also add it to a custom profile or use another configuration method:

http://ipython.scipy.org/doc/stable/html/config/customization.html

高跟鞋的旋律 2024-08-19 04:43:20

好吧,简化它的一种方法是在 $PYTHONPATH 的某个位置创建一个小模块,例如 printopts ,其中包含:

import numpy
numpy.set_printoptions(suppress = True)

然后每当您想要更改打印时导入该模块。您还可以在代码中将 numpy 导入为 from printopts import numpy。这样你只需要一个 import 语句。

干杯。

附录:仅用于交互式使用的解决方案是将 $PYTHONSTARTUP 环境变量设置为 printopts.py 文件的路径。在交互模式下,解释器会先执行该文件,然后再执行其他操作。当然,那么 python 将总是加载 numpy,从而影响启动时间。

考虑更多后,我要做的是创建一个模块,例如 np.py 包含

from numpy import *
set_printoptions(supress=True)

然后总是 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:

import numpy
numpy.set_printoptions(suppress = True)

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 containing

from numpy import *
set_printoptions(supress=True)

Then 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文