ipython 是 jupyter notebook的前身並擁有ipython的所有功能 javascript
jupyter擁有 cell, markdown 整合的功能, 能同時運行代碼, 並且是多組的. 同時也能夠插入markdown這種多功能註釋 包括圖片(但支持不好). html
寫教程,寫博客很是一流. 並且還能夠上傳到jupyterhub…聽說要本身搭建 java
對於初學者來講, jupyter畢竟是一個web應用, 存儲文件有不穩定的地方. 建議仍是使用pycharm這類的軟件編寫代碼. 若是是用於寫博客, sublime能夠安裝插件copy as html jupyter對於copy的優化不夠. 特別是離線圖片保存起來是不能夠顯示的. node
jupyter詳細教程 http://blog.csdn.net/tina_ttl/article/details/51031113 python
Python·Jupyter Notebook各類使用方法記錄·持續更新 git
標籤(空格分隔): Python github
Jupyter notebook )前身爲IPython Notebook,學習時,能夠找二者的教程 web
1.1 新版本Anaconda自帶Jupyter shell
具體的安裝方法:
在其配置文件ipython_notebook_config.py中,有以下一句
# The directory to use for notebooks and kernels.
# c.NotebookApp.notebook_dir = u''
該句就是用來指定其工做空間的,例如,默認的工做空間是:用戶名文件夾,例如,如今想要將工做空間變爲D:\Jupyter,那麼,須要作以下更改(要記得刪掉註釋#)
# The directory to use for notebooks and kernels.
c.NotebookApp.notebook_dir = u'D:\Jupyter'
這句話很容易抄錯, ''前面是u ,還要去掉#號, 並且你要在本地創建一個文件夾, 若是文件夾不存在也會出錯.
注意:路徑最後一級後面不要加符號"\"
參考目錄C:\Users\Administrator\.jupyter
在cmd中輸入:ipython profile create
能夠找到關於jupyter的配置文件的位置
即導入代碼到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)在須要導入該段代碼的cell中輸入
%load test.py #test.py是當前路徑下的一個python文件
(2)運行該cell
利用快捷鍵"Shift+Enter",能夠看到以下結果:
(3)能夠看到,運行後,%load test.py被自動加入了註釋符號#,test.py中的全部代碼都被load到了當前的cell中
首先,在想要導入該段代碼的cell中輸入
%load test.py #test.py是當前路徑下的一個python文件
1
1
%run file.py
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
<a id='the_destination'></a>
這裏的id取值任意賦值,下面在添加連接時要用
[須要添加鏈接的文字](#the_destination)
具體方法:
參考文獻
[1] 爲Jupyter Notebook添加目錄
頂
14
踩
0
下一篇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搜索很像,一旦開始使用,你會欲罷不能。
幾個個人最愛:
2、變量的完美顯示
有一點已經衆所周知。把變量名稱或沒有定義輸出結果的語句放在cell的最後一行,無需print語句,Jupyter也會顯示變量值。當使用Pandas DataFrames時這一點尤爲有用,由於輸出結果爲整齊的表格。
不爲人知的是,你能夠經過修改內核選項ast_note_interactivity,使得Jupyter對獨佔一行的全部變量或者語句都自動顯示,這樣你就能夠立刻看到多個語句的運行結果了。
若是你想在各類情形下(Notebook和Console)Jupyter都一樣處理,用下面的幾行簡單的命令建立文件~/.ipython/profile_default/ipython_config.py便可實現:
3、輕鬆連接到文檔
在Help 菜單下,你能夠找到常見庫的在線文檔連接,包括Numpy,Pandas,Scipy和Matplotlib等。
另外,在庫、方法或變量的前面打上?,便可打開相關語法的幫助文檔。
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的名稱:
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中使用它來並行訓練不少機器學習模型。
27、分享notebook
分享notebook最方便的方法是使用notebook文件(.ipynb),可是對那些不使用notebook的人,你還有這些選擇:
將你的notebook存儲在像dropbox這樣的網站上,而後把連接放在nbviewer,nbviewer能夠呈現任意來源的notebook。
用菜單File > Download as > PDF 保存notebook爲PDF文件。若是你選擇本方法,我強烈建議你讀一讀Julius Schulz的文章
https://www.zybuluo.com/hanxiaoyang/note/534296