防止ipython将输出存储在Out变量中 [英] Prevent ipython from storing outputs in Out variable

查看:102
本文介绍了防止ipython将输出存储在Out变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用熊猫和ipython.由于在执行操作时会复制熊猫数据帧,因此每个单元格的内存使用量增加了500 mb.我相信是因为数据存储在Out变量中,因为默认的python解释器不会发生这种情况.

I'm currently working with pandas and ipython. Since pandas dataframes are copied when you perform operations with it, my memory usage increases by 500 mb with every cell. I believe it's because the data gets stored in the Out variable, since this doesn't happen with the default python interpreter.

如何禁用Out变量?

推荐答案

第一个选择是避免产生输出.如果您真的不需要查看,则只需避免中间结果,然后将所有计算结果放在一个单元格中即可.

The first option you have is to avoid producing output. If you don't really need to see the intermediate results just avoid them and put all the computations in a single cell.

如果需要实际显示该数据,可以使用InteractiveShell.cache_size选项设置缓存的最大大小.将此值设置为0将禁用缓存.

If you need to actually display that data you can use InteractiveShell.cache_size option to set a maximum size for the cache. Setting this value to 0 disables caching.

为此,您必须在~/.ipython/profile_default目录下创建一个名为ipython_config.py(或ipython_notebook_config.py)的文件,其内容如下:

To do so you have to create a file called ipython_config.py (or ipython_notebook_config.py) under your ~/.ipython/profile_default directory with the contents:

c = get_config()

c.InteractiveShell.cache_size = 0

之后,您会看到:

In [1]: 1
Out[1]: 1

In [2]: Out[1]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-2-d74cffe9cfe3> in <module>()
----> 1 Out[1]

KeyError: 1

您还可以使用命令ipython profile create <name>为ipython创建不同的配置文件.这将在~/.ipython/profile_<name>下使用默认配置文件创建一个新的配置文件.然后,您可以使用--profile <name>选项启动ipython来加载该配置文件.

You can also create different profiles for ipython using the command ipython profile create <name>. This will create a new profile under ~/.ipython/profile_<name> with a default configuration file. You can then launch ipython using the --profile <name> option to load that profile.

或者,您可以使用 %reset out 魔术来重置输出缓存或使用 %xdel 删除特定对象的魔术:

Alternatively you can use the %reset out magic to reset the output cache or use the %xdel magic to delete a specific object:

In [1]: 1
Out[1]: 1

In [2]: 2
Out[2]: 2

In [3]: %reset out

Once deleted, variables cannot be recovered. Proceed (y/[n])? y
Flushing output cache (2 entries)

In [4]: Out[1]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-4-d74cffe9cfe3> in <module>()
----> 1 Out[1]

KeyError: 1

In [5]: 1
Out[5]: 1

In [6]: 2
Out[6]: 2

In [7]: v = Out[5]

In [8]: %xdel v    # requires a variable name, so you cannot write %xdel Out[5]

In [9]: Out[5]     # xdel removes the value of v from Out and other caches
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-9-573c4eba9654> in <module>()
----> 1 Out[5]

KeyError: 5

这篇关于防止ipython将输出存储在Out变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