Python调用R编程——rpy2

作者&投稿:茹胀 (若有异议请与网页底部的电邮联系)
~

在Python调用R,最常见的方式是使用rpy2模块。

The package is made of several sub-packages or modules:

Importing R packages is often the first step when running R code, and rpy2 is providing a function rpy2.robjects.packages.importr() that makes that step very similar to importing Python packages.

We mentioned earlier that rpy2 is running an embedded R. This is may be a little abstract, so there is an object rpy2.robjects.r to make it tangible.

The __getitem__() method of rpy2.robjects.r, gets the R object associated with a given symbol

The object r is also callable , and the string passed in a call is evaluated as R code.

An R object has a string representation that can be used directly into R code to be evaluated.

In R, data are mostly represented by vectors, even when looking like scalars. When looking closely at the R object pi used previously, we can observe that this is in fact a vector of length 1.

Creating R vectors can be achieved simply.

The easiest way to create such objects is to do it through R functions.

Calling R functions is disappointingly similar to calling Python functions.

By default, calling R functions return R objects.

Linear models

Creating an R vector or matrix, and filling its cells using Python code

This module should be the right pick for casual and general use. Its aim is to abstract some of the details and provide an intuitive interface to both Python and R programmers.

The instance can be seen as the entry point to an embedded R process. The elements that would be accessible from an equivalent R environment are accessible as attributes of the instance.

When safety matters most, we recommend using __getitem__() to get a given R object.

Storing the object in a python variable will protect it from garbage collection, even if deleted from the objects visible to an R user.

Just like it is the case with RPy-1.x, on-the-fly evaluation of R code contained in a string can be performed by calling the r instance.

The astute reader will quickly realize that R objects named by python variables can be plugged into code through their R representation.

R environments can be described to the Python user as an hybrid of a dictionary and a scope.

The first of all environments is called the Global Environment, that can also be referred to as the R workspace.

Assigning a value to a symbol in an environment has been made as simple as assigning a value to a key in a Python dictionary.

An environment is also iter-able, returning all the symbols (keys) it contains.

R functions exposed by rpy2's high-level interface can be used:

This is all looking fine and simple until R arguments with names such as na.rm are encountered. By default, this is addressed by having a translation of ‘.’ (dot) in the R argument name into a ‘_’ in the Python argument name.

In Python one can write:

R is capable of introspection, and can return the arguments accepted by a function through the function formals().

The method Function.rcall() is an alternative way to call an underlying R function.

For tasks such as modelling and plotting, an R formula can be a terse, yet readable, way of expressing what is wanted.

The class robjects.Formula is representing an R formula.

Other options are:

This is achieved by the R functions library() and require() (attaching the namespace of the package to the R search path).

Beside functions and environments, most of the objects an R user is interacting with are vector-like. For example, this means that any scalar is in fact a vector of length one.

The class Vector has a constructor:

Creating vectors can be achieved either from R or from Python.

When the vectors are created from R, one should not worry much as they will be exposed as they should by rpy2.robjects.

When one wants to create a vector from Python, either the class Vector or the convenience classes IntVector, FloatVector, BoolVector, StrVector can be used.

Extracting, Python-style

The python __getitem__() method behaves like a Python user would expect it for a vector (and indexing starts at zero).

Extracting, R-style

