
回顧:菜鳥數(shù)據(jù)科學(xué)入門01 - 工具包概略
為什么要用 IPython?
IPython (Interactive Python 的簡寫) 是一個強大的交互式 Python Shell,由 Fernando Perez 在 2001 發(fā)起。它的目標是提供 Tools for the entire life cycle of research computing. 如果說 Python 是數(shù)據(jù)科學(xué)操作的引擎,那么 IPython 就是交互式的控制面板。
IPython 的優(yōu)點:
- 交互式和批處理功能
- 提高編寫、測試速度,執(zhí)行結(jié)果立即可見,方便調(diào)試
- 同時保存腳本和計算過程,可重復(fù)可互動
- 豐富的數(shù)據(jù)可視化工具
- 能在本地計算機上對遠程服務(wù)器中的數(shù)據(jù)進行分析
- 兼容 markdown 語法,滿足數(shù)據(jù)分析、課程教學(xué)、博客寫作等需求
Python Scientific Ecosystem

IPython 只是為 NumPy、Scipy、Pandas、Matplotlib 等包提供一個交互式接口,本身并不提供科學(xué)計算的功能。這些工具組合在一起,形成了可以匹敵如 Matlab、Mathmatic 等復(fù)雜工具的科學(xué)計算框架。
Shell or Notebook?
使用 IPython 有兩種方式(前提是已經(jīng)安裝好 IPython)。
在命令行中使用
在命令行中輸入 ipython,進入 IPython 環(huán)境:
Python 2.7.10 (default, Jul 30 2016, 19:40:32)
Type "copyright", "credits" or "license" for more information.
IPython 5.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]:
在 Jupyter Notebook 中使用
Jupyter Notebook 是基于瀏覽器的 IPython shell 圖形界面,非常適合用于開發(fā)、協(xié)同、分享甚至是發(fā)布數(shù)據(jù)科學(xué)研究成果。Notebook 以 JSON 格式保存整個交互式會話,可以兼容 Python 代碼,文本標記語言如 Markdown,圖片,視頻,媒體內(nèi)容等。 IPython Notebook 在 Python 社區(qū)中越來越普遍,特別是在科學(xué)研究與教育領(lǐng)域,很多課程/博客/書籍都是用 Notebook 寫的。
安裝 Jupyter 后,在命令行輸入 jupyter notebook,進入 IPython 環(huán)境
[I 15:28:38.305 NotebookApp] Serving notebooks from local directory: /Users/kidult/Workspace/PresentWrok/OpenMind/2016.OM.DS/my_venv
[I 15:28:38.305 NotebookApp] 0 active kernels
[I 15:28:38.305 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/
[I 15:28:38.305 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
瀏覽器會打開本地的 Jupyter 控制臺,新建或選擇 .ipynb 后綴文件打開,就可以看到 Notebook 的頁面了:

Notebook 基本結(jié)構(gòu)和操作
在 Jupyter Notebook 中,基本的操作單元是 cell:

每個 cell 由 In和Out,即輸入和輸出部分組成。在這里,IPython 實際上生成了名叫 In和Out 的 Python 變量,把操作歷史存儲起來,以便隨時調(diào)用。(_可調(diào)用上一個輸出,_X可調(diào)用 Out[X] 的輸出)
IPython 中的 cell 支持 Markdown 格式,所以很適合用來生成帶源碼和運行結(jié)果的文檔。

Notebook 中常用操作
命令模式 (按 Esc 鍵進入)
| 鍵盤 | 命令 | 操作 |
|---|---|---|
| ? | 回車 | 進入編輯模式 |
| ?? | shift+回車 | 運行 cell,選擇下一個 cell |
| M | M 鍵 | 切換為 Markdown 格式 |
| A | A 鍵 | 在上方插入 cell |
| B | B 鍵 | 在下方插入 cell |
| D,D | 按兩次 D 鍵 | 刪除所選 cell |
| H | H 鍵 | 顯示快捷鍵 |
編輯模式 (按 Enter 鍵進入)
| 鍵盤 | 命令 | 操作 |
|---|---|---|
| ?] | command + ] | 縮進 |
| ?[ | command + ] | 取消縮進 |
| ?Z | command + Z | 撤銷 |
| ??Z | command + shift + Z | 重做 |
| ?↑ | command + up 鍵 | 跳到 cell 開頭 |
| ?↓ | command + down 鍵 | 跳到 cell 結(jié)尾 |
實用功能
Tab自動補全
在輸入命令時按下 tab 鍵,可以查看補全選項:

用 ? 查詢文檔
比如,要查詢 len 的用法,只需要輸入 len?
In [2]: len?
Type: builtin_function_or_method
String form: <built-in function len>
Namespace: Python builtin
Docstring:
len(object) -> integer
Return the number of items of a sequence or mapping.
另外,用 ?? 可以查看源代碼,比如輸入square??
In [8]: square??
Type: function
String form: <function square at 0x103713cb0>
Definition: square(a)
Source:
def square(a):
"Return the square of a"
return a ** 2
魔法命令
通過%lsmagic即可查看,以下為部分命令:
| 命令 | 命令說明 |
|---|---|
| %hist | 查詢輸入的歷史 |
| %reset | 清空 namespace |
| %time | 顯示 Python語句的執(zhí)行時間,包括 cpu time 和 wall clock time |
| %timeit | 顯示 Python語句的執(zhí)行時間,但是這個命令會多次執(zhí)行相應(yīng)的語句(可以指定次數(shù))%timeit
|
| %bookmark | 用于存儲常用路徑 |
| %cd | 進入目錄命令 |
| %env | 顯示系統(tǒng)環(huán)境變量 |
| %pushd dir | 將當(dāng)前目錄入棧,并進入dir指定的目錄 |
參考資料
- Python Data Science Handbook
- IPython.org
- 學(xué)習(xí)IPython進行交互式編程和數(shù)據(jù)可視化
- 使用IPython有哪些好處? - 知乎
- 為什么要使用IPython? - 簡書
Art & Code 的熱門文章
