jupyter notebook 各種用法記錄(陸續更新)
Jupyter notebook )前身為IPython Notebook,學習時,可以找兩者的教程
Jupyter安裝
先決條件:已經安裝了Python(python 2.7 或者是python3.3)
安裝:
官方建議利用Anaconda安裝Jupyter,關於Anaconda的安裝見安裝配置安裝theano環境(非GPU版)
安裝Jupyter的方法:開啟cmd,輸入:conda install jupyter
注意:安裝完anaconda後,自帶了了ipython notebook,即jupyter的低版本,按照上面的方法安裝後,會發現,又多安裝了一個jupyter notebook,原來的ipython notebook還在
這樣安裝完的jupyter不具有New a terminal的功能
經過各種查詢才知道,原來是因為windows不具有terminal需要的TTY,所以,windows下的jupyter是不支援Terminal模式的,而且短期內也沒有增加這種支援的計劃
更改Jupyter notebook的工作空間
(1)Jupyter的工作空間在哪裡指定?
在其配置檔案jupyter_notebook_config.py中,有如下一句
<code class="hljs fsharp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"># The directory <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">use</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> notebooks <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> kernels. # c.NotebookApp.notebook_dir = u'' </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
該句就是用來指定其工作空間的,例如,預設的工作空間是:使用者名稱資料夾,例如,現在想要將工作空間變為D:\Jupyter,那麼,需要做如下更改(要記得刪掉註釋#)
<code class="hljs python has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># The directory to use for notebooks and kernels.</span> c.NotebookApp.notebook_dir = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">u'D:\Jupyter'</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
注意:路徑最後一級後面不要加符號“\”
-
如何找到該配置檔案?
在cmd中輸入:
jupyter notebook --generate-config
C:\Users\DELL\.jupyter\jupyter_notebook_config.py
Jupyter各種用法
程式碼的匯入
即: 匯入程式碼到jupyter notebook的cell中
將本地的.py檔案load到jupyter的一個cell中
問題背景:有一個test.py檔案,需要將其載入到jupyter的一個cell中
test.py內容如下:
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> caffe SolverName = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/root/workspace"</span> sovler = caffe.AdamSolver(SolverName)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
方法步驟:
(1)在需要匯入該段程式碼的cell中輸入
<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">%<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">load</span></span> test.py #test.py是當前路徑下的一個python檔案</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
(2)執行該cell
利用快捷鍵“Shift+Enter”,可以看到如下結果:
(3)可以看到,執行後,%load test.py
被自動加入了註釋符號#,test.py中的所有程式碼都被load到了當前的cell中
從網路load程式碼到jupyter
在cell中輸入%load http://.....
,然後執行該cell,就會將load後面所對應地址的程式碼load到當前的cell中;
下面給出一個例子,匯入matplotlib中的一個小例子color
example code
首先,在想要匯入該段程式碼的cell中輸入
<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">%<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">load</span></span> test.py #test.py是當前路徑下的一個python檔案</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
然後,Shift+Enter執行,可以看到如下結果:
可以看到,執行後,%load test.py
被自動加入了註釋符號#,test.py中的所有程式碼都被load到了當前的cell中
執行python檔案
利用jupyter的cell是可以執行python檔案的,即在cell中執行如下程式碼:
<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;">%<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">run</span></span> file.py</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
file.py為要執行的python程式,結果會顯示在該cell中
其他用法
(1)jupyter的cell可以作為unix command使用!
具體方法為:在在unitx command前面加入一個感嘆號“!”
例子:
檢視python版本:!python --version
執行python檔案:!python myfile.py
(3)獲取current working directory
(即當前執行的程式碼所在的路徑):current_path = %pwd
這樣得到的current_path就是當前工作路徑的字元轉
python常用的modules
scipy
scikit-image基於scipy的高階影象處理模組,該模組將圖片作為numpy ndarray進行處理,比較方便
The scikit-image SciKit (toolkit for SciPy) extends scipy.ndimage to provide a versatile set of image processing routines.
安裝:pip install scikit-image
使用:import skimage