1. 程式人生 > >jupyter notebook使用技巧

jupyter notebook使用技巧

1.簡介:

Jupyther notebook ,也就是一般說的 Ipython notebook,是一個可以把程式碼、影象、註釋、公式和作圖集於一處,從而實現可讀性分析的一種靈活的工具。 預設情況下,Jupyter Notebook 使用Python核心,這就是為什麼它原名 IPython Notebook。Jupyter notebook是Jupyter專案的產物——Jupyter這個名字是它要服務的三種語言的縮寫:Julia,Python和R,這個名字與“木星(jupiter)”諧音。

2.快捷鍵:

高手們都知道,快捷鍵可以節省很多時間。Jupyter在頂部選單提供了一個快捷鍵列表:Help > Keyboard Shortcuts 。每次更新Jupyter的時候,一定要看看這個列表,因為不斷地有新的快捷鍵加進來。另外一個方法是使用Cmd + Shift + P ( Linux 和 Windows下 Ctrl + Shift + P亦可)調出命令面板。這個對話方塊可以讓你通過名稱來執行任何命令——當你不知道某個操作的快捷鍵,或者那個操作沒有快捷鍵的時候尤其有用。這個功能與蘋果電腦上的Spotlight搜尋很像,一旦開始使用,你會欲罷不能。 我比較常用的是:

Shift + M 合併cell. 可以選中cell後用來合併。
1~6 選中cell後用來設定6個級別的heading
L  可以用來切換顯示或隱藏行號
H 用來調出快捷鍵操作指南
A 在選中的cell上方新加入一個cell
B 在選中的cell下方新加入一個cell
Y  將選中的cell轉換為程式碼編輯模式
M 將選中的cell轉換為Markdown模式
shift + enter 執行選中cell,並將游標挪到下一個cell
Ctrl + enter  執行選中cell,游標鎖定到執行的cell

全部的快捷操作如下:這裡寫圖片描述

3.全部顯示

有一點已經眾所周知。把變數名稱或沒有定義輸出結果的語句放在cell的最後一行,無需print語句,Jupyter也會顯示變數值。當使用Pandas DataFrames時這一點尤其有用,因為輸出結果為整齊的表格。

鮮為人知的是,你可以通過修改核心選項ast_node_interactivity,使得Jupyter對獨佔一行的所有變數或者語句都自動顯示,這樣你就可以馬上看到多個語句的執行結果了。

In [1]: from IPython.core.interactiveshell import InteractiveShell

        InteractiveShell.ast_node_interactivity = "all"
如果你想在各種情形下(Notebook和Console)Jupyter都同樣處理,用下面的幾行簡單的命令建立檔案~/.ipython/profile_default/ipython_config.py即可實現:

    c = get_config()

    # Run all nodes interactively

    c.InteractiveShell.ast_node_interactivity = "all"

這個剛瞭解時用起來很開心,不過當用到matplotlib時會輸出很多資訊,看起來比較醜,我就棄用了。

4.在notebook中作圖

如果不想每次用matplotlib 作圖後都要輸入plt.show()來彈出顯示圖,可以如下:

5.Jupyter notebook的magic操作

上面介紹的%matplotlib inline就是其中的一個魔術操作,作圖時用起來流暢極了; %run ====用來執行程式碼指令碼 %store ====命令可以在兩個notebook檔案之間傳遞變數,沒用過。。 %who ====不加任何引數,命令可以列出所有的全域性變數。加上引數 str 將只列出字串型的全域性變數

有兩種用於計時的jupyter magic命令: 當你有一些很耗時的程式碼,想要查清楚問題出在哪時,這兩個命令非常給力。 %%time 會告訴你cell內程式碼的單次執行時間資訊。 %%timeit 使用了Python的 timeit 模組,該模組執行某語句100,000次(預設值),然後提供最快的3次的平均值作為結果。 %prun+函式宣告會給你一個按順序排列的表格,顯示每個內部函式的耗時情況,每次呼叫函式的耗時情況,以及累計耗時。

