珠联璧合 II:Jupyter Notebook 与 Matlab 之融合

发布时间:2020-03-26 阅读 60

作者:韩少真(西北大学) || 展金永(对外经济贸易大学)  
连享会 - lianxh.cn

Stata连享会   主页 || 视频 || 推文

连享会 - Stata 暑期班

线上直播 9 天:2020.7.28-8.7
主讲嘉宾:连玉君 (中山大学) | 江艇 (中国人民大学)
课程主页https://gitee.com/arlionn/PX | 微信版

相关推文:


目录


虽然 Stata 已经有了专门的空间计量模块,但由于多数做空间计量的经济学家都在使用 Matlab,使得很多前沿的方法和模型目前都还是只有 Matlab 程序。因此,我们

言归正传,就学术研究而言,目前在很多 Top 期刊上发表的论文都会综合使用多种计量方法和软件来完成文中的实证分析工作。这意味着,我们需要一个工作平台,以便有效整合手头的 2-3 中软件和语言,不必频繁地地转场。

今天要介绍的 Anaconda 就是这么个玩意儿,其配置的 Jupyter Notebook 便是我们所需的分析和写作平台。你可以在一份文档中同时使用 Stata,Python,R 多种语言来执行实证分析,同时使用 Markdown 等写作,并将完成后的文档输出为 Word,PDF,HTML 甚至是幻灯片等多种格式。

1. 准备工作-关联前提

首先,请按助教提供教程安装 Anaconda 和 MATLAB,并检验 Anaconda 和 MATLAB 是否已经成功安装在本机电脑上。

2. 安装 MATLAB 的 Python 拓展

2.1 获取扩展的路径

打开 MATLAB 的安装路径(如果忘记可以看下 MATLAB 安装教程的第六步),依次打开extern、engines、Python 文件夹,进入 Python 文件夹。然后点击地址栏,复制 Python 文件夹的路径地址,如下图所示: 在这里插入图片描述

我的电脑为D:\Polyspace\R2019a\extern\engines\ Python,根据 MATLAB 安装的地址不同,D:\Polyspace\R2019a\可能有所差异,请根据个人安装状况更改。

