Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make full use of R+Python

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I would like to talk to you about how to make full use of R+Python, many people may not know much about it. In order to make you understand better, the editor summarized the following content for you. I hope you can get something according to this article.

If you work in data science, you may immediately think of two programming languages: r and Python.

In fact, R and Python are good tools themselves, but they are generally considered competitors. Today's recommended article will compare the two, rather than seeing them as two options.

If you type R vs Python in the Google search bar, you will immediately get a large number of articles about one party's advantages.

One of the reasons for this result is that people divide the field of data science into camps, an R camp and an Python camp according to their choice of programming language. And the two camps often do not get along in harmony, and their members believe that their language is better than the other. So, to some extent, the difference is not in the tools, but in--

Why not use at the same time?

A small number of people in data science use both Python and R. But in fact, there are many people who use only one programming language, but they also want to use some of the features of another software. For example, R users sometimes want to use the native object-oriented capabilities of Python, just as some Python users want to use the various statistical distributions in R.

The picture above is the results of a survey conducted by Red Monk in the third quarter of 2018. This survey data comes from the popularity of languages on Stack Overflow and Github, which clearly shows that both R and Python are more popular. Therefore, there is no inherent reason why we can't use both on the same project. Our ultimate goal should be better analysis and better understanding, and the choice of programming language should not be an obstacle to achieving this goal.

Review of R and Python

Let's take a look at these languages and their advantages and disadvantages.

Python

Since its release in 1991, Python has been very popular and widely used in data processing. The advantages are:

Object-oriented language.

A wide range of uses.

There are many extensions and strong community support.

Simple, easy to understand and learn.

On software packages such as pandas,numpy and scikit-learn, Python is the choice of machine learning activities.

However, unlike R, Python does not have a dedicated package for statistical calculations.

R

The version of R was released in 1995, and since then it has become one of the most commonly used data science tools in the industry.

An installation package that contains almost every statistical application imaginable. CRAN currently has more than 10k packages.

Equipped with a complete set? Visualization libraries, such as ggplot2.

Ability to conduct independent analysis.

The well-performing R is not the fastest language and may sometimes have too much memory when dealing with large datasets.

Make full use of these two languages

Can we take advantage of both the statistical power of R and the programming ability of Python? When we can easily embed SQL code in R or Python scripts, why not mix R and Python together?

Basically, there are two ways to use both Python and R.

Using R in Python

PypeR

(http://bioinfo.ihb.ac.cn/softwares/PypeR/)

PypeR provides an easy way to access R from Python through pipes. PypeR is also included in Python's Package Index, which provides a more convenient way to install. PypeR is especially useful when frequent interactive data transfers are not required between Python and R. Under the subflow control of the process operating system platform (including Windows, GNU Linux, and Mac OS), the flexibility of memory control and portability can be achieved by running the R _ Jet Python program through the pipeline.

PyRserve

(https://pypi.org/project/pyRserve/)

PyRserve uses Rserve as the RPC connection gateway. With this connection, you can set variables with R in Python, or you can call R functions remotely. R objects are exposed as instances of classes implemented by Python, and in many cases R functions are used as binding methods for these objects.

Rpy2

(https://rpy2.bitbucket.io/)

Rpy2 runs embedded R in the Python process. It creates a framework that converts Python objects to R objects, passes them to R functions, and converts R output back to Python objects. Rpy2 is more commonly used and is under active development.

One of the advantages of using R in Python is that you can easily use R's powerful software packages, such as ggplot2,tidyr,dplyr, in Python. As an example, let's look at how to use ggplot2 for mapping in Python.

Basic scene

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

Geometry

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

Using Python in R

We can use one of the following alternatives to run the R script in Python:

RJython

(https://r-forge.r-project.org/projects/rjython/)

This package implements the interface of Python through Jython. It is designed to enable other packages to embed python code along with R.

RPython

(https://cran.r-project.org/web/packages/rPython/index.html)

RPython is another package that allows R to call Python. It makes it possible to run Python code in R, make function calls, assign and retrieve variables, and so on.

SnakeCharmR

(https://github.com/asieira/SnakeCharmR)

SnakeCharmR is a modern version of rPython. It is a branch of 'rPython', it uses' jsonlite' and has a lot of improvement over rPython.

PythonInR

(https://bitbucket.org/Floooo/pythoninr/)

PythonInR makes it very easy to access Python from within R by providing functions that interact with Python from within.

Reticulate

(https://github.com/rstudio/reticulate)

The mesh package provides a comprehensive set of tools for interoperability between Python and R. Of all the options mentioned above, this is the most widely used, but also because it is being actively developed by Rstudio. Reticulate embeds Python sessions in R sessions to achieve seamless, high-performance interoperability. This package enables you to grid the Python code into R, creating a new project that weaves the two languages together.

The mesh package provides the following facilities:

Calling Python from R in a variety of ways, including R Markdown, getting Python scripts, importing Python modules, and interactively using Python in R sessions.

Conversion between R and Python objects (for example, between R and Pandas data frames, or between R matrices and NumPy arrays).

Flexible binding to different versions of Python, including virtual and Conda environments.

R and Python are both very powerful languages, and any one of them is sufficient to perform data analysis tasks. However, there must be some advantages and disadvantages for these two, if we can take advantage of the advantages of both, we can certainly do better. In any case, a full understanding of both allows us to work in more environments. The above is the editor for you to share how to make full use of R+Python, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

After reading the above, do you have any further understanding of how to make full use of R+Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report