Access to R-style extracting/subsetting is granted though the two delegators rx and rx2, representing the R functions [ and [[ respectively.

Assigning, Python-style

Since vectors are exposed as Python mutable sequences, the assignment works as for regular Python lists.

In R vectors can be named, that is elements of the vector have a name.

Assigning, R-style

The attributes rx and rx2 used previously can again be used:

For the sake of complete compatibility with R, arguments can be named (and passed as a dict or rpy2.rlike.container.TaggedList).

In S/Splus/R special NA values can be used in a data vector to indicate that fact, and rpy2.robjects makes aliases for those available as data objects NA_Logical, NA_Real, NA_Integer, NA_Character, NA_Complex .

To expose that to Python, a delegating attribute ro is provided for vector-like objects.

R vectors can have a name given to all or some of the elements. The property names can be used to get, or set, those names.

Array

In R, arrays are simply vectors with a dimension attribute. That fact was reflected in the class hierarchy with robjects.Array inheriting from robjects.Vector.

Matrix

A Matrix is a special case of Array. As with arrays, one must remember that this is just a vector with dimension attributes (number of rows, number of columns).

DataFrame

In rpy2.robjects, DataFrame represents the R class data.frame.

Creating a DataFrame can be done by:

The DataFrame constructor accepts either an rinterface.SexpVector (with typeof equal to VECSXP, that is, an R list) or any Python object implementing the method items() (for example dict or rpy2.rlike.container.OrdDict).

To create a DataFrame and be certain of the clumn order order, an ordered dictionary can be used:

Here again, Python’s __getitem__() will work as a Python programmer will expect it to:

The DataFrame is composed of columns, with each column being possibly of a different type:

The approach followed in rpy2 has 2 levels (rinterface and robjects), and conversion functions help moving between them.

R vectors are mapped to Python objects implementing the methods __getitem__() / __setitem__() in the sequence protocol so elements can be accessed easily.

R functions are mapped to Python objects implementing the __call__() so they can be called just as if they were functions.

R environments are mapped to Python objects implementing __getitem__() / __setitem__() in the mapping protocol so elements can be accessed similarly to in a Python dict.

In its high-level interface rpy2 is using a conversion system that has the task of convertion objects between the following 3 representations: - lower-level interface to R (rpy2.rinterface level), - higher-level interface to R (rpy2.robjects level) - other (no rpy2) representations

R vectors or arrays can be converted to numpy arrays using numpy.array() or numpy.asarray().

The activation (and deactivation) of the automatic conversion of numpy objects into rpy2 objects can be made with:




python中的r: tuple index out of range问题
在Python编程中,遇到`tuple index out of range`问题通常是由于对元组(tuple)的索引操作不当。例如,当你看到`studen=('xzj','jzx','zxj','jxz')`这样的代码,如果忘记删除最后一个逗号,就会产生误解。实际上,这会导致`studen`成为一个包含两个元素的元组,每个元素又是一个子元组,即`studen...

r语言和python哪个好学
3. Python优于R的另一个优势是将模型部署到软件的其他部分。Python是一种通用性语言,用python编写应用程序,包含基于Python的模型的过程是无缝的。4. Python是一套比较平衡的语言,各方面都可以,无论是对其他语言的调用,和数据源的连接、读取,对系统的操作,还是正则表达和文字处理,Python都有着明显...

Python和R语言的区别
介绍 Python和R本身在数据分析和数据挖掘方面都有比较专业和全面的模块,很多常用的功能,比如矩阵运算、向量运算等都有比较高级的用法,所以使用起来产出比大。这两门语言对于平台方面适用性比较广,linux、window都可以使用,并且代码可移植性还算不错的。对于学数理统计的人来说,应该大多用过MATLAB以及...

r语言和python的区别
1、适用场景不同 R适用于数据分析任务需要独立计算或单个服务器的应用场景。Python作为一种粘合剂语言,在数据分析任务中需要与Web应用程序集成或者当一条统计代码需要插入到生产数据库中时,使用Python更好。2、任务不同 在进行探索性统计分析时,R胜出。它非常适合初学者,统计模型仅需几行代码即可实现...

Python中文件编辑下r和r+有什么区别? 还有w和w+
r是只读不写,w是只写不读 r+,w+大同小异,都是可读可写。区别是r+读写不存在的文件会报错,w+在文件不存在的情况下会自动创建文件。

python中 r'', b'', u'', f'' 的含义
1、字符串前+u,如u"我是张三":前缀u表示该字符串是unicode编码,在Python2中常被用在中文字符的字符串前,防止因为编码问题所导致的乱码,一般在文件开头标明编码方式采用utf8。在Python3中,所有的字符串默认都是unicode字符串。2、字符串前+r,如r"\\n\\n\\n\\n\\n":表示一个正常的字符串,...

python和r哪个难一点
R语言 R是由统计学家开发的,它的出生就肩负着统计分析、绘图、数据挖掘的重要使命。因此在R的语言体系里,有非常多统计学的原理和知识。如果你具备一些统计背景,R会令你使用各类model和复杂的公式时更加愉悦有爽感,因为你总能找到对应的package,并且几行代码就可以调用搞定。Python Python的创始人初衷...

Python和R的区别
Python与R的区别:虽然R语言更为专业,但Python是为各种用例设计的通用编程语言。如果你第一次学编程,会发现Python上手更容易,应用范围也较广,如果你对编程已经有了一定的基础,或者就是以数据分析为中心的特定职业目标,R语言可能会更适合你的需求,Python和R也有很多相似之处,两者都是流行的开源编程...

什么是R\/Python语言?
Python的设计理念是“简单”、“明确”、“优雅”。Python由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。像Perl语言一样, Python 源代码同样遵循 GPL(GNU General Public License)协议。 对比Java 读取文件 在 Java中需要10行代码 Python只需要两行.R是用于统计分析、绘图的语言和...

Python|运行程序的4种方式
第一种:第一种就是最直观的方式了。将python或pycharm应用程序打开编程即可。第二种:用windows徽标+R再输入cmd打开命令行,然后键入python进入python环境,即可在命令行下编程。第三种:点击windows徽标,再输入IDLE即可打开python进行编程。第四种:选择任意编辑器比如记事本用编程语法写程序,再将文件...

庐江县17695228734: 求助,R语言在python中调用问题 -
蒋缪妇科: 你使用的方法没有问题.r.mydose()调用后返回的东西不是报错,是因为mydose这个function返回值就是NULL,所以rpy就相应的返回了rpy2.rinterface.NULL,没有问题.以上是我的测试nofunc是一个什么也不做的functionhello是输出Hello world的function

庐江县17695228734: Python 怎么调用 R 语言?rpy2 windows 下不好用了. -
蒋缪妇科: windows的话rpy2好像只支持winXP.所以干脆就不要用在Python里用R了,直接用python吧.

庐江县17695228734: 如何做到R和python的完美配合 -
蒋缪妇科: python是一门标准化的编程语言,结构比较规范,但是很多时候代码写起来不如R简便.因此,在python中用rpy2包(接口)调用R代码不失为一种两全其美的办法.此外,python中直接装个ggplot的第三方模块来代替matplotlib来画图,可以有效提高工作效率的.

庐江县17695228734: R和Python -
蒋缪妇科: Python与R的区别和联系 1、区别Python与R的区别是显而易见的,因为R是针对统计的,python是给程序员设计的.2012年R是学术界的主流,但是现在Python正在慢慢取代R在学术界的地位.Python与R相比速度要快.Python可以直接处理上...

庐江县17695228734: 利用python对外部程序进行操作 -
蒋缪妇科: 代码如下 复制代码 object_id_list=[1, 3, 88, 99] f=open('mylist', “w”) for id in object_id_list: f.writelines(str(id)) f.close() #只有输入这一句之后才会真正写入到文件中 cat mylist138899% # 最后有一个%表示没有换行>>> object_id_list=[1, 3, ...

庐江县17695228734: eclipse中 java程序怎样调用python -
蒋缪妇科: eclipse中 java程序调用python方法如下:package com.lyz.test.jython; import org.python.util.PythonInterpreter; /** * 第一个Jython程序 * @author liuyazhuang * */ public class FirstJythonScript { public static void main(String args[]) { PythonInterpreter ...

庐江县17695228734: oracle怎么调用python代码 -
蒋缪妇科: 1、把python程序放到一个bat文件里面:python.bat [内容:start python clearlog.py]2、在oracle里面创建JOB scheduler3、在JOB scheduler 里面 调用 xxxx.bat文件 即可.

庐江县17695228734: python shell 调用.py的程序 路径问题 -
蒋缪妇科: 输入import a即可. 注意这样是以模块方式调用, __name__ == '__main__'不为真.如果找不到a, 可以添加a所在的路径后再import.假设a在E:\pythonwork\a.py :import sys sys.path += "E:\pythonwork" import a

庐江县17695228734: python3如何实现 远程调用 -
蒋缪妇科: XMLRPCLIB或者是rpyc两个库都简单好用.还有些老版本的远程 过程 调用,都有很多限制.其实restful方式,通过WEB远程调用也不是不可以呵. rpyc效率不高,不过简单,强大. 可以在pypi这个网站下载,其它的包也可以在这里下载.另外如果自己想追求效率,可以直接使用socketserver,帮助里有一个例子,复制过来改一改就能用. 此外 twist方式实现的通讯也很强大.rpyc是源码.你直接python setup.py install就可以安装上. 不需要编译好的版本.也不存在不兼容的问题.此外thrift, avro都是hadoop大数据环境里常用的协议,还有protocol_buf,都可以研究一下.

庐江县17695228734: 如何让python调用C和C++代码 -
蒋缪妇科: 如何让python调用C和C++代码安装python后,会有一个chm格式的python手册.要搞明白如何让python调用C/C++代码(也就是写python的 extension),你需要征服手册中的 <<Extending && embedding>>厚厚的一章.在昨天花了一个小时看...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网