美文网首页大数据 爬虫Python AI SqlPython小哥哥
放弃 PK,拥抱合作——R 和 Python 能做出什么新花样?

放弃 PK,拥抱合作——R 和 Python 能做出什么新花样?

作者: 14e61d025165 | 来源:发表于2019-05-09 19:28 被阅读0次
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1557401220658" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

Python学习交流群:1004391443,这里是python学习者聚集地,有大牛答疑,有资源共享!小编也准备了一份python学习资料,有想学习python编程的,或是转行,或是大学生,还有工作中想提升自己能力的,正在学习的小伙伴欢迎加入学习。

我们更应该关注的是技能,而不是工具。

如果你想进入数据科学领域,你可能立即会想到R和Python。然而,我们并不是要以作为两种选择来考虑他们,相反地,我们更多的是去比较他们。R和Pyhton在他们各自的领域里,都是非常完美的工具。尽管如此,他们往往成为各自敌人而争吵。如果你在谷歌搜索栏里输入“R vs Python”,你会看到非常多的关于他们霸权之争。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1557401220662" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

产生这种观点的原因之一是,人们根据所使用的编程语言的选择将数据科学领域划分为不同的阵营——R阵营和Python阵营,历史证明了两个阵营不能和谐相处。两大阵营的成员都坚信他们对语言的选择优于对方。所以,在某种程度上,分歧不在于工具,而在于使用这些工具的人。

为啥不能将两者结合?

在数据科学领域中有一些人同时使用Python和R,但是他们的比例很小。另一方面,有很多人只致力于一种编程语言,但他们希望能够使用对手的一些功能。例如,R用户有时渴望Python固有的面向对象能力,同样地,一些Python用户渴望R中可用的统计分布的广泛范围。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1557401220667" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

上图显示的是Red Monk在2018年第三季度的调查结果。这些结果是基于Stack Overflow和Github上流行的语言得出的,并清楚地表明R和Python的评分都很高。因此,我们没有内在的理由不能在同一个项目上与他们一起工作。我们的最终目标应该是做更好的分析,得出更好的见解,选择一种编程语言不应该成为实现这一目标的障碍。

纵观R和python

让我们来看看这两种语言的各个方面和他们有哪些优势与弊端。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1557401220672 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

pyhton

自发布于与1991年,pyhton变得十分流行并且它广泛地用于数据处理。以下就是python为什么这么流行的原因:

  • 面对对象
  • 通用
  • 有大量的扩展库和无与伦比的社区支持
  • 易于理解和学习
  • 由于拥有大量用于科学计算和机器学习的包,例如pandas,numpy 和scikit-learn,这使得在机器学习领域中,python变成了一个完美的选择.

不幸的是,pyhton并没有专门用于统计计算的包。但是,R有.

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1557401220678 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

R

R在1995年发布。从那时候开始,R在数据科学领域就变成了最有用的工具之一。它具有如下优势

  • R几乎拥有所能想到的用于统计计算的包。CRAN目前已经拥有超过10000个包了。
  • 配备了完美的可视化库,例如ggplot
  • 能够进行独立分析

R虽然性能如此强大,但是它不是最快的语言并且在处理大数据集的时候可能会消耗大量的内存。

两全其美

我们可以利用R的统计能力和Python的编程能力吗?那么,当我们可以很容易地在R或Python脚本中嵌入SQL代码时,为什么不将R和Python混合在一起呢?

基本上有两种方法可以同时在一个项目中使用Python和R。

Python中的R

  • PypeR

PypeR提供了一种通过管道从Python访问R的简单方法。PypeR还包含在Python的包索引中,这为安装提供了更方便的方法。当不需要在Python和R之间频繁地交互数据传输时,PypeR特别有用。通过管道运行R, Python程序在子进程控制、内存控制和跨流行操作系统平台(包括Windows、GNU Linux和Mac OS)的可移植性方面获得了灵活性。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1557401220689" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

  • pyRserve

pyRserve使用Rserve作为RPC连接网关。通过这样的连接,可以在Python的R中设置变量,也可以远程调用R函数。R对象作为python实现的类的实例公开,在许多情况下,R函数作为这些对象的绑定方法。

  • rpy2

rpy2在Python进程中运行嵌入式R。它创建了一个框架,可以将Python对象转换为R对象,将它们传递给R函数,并将R输出转换回Python对象。rpy2使用得更多,因为它是一个正在积极开发的。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1557401220695" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

rpy2在Python进程中运行嵌入式R。它创建了一个框架,可以将Python对象转换为R对象,将它们传递给R函数,并将R输出转换回Python对象。由于它是一个正在积极开发的项目,所以它被使用得更频繁。

  • Basic Plot
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1557401220701 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

https://rpy2.github.io/doc/latest/html/graphics.html#plot

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1557401220709 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

  • Geometry
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1557401220713" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

https://rpy2.github.io/doc/latest/html/graphics.html#geometry

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1557401220719 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

资源

想要更深入地了解rpy2,请参阅以下资源:

  • rpy2’s Official Documentation
  • RPy2: Combining the Power of R + Python for Data Science
  • Accessing R from Python using RPy2

R调用Python

我们可以选择以下的一种方法在Python中运行R脚本:

  • rJython

这个工具包使用Jython实现到Python的接口。它的目的是使其他包能够在R中嵌入Python代码。

  • rPython

rPython也是一种工具包,使得R语言可以调用Python。使用rPython,通过R语言可以运行Python代码,调用函数,分配和检索变量等。 雷锋网雷锋网雷锋网 (公众号:雷锋网)

  • SnakeCharmR

SnakeCharmR是改良版的 rPython。它源于 rPython 的 'jsonlite' 分支,相比于 rPython 有诸多提升。

  • PythonInR

PythonInR提供了R与Python交互的函数,简化了通过R访问Python的过程。

  • reticulate

reticulate包为Python和R之间的协同操作提供了全套工具。 在以上提供的所有方法中,reticulate被最广泛使用,更重要的是,它使用Rstudio开发的。Reticulate将Python会话嵌入到R会话中,支持无缝、高性能的交互性能。 该包使您能够将Python代码编织到R中,从而创建一种将两种语言融合在一起的新项目。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1557401220729" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

reticulate 包提供下列工具:

  • 支持从R中以多种方式调用Python,包括R标记、加载Python脚本、导入Python模块,以及在R会话中交互式地使用Python。
  • 实现R和Python对象之间的转换 (例如:R和Pandas数据帧之间,或者R矩阵和NumPy数组之间的转换)。
  • 灵活地绑定到不同版本的Python,包括虚拟环境和Conda环境。

资源

使用网状包的一些很好的资源是:

结论

R和Python都是非常健壮的语言,它们中的任何一种实际上都足以执行数据分析任务。然而,这两者肯定都有一些高和低的点,如果我们能利用两者的优势,我们可以做得更好。无论哪种方式,掌握这两方面的知识都将使我们更加灵活,从而增加我们能够在多种环境中工作的机会。

相关文章

网友评论

    本文标题:放弃 PK,拥抱合作——R 和 Python 能做出什么新花样?

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