2.2 安装 MATLAB 的 Python 扩展

  • 通过开始- Anaconda 3(64-bit)- Anaconda Powershell prompt,打开 Anaconda Powershell prompt。如果直接打开执行下面的操作无法正常关联 Python 和 MATLAB,则需要右击选择以管理员身份运行,重新进行接下来的操作。 在这里插入图片描述
  • 在powershell中依次输入以下命令并按回车键执行
  • ounter(line
  • ounter(line
cd "D:\Polyspace\R2019a\extern\engines\ Python " Python  setup.py install
  • 执行结果如下所示,表明扩展安装成功
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
(base) PS C:\Users\mailshaozhen> cd D:\Polyspace\R2019a\extern\engines\ Python (base) PS D:\Polyspace\R2019a\extern\engines\ Python >  Python  setup.py installrunning installrunning buildrunning build_pycreating buildcreating build\libcreating build\lib\ MATLAB copying dist\ MATLAB \mlarray.py -> build\lib\ MATLAB copying dist\ MATLAB \mlexceptions.py -> build\lib\ MATLAB copying dist\ MATLAB \__init__.py -> build\lib\ MATLAB creating build\lib\ MATLAB \enginecopying dist\ MATLAB \engine\basefuture.py -> build\lib\ MATLAB \enginecopying dist\ MATLAB \engine\engineerror.py -> build\lib\ MATLAB \enginecopying dist\ MATLAB \engine\enginehelper.py -> build\lib\ MATLAB \enginecopying dist\ MATLAB \engine\enginesession.py -> build\lib\ MATLAB \enginecopying dist\ MATLAB \engine\fevalfuture.py -> build\lib\ MATLAB \enginecopying dist\ MATLAB \engine\futureresult.py -> build\lib\ MATLAB \enginecopying dist\ MATLAB \engine\ MATLAB engine.py -> build\lib\ MATLAB \enginecopying dist\ MATLAB \engine\ MATLAB future.py -> build\lib\ MATLAB \enginecopying dist\ MATLAB \engine\__init__.py -> build\lib\ MATLAB \enginecreating build\lib\ MATLAB \_internalcopying dist\ MATLAB \_internal\mlarray_sequence.py -> build\lib\ MATLAB \_internalcopying dist\ MATLAB \_internal\mlarray_utils.py -> build\lib\ MATLAB \_internalcopying dist\ MATLAB \_internal\__init__.py -> build\lib\ MATLAB \_internalrunning install_libcreating D:\ Anaconda 3\Lib\site-packages\ MATLAB creating D:\ Anaconda 3\Lib\site-packages\ MATLAB \enginecopying build\lib\ MATLAB \engine\basefuture.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \enginecopying build\lib\ MATLAB \engine\engineerror.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \enginecopying build\lib\ MATLAB \engine\enginehelper.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \enginecopying build\lib\ MATLAB \engine\enginesession.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \enginecopying build\lib\ MATLAB \engine\fevalfuture.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \enginecopying build\lib\ MATLAB \engine\futureresult.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \enginecopying build\lib\ MATLAB \engine\ MATLAB engine.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \enginecopying build\lib\ MATLAB \engine\ MATLAB future.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \enginecopying build\lib\ MATLAB \engine\_arch.txt -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \enginecopying build\lib\ MATLAB \engine\__init__.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \enginecopying build\lib\ MATLAB \mlarray.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB copying build\lib\ MATLAB \mlexceptions.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB creating D:\ Anaconda 3\Lib\site-packages\ MATLAB \_internalcopying build\lib\ MATLAB \_internal\mlarray_sequence.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \_internalcopying build\lib\ MATLAB \_internal\mlarray_utils.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \_internalcopying build\lib\ MATLAB \_internal\__init__.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \_internalcopying build\lib\ MATLAB \__init__.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB byte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine\basefuture.py to basefuture.c Python -37.pycbyte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine\engineerror.py to engineerror.c Python -37.pycbyte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine\enginehelper.py to enginehelper.c Python -37.pycbyte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine\enginesession.py to enginesession.c Python -37.pycbyte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine\fevalfuture.py to fevalfuture.c Python -37.pycbyte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine\futureresult.py to futureresult.c Python -37.pycbyte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine\ MATLAB engine.py to  MATLAB engine.c Python -37.pycbyte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine\ MATLAB future.py to  MATLAB future.c Python -37.pycbyte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine\__init__.py to __init__.c Python -37.pycbyte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \mlarray.py to mlarray.c Python -37.pycbyte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \mlexceptions.py to mlexceptions.c Python -37.pycbyte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \_internal\mlarray_sequence.py to mlarray_sequence.c Python -37.pycbyte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \_internal\mlarray_utils.py to mlarray_utils.c Python -37.pycbyte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \_internal\__init__.py to __init__.c Python -37.pycbyte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \__init__.py to __init__.c Python -37.pycrunning install_egg_infoWriting D:\ Anaconda 3\Lib\site-packages\ MATLAB enginefor Python -R2018a-py3.7.egg-info(base) PS D:\Polyspace\R2019a\extern\engines\ Python >

3.在 Python 中安装 MATLAB _kernel 包

  • 根据下图,打开 Anaconda prompt 在这里插入图片描述
  • Anaconda prompt 中运行以下代码
  • ounter(line
pip install  MATLAB _kernel
  • Anaconda 会自动安装 MATLAB _kernel 包,具体如下所示,则安装成功
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
  • ounter(line
(base) C:\Users\mailshaozhen>pip install  MATLAB _kernelCollecting  MATLAB _kernel  Downloading https://files. Python hosted.org/packages/64/ad/5f471160ec33e2f0f3586d285cd5e3b6dff51027849f28369d43d1d57fd1/ MATLAB _kernel-0.16.7-py3-none-any.whlRequirement already satisfied: i Python >=4.0.0 in d:\ Anaconda 3\lib\site-packages (from  MATLAB _kernel) (7.4.0)Collecting metakernel>=0.23.0 (from  MATLAB _kernel)  Downloading https://files. Python hosted.org/packages/bb/bd/658103e652c2c12a791c83539496541e8f8d755c1cc3293861983f0a6742/metakernel-0.24.2-py2.py3-none-any.whl (208kB)    100% |████████████████████████████████| 215kB 13kB/sRequirement already satisfied: jupyter-client>=4.4.0 in d:\ Anaconda 3\lib\site-packages (from  MATLAB _kernel) (5.2.4)Requirement already satisfied: jedi>=0.10 in d:\ Anaconda 3\lib\site-packages (from i Python >=4.0.0-> MATLAB _kernel) (0.13.3)Requirement already satisfied: backcall in d:\ Anaconda 3\lib\site-packages (from i Python >=4.0.0-> MATLAB _kernel) (0.1.0)Requirement already satisfied: prompt-toolkit<2.1.0,>=2.0.0 in d:\ Anaconda 3\lib\site-packages (from i Python >=4.0.0-> MATLAB _kernel) (2.0.9)Requirement already satisfied: traitlets>=4.2 in d:\ Anaconda 3\lib\site-packages (from i Python >=4.0.0-> MATLAB _kernel) (4.3.2)Requirement already satisfied: colorama; sys_platform == "win32" in d:\ Anaconda 3\lib\site-packages (from i Python >=4.0.0-> MATLAB _kernel) (0.4.1)Requirement already satisfied: setuptools>=18.5 in d:\ Anaconda 3\lib\site-packages (from i Python >=4.0.0-> MATLAB _kernel) (40.8.0)Requirement already satisfied: decorator in d:\ Anaconda 3\lib\site-packages (from i Python >=4.0.0-> MATLAB _kernel) (4.4.0)Requirement already satisfied: pygments in d:\ Anaconda 3\lib\site-packages (from i Python >=4.0.0-> MATLAB _kernel) (2.3.1)Requirement already satisfied: pickleshare in d:\ Anaconda 3\lib\site-packages (from i Python >=4.0.0-> MATLAB _kernel) (0.7.5)Collecting ipyparallel (from metakernel>=0.23.0-> MATLAB _kernel)  Downloading https://files. Python hosted.org/packages/3f/82/aaa7a357845a98d4028f27c799f0d3bb2fe55fc1247c73dc712b4ae2344c/ipyparallel-6.2.4-py2.py3-none-any.whl (198kB)    100% |████████████████████████████████| 204kB 7.5kB/sRequirement already satisfied: ipykernel in d:\ Anaconda 3\lib\site-packages (from metakernel>=0.23.0-> MATLAB _kernel) (5.1.0)Collecting pexpect>=4.2 (from metakernel>=0.23.0-> MATLAB _kernel)  Downloading https://files. Python hosted.org/packages/0e/3e/377007e3f36ec42f1b84ec322ee12141a9e10d808312e5738f52f80a232c/pexpect-4.7.0-py2.py3-none-any.whl (58kB)    100% |████████████████████████████████| 61kB 10kB/sRequirement already satisfied: tornado>=4.1 in d:\ Anaconda 3\lib\site-packages (from jupyter-client>=4.4.0-> MATLAB _kernel) (6.0.2)Requirement already satisfied: jupyter-core in d:\ Anaconda 3\lib\site-packages (from jupyter-client>=4.4.0-> MATLAB _kernel) (4.4.0)Requirement already satisfied: pyzmq>=13 in d:\ Anaconda 3\lib\site-packages (from jupyter-client>=4.4.0-> MATLAB _kernel) (18.0.0)Requirement already satisfied:  Python -dateutil>=2.1 in d:\ Anaconda 3\lib\site-packages (from jupyter-client>=4.4.0-> MATLAB _kernel) (2.8.0)Requirement already satisfied: parso>=0.3.0 in d:\ Anaconda 3\lib\site-packages (from jedi>=0.10->i Python >=4.0.0-> MATLAB _kernel) (0.3.4)Requirement already satisfied: wcwidth in d:\ Anaconda 3\lib\site-packages (from prompt-toolkit<2.1.0,>=2.0.0->i Python >=4.0.0-> MATLAB _kernel) (0.1.7)Requirement already satisfied: six>=1.9.0 in d:\ Anaconda 3\lib\site-packages (from prompt-toolkit<2.1.0,>=2.0.0->i Python >=4.0.0-> MATLAB _kernel) (1.12.0)Requirement already satisfied: i Python -genutils in d:\ Anaconda 3\lib\site-packages (from traitlets>=4.2->i Python >=4.0.0-> MATLAB _kernel) (0.2.0)Collecting ptyprocess>=0.5 (from pexpect>=4.2->metakernel>=0.23.0-> MATLAB _kernel)  Downloading https://files. Python hosted.org/packages/d1/29/605c2cc68a9992d18dada28206eeada56ea4bd07a239669da41674648b6f/ptyprocess-0.6.0-py2.py3-none-any.whlInstalling collected packages: ipyparallel, ptyprocess, pexpect, metakernel,  MATLAB -kernelSuccessfully installed ipyparallel-6.2.4  MATLAB -kernel-0.16.7 metakernel-0.24.2 pexpect-4.7.0 ptyprocess-0.6.0
  • 然后在 Anaconda prompt 中输入 Jupyter Notebook,回车执行就会直接打开 Jupyter Notebook 。打开 Jupyter Notebook 的方法有很多,可以在开始程序中找到 Jupyter Notebook 点击打开,也可以到 Anaconda 的主界面打开 Jupyter Notebook 。根据下图步骤新建 MATLAB 的文档。 在这里插入图片描述

  • 在打开的notebook中输入 MATLAB 语句,例如a=3,表示生成新变量a,并给其赋值为3。点击运行,出现运行结果,表明关联成功。具体如下图所示:

  • **注意:**执行第一条指令的时候要耐心等一等, MATLAB r2019a程序比较大,启动比较慢,执行完第一行执行,接下来执行就会快很多,可以继续执行b=a,测试发现基本不用等待就会出结果。 在这里插入图片描述

   

连享会 - 文本分析与爬虫 - 专题视频

主讲嘉宾:司继春 || 游万海

连享会-文本分析与爬虫-专题视频教程

相关课程

连享会-直播课 上线了!
http://lianxh.duanshu.com

免费公开课:


课程一览

支持回看,所有课程可以随时购买观看。

专题 嘉宾 直播/回看视频
Stata暑期班 连玉君
江艇
线上直播 9 天
2020.7.28-8.7
效率分析-专题 连玉君
鲁晓东
张 宁
视频-TFP-SFA-DEA
已上线,3天
文本分析/爬虫 游万海
司继春
视频-文本分析与爬虫
已上线,4天
空间计量系列 范巧 空间全局模型, 空间权重矩阵
空间动态面板, 空间DID
研究设计 连玉君 我的特斯拉-实证研究设计-幻灯片-
面板模型 连玉君 动态面板模型-幻灯片-
直击面板数据模型 [免费公开课,2小时]

Note: 部分课程的资料,PPT 等可以前往 连享会-直播课 主页查看,下载。


关于我们

  • Stata连享会 由中山大学连玉君老师团队创办,定期分享实证分析经验。直播间 有很多视频课程,可以随时观看。
  • 连享会-主页知乎专栏,300+ 推文,实证分析不再抓狂。
  • 公众号推文分类: 计量专题 | 分类推文 | 资源工具。推文分成 内生性 | 空间计量 | 时序面板 | 结果输出 | 交乘调节 五类,主流方法介绍一目了然:DID, RDD, IV, GMM, FE, Probit 等。
  • 公众号关键词搜索/回复 功能已经上线。大家可以在公众号左下角点击键盘图标,输入简要关键词,以便快速呈现历史推文,获取工具软件和数据下载。常见关键词:
    • 课程, 直播, 视频, 客服, 模型设定, 研究设计, 暑期班
    • stata, plus,Profile, 手册, SJ, 外部命令, profile, mata, 绘图, 编程, 数据, 可视化
    • DID,RDD, PSM,IV,DID, DDD, 合成控制法,内生性, 事件研究, 交乘, 平方项, 缺失值, 离群值, 缩尾, R2, 乱码, 结果
    • Probit, Logit, tobit, MLE, GMM, DEA, Bootstrap, bs, MC, TFP, 面板, 直击面板数据, 动态面板, VAR, 生存分析, 分位数
    • 空间, 空间计量, 连老师, 直播, 爬虫, 文本, 正则, python
    • Markdown, Markdown幻灯片, marp, 工具, 软件, Sai2, gInk, Annotator, 手写批注, 盈余管理, 特斯拉, 甲壳虫, 论文重现, 易懂教程, 码云, 教程, 知乎

连享会主页  lianxh.cn
连享会主页 lianxh.cn

连享会小程序:扫一扫,看推文,看视频……


扫码加入连享会微信群,提问交流更方便

✏ 连享会学习群-常见问题解答汇总:
https://gitee.com/arlionn/WD