ipython 是 jupyter notebook的前身並擁有ipython的全部功能
jupyter擁有 cell, markdown 整合的功能, 能同時運行代碼, 而且是多組的. 同時也可以插入markdown這種多功能注釋 包括圖片(但支持很差).
寫教程,寫博客非常一流. 而且還可以上傳到jupyterhub…據說要自己搭建
對於初學者來說, jupyter畢竟是一個web應用, 存儲文件有不穩定的地方. 建議還是使用pycharm這類的軟件編寫代碼. 如果是用於寫博客, sublime可以安裝插件copy as html jupyter對於copy的優化不夠. 特別是離線圖片保存起來是不可以顯示的.
jupyter詳細教程 http://blog.csdn.net/tina_ttl/article/details/51031113
Python·Jupyter Notebook各種使用方法記錄·持續更新
標簽(空格分隔): Python
Jupyter notebook )前身為IPython Notebook,學習時,可以找兩者的教程
- Jupyter Project Documentation
- Jupyter Notebook Documentation
- Jupyter/IPython Notebook Quick Start Guide
- Old IPython Notebook Homepage
- 目前,最新版本的Anaconda是自帶Jupyter NoteBook的,不需要再單獨安裝
- 安裝Jupyter Notebook的先決條件:已經安裝了python(python 2.7 或者是python3.3)
-
具體的安裝方法:
- 官方建議利用Anaconda安裝Jupyter
- 安裝完成Anaconda后,如果該Anaconda並不自帶Jupyter Noterbook,那么,打開cmd,輸入:conda install jupyter
-
這樣安裝完的jupyter不具有New a terminal的功能
經過各種查詢才知道,原來是因為windows不具有terminal需要的TTY,所以,windows下的jupyter是不支持Terminal模式的,而且短期內也沒有增加這種支持的計划在其配置文件ipython_notebook_config.py中,有如下一句
# The directory to use for notebooks and kernels.
# c.NotebookApp.notebook_dir = u''
-
1
-
2
-
3
-
1
-
2
-
3
該句就是用來指定其工作空間的,例如,默認的工作空間是:用戶名文件夾,例如,現在想要將工作空間變為D:\Jupyter,那么,需要做如下更改(要記得刪掉注釋#)
# The directory to use for notebooks and kernels.
c.NotebookApp.notebook_dir = u'D:\Jupyter'
這句話很容易抄錯, ''前面是u ,還要去掉#號, 而且你要在本地建立一個文件夾, 如果文件夾不存在也會出錯.
-
1
-
2
-
3
-
1
-
2
-
3
注意:路徑最后一級后面不要加符號"\"
參考目錄C:\Users\Administrator\.jupyter
- 在cmd中輸入jupyter notebook --generate-config
- 如果該配置文件已經存在,那么,會出現如下信息,從中可以見到配置文件存在的位置,注意,此時,輸入N,不要overwrite
- 如果該配置文件不存在,那么,將會初始化產生一個配置文件
在cmd中輸入:ipython profile create
可以找到關於jupyter的配置文件的位置- 執行當前cell,並自動跳到下一個cell:Shift Enter
- 執行當前cell,執行后不自動調轉到下一個cell:Ctrl-Enter
- 是當前的cell進入編輯模式:Enter
- 退出當前cell的編輯模式:Esc
- 刪除當前的cell:雙D
- 為當前的cell加入line number:單L
- 將當前的cell轉化為具有一級標題的maskdown:單1
- 將當前的cell轉化為具有二級標題的maskdown:單2
- 將當前的cell轉化為具有三級標題的maskdown:單3
- 為一行或者多行添加/取消注釋:Crtl /
- 撤銷對某個cell的刪除:z
- 瀏覽器的各個Tab之間切換:Crtl PgUp和Crtl PgDn
- 快速跳轉到首個cell:Crtl Home
- 快速跳轉到最后一個cell:Crtl End
即導入代碼到jupyter notebook的cell中
4.1 將本地的.py文件load到jupyter的一個cell中
問題背景:有一個test.py文件,需要將其載入到jupyter的一個cell中
test.py內容如下:import caffe
SolverName = "/root/workspace"
sovler = caffe.AdamSolver(SolverName)
-
1
-
2
-
3
-
1
-
2
-
3
方法步驟:
(1)在需要導入該段代碼的cell中輸入%load test.py #test.py是當前路徑下的一個python文件
-
1
-
1
(2)運行該cell
利用快捷鍵"Shift+Enter",可以看到如下結果:(3)可以看到,運行后,%load test.py被自動加入了注釋符號#,test.py中的所有代碼都被load到了當前的cell中
- 在cell中輸入%load http://.....,然后運行該cell,就會將load后面所對應地址的代碼load到當前的cell中;
-
下面給出一個例子,導入matplotlib中的一個小例子color example code
-
首先,在想要導入該段代碼的cell中輸入
%load test.py #test.py是當前路徑下的一個python文件
-
1
-
1
- 然后,Shift+Enter運行,可以看到如下結果:
可以看到,運行后,%load test.py被自動加入了注釋符號#,test.py中的所有代碼都被load到了當前的cell中
-
-
- 利用jupyter的cell是可以運行python文件的,即在cell中運行如下代碼:
%run file.py
-
1
-
1
file.py為要運行的python程序,結果會顯示在該cell中
6.1 jupyter的cell可以作為unix command使用
具體方法為:在unitx command前面加入一個感嘆號"!"
例子:
查看python版本:!python --version
運行python文件:!python myfile.py還沒有太明白,具體細節見The cell magics in IPython
6.3 獲取current working directory
即當前運行的代碼所在的路徑
具體方法:current_path = %pwd
這樣得到的current_path就是當前工作路徑的字符轉在Jupyter Notebook中,如果使用Matplotlib繪圖,有時是彈不出圖像框的,此時,可以在開頭加入
%matplotlib inline
-
1
-
1
-
要跳到的位置(the destination)
需要在要跳轉到的位置添加下面語句:<a id='the_destination'></a>
-
1
-
1
這里的id取值任意賦值,下面在添加鏈接時要用
-
需要添加鏈接的文字(an internal hyperlink to the destination),即點擊該處可以跳轉到the destination,在需要添加鏈接的文字后面加入:
[需要添加連接的文字](#the_destination)
-
1
-
1
- 下面是一個例子:
源碼:
效果圖:
- 原始的Jupyter是不支持markdown添加目錄功能的
- 實際上,可以利用Jupyter notebook extensions去使得這種功能實現
-
具體方法:
- 利用Anaconda安裝Jupyter Notebook extensions
conda install -c conda-forge jupyter_contrib_nbextensions - 打開Jupyter Notebook,在它的(新增的)Nbextensions標簽下勾選"Table of Contents(2)"
- 打開一個.jpynb文件,發現,目錄功能可用了!
參考文獻
[1] 為Jupyter Notebook添加目錄頂
14
踩
0
- 上一篇theano安裝(一)windows配置安裝theano環境(非GPU版)、keras
-
下一篇Docker學習系列(二):Ubuntu14.04下安裝Docker-2016.06.26更新版
相關文章推薦
-
猜你在找
Jupyter Notebook的27個秘訣,技巧和快捷鍵
文摘供稿
原文鏈接
翻譯:姜范波
校對:毛麗 && 寒小陽Jupyter Notebook
Jupyther notebook ,也就是一般說的 Ipython notebook,是一個可以把代碼、圖像、注釋、公式和作圖集於一處,從而實現可讀性分析的一種靈活的工具。
Jupyter延伸性很好,支持多種編程語言,可以很輕松地安裝在個人電腦或者任何服務器上——只要有ssh或者http接入就可以啦。最棒的一點是,它完全免費哦。
Jupyter 界面默認情況下,Jupyter Notebook 使用Python內核,這就是為什么它原名 IPython Notebook。Jupyter notebook是Jupyter項目的產物——Jupyter這個名字是它要服務的三種語言的縮寫:Julia,PYThon和R,這個名字與"木星(jupiter)"諧音。本文將介紹27個輕松使用Jupyter的小竅門和技巧。
1、快捷鍵
高手們都知道,快捷鍵可以節省很多時間。Jupyter在頂部菜單提供了一個快捷鍵列表:Help > Keyboard Shortcuts 。每次更新Jupyter的時候,一定要看看這個列表,因為不斷地有新的快捷鍵加進來。另外一個方法是使用Cmd + Shift + P ( Linux 和 Windows下 Ctrl + Shift + P亦可)調出命令面板。這個對話框可以讓你通過名稱來運行任何命令——當你不知道某個操作的快捷鍵,或者那個操作沒有快捷鍵的時候尤其有用。這個功能與蘋果電腦上的Spotlight搜索很像,一旦開始使用,你會欲罷不能。
幾個我的最愛:
-
Esc + F 在代碼中查找、替換,忽略輸出。
-
Esc + O 在cell和輸出結果間切換。
-
選擇多個cell:
-
Shift + J 或 Shift + Down 選擇下一個cell。
-
Shift + K 或 Shift + Up 選擇上一個cell。
-
一旦選定cell,可以批量刪除/拷貝/剪切/粘貼/運行。當你需要移動notebook的一部分時這個很有用。
-
-
Shift + M 合並cell.
2、變量的完美顯示
有一點已經眾所周知。把變量名稱或沒有定義輸出結果的語句放在cell的最后一行,無需print語句,Jupyter也會顯示變量值。當使用Pandas DataFrames時這一點尤其有用,因為輸出結果為整齊的表格。
鮮為人知的是,你可以通過修改內核選項ast_note_interactivity,使得Jupyter對獨占一行的所有變量或者語句都自動顯示,這樣你就可以馬上看到多個語句的運行結果了。 - 利用Anaconda安裝Jupyter Notebook extensions
-
- In [1]: from IPython.core.interactiveshell import InteractiveShell
- InteractiveShell.ast_node_interactivity = "all"
- In [2]: from pydataset import data
- quakes = data('quakes')
- quakes.head()
- quakes.tail()
- Out[2]:
- lat long depth mag stations
- 1 -20.42 181.62 562 4.8 41
- 2 -20.62 181.03 650 4.2 15
- 3 -26.00 184.10 42 5.4 43
- 4 -17.97 181.66 626 4.1 19
- 5 -20.42 181.96 649 4.0 11
- Out[2]:
- lat long depth mag stations
- 996 -25.93 179.54 470 4.4 22
- 997 -12.28 167.06 248 4.7 35
- 998 -20.13 184.20 244 4.5 34
- 999 -17.40 187.80 40 4.5 14
- 1000 -21.59 170.56 165 6.0 119
如果你想在各種情形下(Notebook和Console)Jupyter都同樣處理,用下面的幾行簡單的命令創建文件~/.ipython/profile_default/ipython_config.py即可實現:
- c = get_config()
- # Run all nodes interactively
- c.InteractiveShell.ast_node_interactivity = "all"
3、輕松鏈接到文檔
在Help 菜單下,你可以找到常見庫的在線文檔鏈接,包括Numpy,Pandas,Scipy和Matplotlib等。
另外,在庫、方法或變量的前面打上?,即可打開相關語法的幫助文檔。
- In [3]: ?str.replace()
Docstring:
S.replace(old, new[, count]) -> str
Return a copy of S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
Type: method_descriptor
4、 在notebook里作圖
在notebook里作圖,有多個選擇:
- matplotlib (事實標准),可通過%matplotlib inline 激活,詳細鏈接
- %matplotlib notebook 提供交互性操作,但可能會有點慢,因為響應是在服務器端完成的。
- mpld3 提供matplotlib代碼的替代性呈現(通過d3),雖然不完整,但很好。
- bokeh 生成可交互圖像的更好選擇。
- plot.ly 可以生成非常好的圖,可惜是付費服務。
5、 Jupyter Magic命令
上文提到的%matplotlib inline 是Jupyter Magic命令之一。
推薦閱讀Jupyter magic命令的相關文檔,它一定會對你很有幫助。下面是我最愛的幾個:
6、 Jupyter Magic-%env:設置環境變量
不必重啟jupyter服務器進程,也可以管理notebook的環境變量。有的庫(比如theano)使用環境變量來控制其行為,%env是最方便的途徑。
In [55]: # Running %env without any arguments
# lists all environment variables
# The line below sets the environment
# variable OMP_NUM_THREADS
%env OMP_NUM_THREADS=4
env: OMP_NUM_THREADS=4
7、Jupyter Magic - %run: 運行python代碼
%run 可以運行.py格式的python代碼——這是眾所周知的。不那么為人知曉的事實是它也可以運行其它的jupyter notebook文件,這一點很有用。
注意:使用%run 與導入一個python模塊是不同的。
In [56]: # this will execute and show the output from
# all code cells of the specified notebook
%run ./two-histograms.ipynb
8、Jupyter Magic -%load:從外部腳本中插入代碼
該操作用外部腳本替換當前cell。可以使用你的電腦中的一個文件作為來源,也可以使用URL。
In [ ]: # Before Running
%load ./hello_world.py
In [61]: # After Running
# %load ./hello_world.py
if __name__ == "__main__":
print("Hello World!")
Hello World!
9、Jupyter Magic - %store: 在notebook文件之間傳遞變量
%store 命令可以在兩個notebook文件之間傳遞變量。
In [62]: data = 'this is the string I want to pass to different notebook'
%store data
del data # This has deleted the variable
Stored 'data' (str)
現在,在一個新的notebook文檔里……
In [1]: %store -r data
print(data)
this is the string I want to pass to different notebook
10、Jupyter Magic - %who: 列出所有的全局變量
不加任何參數, %who 命令可以列出所有的全局變量。加上參數 str 將只列出字符串型的全局變量。
In [1]: one = "for the money"
two = "for the show"
three = "to get ready now go cat go"
%who str
one three two
11、Jupyter Magic – 計時
有兩種用於計時的jupyter magic命令: %%time 和 %timeit.當你有一些很耗時的代碼,想要查清楚問題出在哪時,這兩個命令非常給力。
仔細體會下我的描述哦。
%%time 會告訴你cell內代碼的單次運行時間信息。
In [4]: %%time
import time
for _ in range(1000):
time.sleep(0.01)# sleep for 0.01 seconds
CPU times: user 21.5 ms, sys: 14.8 ms, total: 36.3 ms
Wall time: 11.6 s
%%timeit 使用了Python的 timeit 模塊,該模塊運行某語句100,000次(默認值),然后提供最快的3次的平均值作為結果。
In [3]: import numpy
%timeit numpy.random.normal(size=100)
The slowest run took 7.29 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 5.5 µs per loop
12、Jupyter Magic - %%writefile and %pycat:導出cell內容/顯示外部腳本的內容
使用%%writefile magic可以保存cell的內容到外部文件。 而%pycat功能相反,把外部文件語法高亮顯示(以彈出窗方式)。
In [7]: %%writefile pythoncode.py
import numpy
def append_if_not_exists(arr, x):
if x not in arr:
arr.append(x)
def some_useless_slow_function():
arr = list()
for i in range(10000):
x = numpy.random.randint(0, 10000)
append_if_not_exists(arr, x)
Writing pythoncode.py
In [8]: %pycat pythoncode.py
import numpy
def append_if_not_exists(arr, x):
if x not in arr:
arr.append(x)
def some_useless_slow_function():
arr = list()
for i in range(10000):
x = numpy.random.randint(0, 10000)
append_if_not_exists(arr, x)
13、Jupyter Magic - %prun: 告訴你程序中每個函數消耗的時間
使用%prun+函數聲明會給你一個按順序排列的表格,顯示每個內部函數的耗時情況,每次調用函數的耗時情況,以及累計耗時。
In [47]: %prun some_useless_slow_function()
26324 function calls in 0.556 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
10000 0.527 0.000 0.528 0.000 <ipython-input-46-b52343f1a2d5>:2(append_if_not_exists)
10000 0.022 0.000 0.022 0.000 {method 'randint' of 'mtrand.RandomState' objects}
1 0.006 0.006 0.556 0.556 <ipython-input-46-b52343f1a2d5>:6(some_useless_slow_function)
6320 0.001 0.000 0.001 0.000 {method 'append' of 'list' objects}
1 0.000 0.000 0.556 0.556 <string>:1(<module>)
1 0.000 0.000 0.556 0.556 {built-in method exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
14、Jupyter Magic –用%pdb調試程序
Jupyter 有自己的調試界面The Python Debugger (pdb),使得進入函數內部檢查錯誤成為可能。
Pdb中可使用的命令見鏈接
In [ ]: %pdb
def pick_and_take():
picked = numpy.random.randint(0, 1000)
raise NotImplementedError()
pick_and_take()
Automatic pdb calling has been turned ON
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-24-0f6b26649b2e> in <module>()
5 raise NotImplementedError()
6
----> 7 pick_and_take()
<ipython-input-24-0f6b26649b2e> in pick_and_take()
3 def pick_and_take():
4 picked = numpy.random.randint(0, 1000)
----> 5 raise NotImplementedError()
6
7 pick_and_take()
NotImplementedError:
> <ipython-input-24-0f6b26649b2e>(5)pick_and_take()
3 def pick_and_take():
4 picked = numpy.random.randint(0, 1000)
----> 5 raise NotImplementedError()
6
7 pick_and_take()
ipdb>
15、末句函數不輸出
有時候不讓末句的函數輸出結果比較方便,比如在作圖的時候,此時,只需在該函數末尾加上一個分號即可。
In [4]: %matplotlib inline
from matplotlib import pyplot as plt
import numpy
x = numpy.linspace(0, 1, 1000)**1.5
In [5]: # Here you get the output of the function
plt.hist(x)
Out[5]:
(array([ 216., 126., 106., 95., 87., 81., 77., 73., 71., 68.]),
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ]),
<a list of 10 Patch objects>)
In [6]: # By adding a semicolon at the end, the output is suppressed.
plt.hist(x);
16、運行Shell命令
在notebook內部運行shell命令很簡單,這樣你就可以看到你的工作文件夾里有哪些數據集。
In [7]: !ls *.csv
nba_2016.csv titanic.csv
pixar_movies.csv whitehouse_employees.csv
17、用LaTex 寫公式
當你在一個Markdown單元格里寫LaTex時,它將用MathJax呈現公式:如
$$ P(A \mid B) = \frac{P(B \mid A) , P(A)}{P(B)} $$
會變成
18、在notebook內用不同的內核運行代碼
如果你想要,其實可以把不同內核的代碼結合到一個notebook里運行。
只需在每個單元格的起始,用Jupyter magics調用kernal的名稱:
-
%%bash
-
%%HTML
-
%%python2
-
%%python3
-
%%ruby
-
%%perl
-
In [6]: %%bash
-
for i in {1..5}
-
do
-
echo "i is $i"
-
done
i is 1
i is 2
i is 3
i is 4
i is 5
19、給Jupyter安裝其他的內核
Jupyter的優良性能之一是可以運行不同語言的內核。下面以運行R內核為例說明:
簡單的方法:通過Anaconda安裝R內核
conda install -c r r-essentials
稍微麻煩的方法:手動安裝R內核
如果你不是用Anaconda,過程會有點復雜,首先,你需要從CRAN安裝R。
之后,啟動R控制台,運行下面的語句:
install.packages(c('repr', 'IRdisplay', 'crayon', 'pbdZMQ', 'devtools'))
devtools::install_github('IRkernel/IRkernel')
IRkernel::installspec() # to register the kernel in the current R installation
20、在同一個notebook里運行R和Python
要這么做,最好的方法事安裝rpy2(需要一個可以工作的R),用pip操作很簡單:
pip install rpy2
然后,就可以同時使用兩種語言了,甚至變量也可以在二者之間公用:
In [1]: %load_ext rpy2.ipython
In [2]: %R require(ggplot2)
Out[2]: array([1], dtype=int32)
In [3]: import pandas as pd
df = pd.DataFrame({
'Letter': ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'],
'X': [4, 3, 5, 2, 1, 7, 7, 5, 9],
'Y': [0, 4, 3, 6, 7, 10, 11, 9, 13],
'Z': [1, 2, 3, 1, 2, 3, 1, 2, 3]
})
In [4]: %%R -i df
ggplot(data = df) + geom_point(aes(x = X, y= Y, color = Letter, size = Z))
21、用其他語言寫函數
有時候numpy的速度有點慢,我想寫一些更快的代碼。
原則上,你可以在動態庫里編譯函數,用python來封裝…
但是如果這個無聊的過程不用自己干,豈不更好?
你可以在cython或fortran里寫函數,然后在python代碼里直接調用。
首先,你要先安裝:
!pip install cython fortran-magic
In [ ]: %load_ext Cython
In [ ]: %%cython
def myltiply_by_2(float x):
return 2.0 * x
In [ ]: myltiply_by_2(23.)
我個人比較喜歡用Fortran,它在寫數值計算函數時十分方便。更多的細節在這里。
In [ ]: %load_ext fortranmagic
In [ ]: %%fortran
subroutine compute_fortran(x, y, z)
real, intent(in) :: x(:), y(:)
real, intent(out) :: z(size(x, 1))
z = sin(x + y)
end subroutine compute_fortran
In [ ]: compute_fortran([1, 2, 3], [4, 5, 6])
還有一些別的跳轉系統可以加速python 代碼。更多的例子見鏈接:
22、支持多指針
Jupyter支持多個指針同步編輯,類似Sublime Text編輯器。按下Alt鍵並拖拽鼠標即可實現。
23、Jupyter外接拓展
Jupyter-contrib extensions是一些給予Jupyter更多更能的延伸程序,包括jupyter spell-checker和code-formatter之類.
下面的命令安裝這些延伸程序,同時也安裝一個菜單形式的配置器,可以從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
24、從Jupyter notebook創建演示稿
Damian Avila的RISE允許你從已有的notebook創建一個powerpoint形式的演示稿。
你可以用conda來安裝RISE:
conda install -c damianavila82 rise
或者用pip安裝:
pip install RISE
然后運行下面的代碼來安裝和激活延伸程序:
jupyter-nbextension install rise --py --sys-prefix
jupyter-nbextension enable rise --py --sys-prefix
25、Jupyter輸出系統
Notebook本身以HTML的形式顯示,單元格輸出也可以是HTML形式的,所以你可以輸出任何東西:視頻/音頻/圖像。
這個例子是瀏覽我所有的圖片,並顯示前五張圖的縮略圖。
In [12]: import os
from IPython.display import display, Image
names = [f for f in os.listdir('../images/ml_demonstrations/') if f.endswith('.png')]
for name in names[:5]:
display(Image('../images/ml_demonstrations/' + name, width=100))
我們也可以用bash命令創建一個相同的列表,因為magics和bash運行函數后返回的是python 變量:
In [10]: names = !ls ../images/ml_demonstrations/*.png
names[:5]
Out[10]: ['../images/ml_demonstrations/colah_embeddings.png',
'../images/ml_demonstrations/convnetjs.png',
'../images/ml_demonstrations/decision_tree.png',
'../images/ml_demonstrations/decision_tree_in_course.png',
'../images/ml_demonstrations/dream_mnist.png']
26、大數據分析
很多方案可以解決查詢/處理大數據的問題:
-
ipyparallel(之前叫 ipython cluster) 是一個在python中進行簡單的map-reduce運算的良好選擇。我們在rep中使用它來並行訓練很多機器學習模型。
-
spark-sql magic %%sql
27、分享notebook
分享notebook最方便的方法是使用notebook文件(.ipynb),但是對那些不使用notebook的人,你還有這些選擇:
-
通過File > Download as > HTML 菜單轉換到html文件。
-
如果你把自己的notebook文件上傳到github的倉庫,可以使用很便利的Mybinder服務,允許另一個人進行半個小時的Jupyter交互連接到你的倉庫。
-
-
將你的notebook存儲在像dropbox這樣的網站上,然后把鏈接放在nbviewer,nbviewer可以呈現任意來源的notebook。
-
用菜單File > Download as > PDF 保存notebook為PDF文件。如果你選擇本方法,我強烈建議你讀一讀Julius Schulz的文章
-
https://www.zybuluo.com/hanxiaoyang/note/534296