美文网首页
珠联璧合 II:Jupyter Notebook 与 Matla

珠联璧合 II:Jupyter Notebook 与 Matla

作者: stata连享会 | 来源:发表于2019-09-15 09:56 被阅读0次

    作者:韩少真(西北大学) || 展金永(对外经济贸易大学)

    点击查看完整推文列表

    Stata连享会   计量专题 || 精品课程 || 简书推文 || 公众号合集

    2020寒假Stata现场班 (北京, 1月8-17日,连玉君-江艇主讲),「+助教招聘」

    2020寒假Stata现场班

    虽然 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中依次输入以下命令并按回车键执行
    cd "D:\Polyspace\R2019a\extern\engines\ Python "
     Python  setup.py install
    
    • 执行结果如下所示,表明扩展安装成功
    (base) PS C:\Users\mailshaozhen> cd D:\Polyspace\R2019a\extern\engines\ Python 
    (base) PS D:\Polyspace\R2019a\extern\engines\ Python >  Python  setup.py install
    running install
    running build
    running build_py
    creating build
    creating build\lib
    creating 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 \engine
    copying dist\ MATLAB \engine\basefuture.py -> build\lib\ MATLAB \engine
    copying dist\ MATLAB \engine\engineerror.py -> build\lib\ MATLAB \engine
    copying dist\ MATLAB \engine\enginehelper.py -> build\lib\ MATLAB \engine
    copying dist\ MATLAB \engine\enginesession.py -> build\lib\ MATLAB \engine
    copying dist\ MATLAB \engine\fevalfuture.py -> build\lib\ MATLAB \engine
    copying dist\ MATLAB \engine\futureresult.py -> build\lib\ MATLAB \engine
    copying dist\ MATLAB \engine\ MATLAB engine.py -> build\lib\ MATLAB \engine
    copying dist\ MATLAB \engine\ MATLAB future.py -> build\lib\ MATLAB \engine
    copying dist\ MATLAB \engine\__init__.py -> build\lib\ MATLAB \engine
    creating build\lib\ MATLAB \_internal
    copying dist\ MATLAB \_internal\mlarray_sequence.py -> build\lib\ MATLAB \_internal
    copying dist\ MATLAB \_internal\mlarray_utils.py -> build\lib\ MATLAB \_internal
    copying dist\ MATLAB \_internal\__init__.py -> build\lib\ MATLAB \_internal
    running install_lib
    creating D:\ Anaconda 3\Lib\site-packages\ MATLAB 
    creating D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine
    copying build\lib\ MATLAB \engine\basefuture.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine
    copying build\lib\ MATLAB \engine\engineerror.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine
    copying build\lib\ MATLAB \engine\enginehelper.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine
    copying build\lib\ MATLAB \engine\enginesession.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine
    copying build\lib\ MATLAB \engine\fevalfuture.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine
    copying build\lib\ MATLAB \engine\futureresult.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine
    copying build\lib\ MATLAB \engine\ MATLAB engine.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine
    copying build\lib\ MATLAB \engine\ MATLAB future.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine
    copying build\lib\ MATLAB \engine\_arch.txt -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine
    copying build\lib\ MATLAB \engine\__init__.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine
    copying 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 \_internal
    copying build\lib\ MATLAB \_internal\mlarray_sequence.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \_internal
    copying build\lib\ MATLAB \_internal\mlarray_utils.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \_internal
    copying build\lib\ MATLAB \_internal\__init__.py -> D:\ Anaconda 3\Lib\site-packages\ MATLAB \_internal
    copying 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.pyc
    byte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine\engineerror.py to engineerror.c Python -37.pyc
    byte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine\enginehelper.py to enginehelper.c Python -37.pyc
    byte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine\enginesession.py to enginesession.c Python -37.pyc
    byte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine\fevalfuture.py to fevalfuture.c Python -37.pyc
    byte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine\futureresult.py to futureresult.c Python -37.pyc
    byte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine\ MATLAB engine.py to  MATLAB engine.c Python -37.pyc
    byte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine\ MATLAB future.py to  MATLAB future.c Python -37.pyc
    byte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \engine\__init__.py to __init__.c Python -37.pyc
    byte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \mlarray.py to mlarray.c Python -37.pyc
    byte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \mlexceptions.py to mlexceptions.c Python -37.pyc
    byte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \_internal\mlarray_sequence.py to mlarray_sequence.c Python -37.pyc
    byte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \_internal\mlarray_utils.py to mlarray_utils.c Python -37.pyc
    byte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \_internal\__init__.py to __init__.c Python -37.pyc
    byte-compiling D:\ Anaconda 3\Lib\site-packages\ MATLAB \__init__.py to __init__.c Python -37.pyc
    running install_egg_info
    Writing 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 中运行以下代码
    pip install  MATLAB _kernel
    
    • Anaconda
      会自动安装 MATLAB _kernel 包,具体如下所示,则安装成功
    (base) C:\Users\mailshaozhen>pip install  MATLAB _kernel
    Collecting  MATLAB _kernel
      Downloading https://files. Python hosted.org/packages/64/ad/5f471160ec33e2f0f3586d285cd5e3b6dff51027849f28369d43d1d57fd1/ MATLAB _kernel-0.16.7-py3-none-any.whl
    Requirement 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/s
    Requirement 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/s
    Requirement 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/s
    Requirement 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.whl
    Installing collected packages: ipyparallel, ptyprocess, pexpect, metakernel,  MATLAB -kernel
    Successfully 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,测试发现基本不用等待就会出结果。

      在这里插入图片描述

    连享会计量方法专题……

    关于我们

    • 「Stata 连享会」 由中山大学连玉君老师团队创办,定期分享实证分析经验, 公众号:StataChina
    • 公众号推文同步发布于 CSDN简书知乎Stata专栏。可在百度中搜索关键词 「Stata连享会」查看往期推文。
    • 点击推文底部【阅读原文】可以查看推文中的链接并下载相关资料。
    • 欢迎赐稿: 欢迎赐稿。录用稿件达 三篇 以上,即可 免费 获得一期 Stata 现场培训资格。
    • E-mail: StataChina@163.com
    • 往期推文:计量专题 || 精品课程 || 简书推文 || 公众号合集

    点击此处-查看完整推文列表
    欢迎加入Stata连享会(公众号: StataChina)

    相关文章

      网友评论

          本文标题:珠联璧合 II:Jupyter Notebook 与 Matla

          本文链接:https://www.haomeiwen.com/subject/xipzyctx.html