我目前正在使用pandas和ipython。由於熊貓數據幀在您執行操作時被複制,因此每個單元的內存使用量增加500 MB。我相信這是因爲數據存儲在Out
變量中,因爲這在默認的python解釋器中不會發生。防止ipython將輸出存儲在Out變量中
如何禁用Out
變量?
我目前正在使用pandas和ipython。由於熊貓數據幀在您執行操作時被複制,因此每個單元的內存使用量增加500 MB。我相信這是因爲數據存儲在Out
變量中,因爲這在默認的python解釋器中不會發生。防止ipython將輸出存儲在Out變量中
如何禁用Out
變量?
您擁有的第一個選項是避免產生輸出。如果您不需要,請參閱中間結果只是避免它們,並將所有計算放在一個單元中。
如果您需要實際顯示該數據,則可以使用InteractiveShell.cache_size
選項爲高速緩存設置最大大小。將此值設置爲0
將禁用緩存。
要做到這一點你有你的~/.ipython/profile_default
目錄下創建一個名爲ipython_config.py
(或ipython_notebook_config.py
)文件與內容:
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來加載該配置文件。
或者您可以使用%reset out
魔法重置輸出緩存或者使用%xdel
魔法刪除特定對象:
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
你說你在使用cache_size設置時遇到問題 - 你在哪裏設置它?這是一個IPython配置選項,而不是Jupyter配置選項,所以它仍然在'〜/ .ipython/profile_default /'下。 –
@ThomasK我嘗試使用'%config'魔法並從命令行傳遞選項'--InteractiveShell.cache_size = 0'。我知道你可以創建配置文件,但我相信至少有一種方法可以做到這一點,而無需創建配置文件或更改默認配置文件。無論如何,如果這是我將在答案中編輯它的唯一方法。 – Bakuriu
您可以使用'%config'魔術或命令行選項暫時設置它。永久設置它需要在配置文件中這樣做。 –
你能提供你的代碼的例子嗎?我認爲操作可能是'inplace = True'或者不是...... – IanS
@IanS我使用'+,/'等操作符。即使我不是,我寧願不使用'infile' 。 – parchment