6.末句函式不輸出

有時候不讓末句的函式輸出結果比較方便,比如在作圖的時候,此時,只需在該函式末尾加上一個分號即可===這個用起來作的圖看起來就清爽多了;

7.執行Shell命令

在notebook中可以用cd 來切換目錄; ls用來顯示當前目錄內容; !pip install或者!conda install用來使用cmd下的命令操作;

8.支援多指標

Jupyter支援多個指標同步編輯,類似Sublime Text編輯器。按下Alt鍵並拖拽滑鼠即可實現。====這個我用著很不順手,按住ctrl後用移動滑鼠可實現一樣的多次選中,我還是喜歡用這個。。

9.Jupyter外界拓展

下面的命令安裝這些延伸程式,同時也安裝一個選單形式的配置器,可以從Jupyter的主螢幕瀏覽和啟用延伸程式。 !pip install https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tarball/master !pip install jupyter_nbextensions_configurator !jupyter contrib nbextension install –user !jupyter nbextensions_configurator enable –user這裡寫圖片描述 這個用起來很爽,可以增加許多功能,尤其是裡面可以增加側邊欄,這個用起來對程式碼管理就看起來層次分明多了,找程式碼也更方便了~~

10.隱藏程式碼只顯示程式碼輸出

from IPython.display import HTML

HTML('''<script>
code_show=true; 
function code_toggle() {
 if (code_show){
 $('div.input').hide();
 } else {
 $('div.input').show();
 }
 code_show = !code_show
} 
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')

也可以這樣:

code_show=true;
function code_toggle() {
 if (code_show){
 $('div.input').hide();
 } else {
 $('div.input').show();
 }
 code_show = !code_show
}

$([IPython.events]).on("app_initialized.NotebookApp", function () {
  $("#view_menu").append("<li id=\"toggle_toolbar\" title=\"Show/Hide code cells\"><a href=\"javascript:code_toggle()\">Toggle Code Cells</a></li>")

或者這樣:

from IPython.display import display
from IPython.display import HTML
import IPython.core.display as di # Example: di.display_html('<h3>%s:</h3>' % str, raw=True)

# This line will hide code by default when the notebook is exported as HTML
di.display_html('<script>jQuery(function() {if (jQuery("body.notebook_app").length == 0) { jQuery(".input_area").toggle(); jQuery(".prompt").toggle();}});</script>', raw=True)

# This line will add a button to toggle visibility of code blocks, for use with the HTML export version
di.display_html('''<button onclick="jQuery('.input_area').toggle(); jQuery('.prompt').toggle();">Toggle code</button>''', raw=True)

這樣:

$([IPython.events]).on('notebook_loaded.Notebook', function(){
    IPython.toolbar.add_buttons_group([
        {
             'label'   : 'toggle input cells',
             'icon'    : 'icon-refresh', 
             'callback': function(){$('.input').slideToggle()}
        }
    ]);
});

甚至可以這樣:

# This is a cell to hide code snippets from displaying
# This must be at first cell!

from IPython.display import HTML

hide_me = ''
HTML('''<script>
code_show=true; 
function code_toggle() {
  if (code_show) {
    $('div.input').each(function(id) {
      el = $(this).find('.cm-variable:first');
      if (id == 0 || el.text() == 'hide_me') {
        $(this).hide();
      }
    });
    $('div.output_prompt').css('opacity', 0);
  } else {
    $('div.input').each(function(id) {
      $(this).show();
    });
    $('div.output_prompt').css('opacity', 1);
  }
  code_show = !code_show
} 
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input style="opacity:0" type="submit" value="Click here to toggle on/off the raw code."></form>''')

--------------------- 本文來自 曉東邪 的CSDN 部落格 ,全文地址請點選:https://blog.csdn.net/xiaodongxiexie/article/details/54633183?utm_source=copy