使用 Jupyter Notebook 配置Stata/Python/Julia/R

早在去年在 Coursera 上學 Julia 的時候就用過 Jupyter Notebook,當時感覺沒啥特別的。然鵝最近和連老師交流的時候突然發現了Jupyter 的妙處,就像一個美人兒,看一眼覺得還行,多看幾眼就發現了韻味,所以寫篇 Jupyter 配置筆記來記錄一下。

爲什麼使用 Jupyter Notebook

官網上的解釋:

The notebook extends the console-based approach to interactive computing in a qualitatively new direction, providing a web-based application suitable for capturing the whole computation process: developing, documenting, and executing code, as well as communicating the results. The Jupyter notebook combines two components:

  • A web application: a browser-based tool for interactive authoring of documents which combine explanatory text, mathematics, computations and their rich media output.
  • Notebook documents: a representation of all content visible in the web application, including inputs and outputs of the computations, explanatory text, mathematics, images, and rich media representations of objects.

總結起來就是一個彙集開發、執行和文檔爲一體,而且支持包含 Python、Julia、Stata、R、Ruby、Matlab、SAS 等多種語言並可以實現交互式代碼的網頁版IDE。直觀來講是 IPython 在多種語言上的網頁版外推。有了這個神器,我們能規避掉的目前主流 IDE 裏存在的問題包括但不侷限於:

  • 在IDE裏測試,看不到每個部分的 output 效果。在命令行裏測試,則沒法輕鬆撤銷前面的代碼
  • 沒辦法一遍編輯一邊顯示每部分執行的效果,
  • 沒辦法一遍編輯一邊在旁邊用 Markdown 做筆記
  • 不方便和合作者協同交互

所以 Jupyter Notebook 真的是個很酷的小東西了hhh。

使用效果

Stata

在這裏插入圖片描述

Julia

在這裏插入圖片描述
不得不說,把敲代碼、運行程序和記\color{red}{好看的}筆記集於一身的 Jupyter 真是太優秀啦!最重要的是導出格式也豐富多樣(Latex和Markdown都有,看了都激動):
在這裏插入圖片描述

如何安裝 Jupyter Notebook

有兩種方案,但不管哪種方案,都需要電腦已經安裝好了 Python(推薦3.X版本)。

第一種方案

  • Anaconda 下載地址 上下載並安裝 Anaconda。
  • 在 Mac 的 終端 / Windows 裏的 cmd 裏輸入:jupyter notebook 即可使用:
    jupyter 頁面

第二種方案

在 Mac 的 終端 / Windows 裏的 cmd 裏輸入:

python3 -m pip install --upgrade pip
python3 -m pip install jupyter

然後輸入:

jupyter notebook

看能不能打開 jupyter 頁面。

兩種方案的選擇

如果只是想測試下 Jupyter 環境,並且日常主要使用的不是 Python 語言,就沒必要安裝龐大的 Anaconda 大禮包啦,研究數據科學、機器學習和自然語言處理的同學牆裂推薦安裝。因爲 Anaconda 包括:

  • 1500+ Python/R 的數據科學包
  • scikit-learn、TensorFlow、Theano 等一系列機器學習和深度學習包
  • Dask、NumPy、pandas、Numba 等數據分析包
  • Matplotlib、Bokeh、Datashader、Holoviews 等數據可視化包

有些包裝起來還挺麻煩的, Anaconda 一鍵安裝省了很多事。除了這些豐富的包外,豐富的集成環境 Anaconda Navigator 和功能強大的 conda 命令也是一大亮點:
na

然鵝如果對數據科學並不感興趣,直接用第二種方法即可,更加輕便快捷。

在 Jupyter 裏添加 Kernal

如果我們想在 Jupyter 上面運行 Python3.X / Stata /Julia/R 的程序,就需要手動加 Kernal。

Stata Kernal

在 Mac 的 終端 / Windows 裏的 cmd 裏輸入:

pip3 install stata_kernel
python -m stata_kernel.install

Python3.X Kernal

pip3 install ipykernel
python -m ipykernel install

Julia Kernal

可以直接使用內嵌 Jupyter 的 JuliaBox(需要登錄):
juliabox也可以使用 IJulia 配置:
Step1: 在 Mac 的 終端 / Windows 裏的 cmd 裏輸入 julia 進入 julia :
julia
Step2: 英文字符下輸入 ] 進入 pkg 模式:
pkg
Step3: 輸入命令 add IJulia 安裝(或升級) IJulia:
up
Step4: 在 Mac 的 終端 / Windows 裏的 cmd 裏輸入 jupyter notebook 啓動 jupyter

R Kernal

R kernal 我是用 Anaconda Navigator 裝的:
Step1: 打開 Anaconda Navigator
Step2: 選擇 Environment 中的 create 創建一個新環境

Step3: 將環境命名爲 “r-tutorial”,在下拉菜單中選擇 Python3.7 和 R

Step4: 環境創建完成後,右鍵選擇 Open the environment with the R package,就完成啦

到此爲止我們已經在 Jupyter 裏安裝了 Stata/Python/Julia/R 的Kernal,啓動後我們可以看到如下的效果:
效果

主要參考鏈接

1.https://nbviewer.jupyter.org/github/kylebarron/stata_kernel/blob/master/examples/Example.ipynb#
2.https://docs.anaconda.com/anaconda/navigator/tutorials/r-lang/
3.https://zhuanlan.zhihu.com/p/42812662
4.https://mlln.cn/2018/11/01/安裝stata並在jupyter-notebook中調用/
5.https://www.juliabox.com/
6.https://jupyter.org/install
7.https://www.anaconda.com/distribution/
8.https://www.dataquest.io/blog/jupyter-notebook-tutorial/
9.https://segmentfault.com/a/1190000015143460
10.https://www.jianshu.com/p/91365f343585

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章