使用 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

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