# Python vs R for machine learning

I'm just starting to develop a machine learning application for academic purposes. I'm currently using R and training myself in it. However, in a lot of places, I saw people using Python.

What are people using in academia and industry, and what is the recommendation?

• Well, what type of machine learning (image/video? NLP? financial? astronomy?), which classifiers, what size datasets (Mb? Gb? Tb?), what scale, what latency, on what platform (mobile/single-computer/multicore/cluster/cloud)...? What specific libraries will your application use/need, and have you checked what is available in each language? Are you just building a toy application for your personal learning or does it matter if it ever gets productized? Using open-source or proprietary? Will you be working with other people or existing apps, and what do they use/support? Web frontend/GUI? etc – smci Dec 12 '16 at 22:51
• One observation is that Python is more used by machine learning people working with big datasets while R is more used by traditional "statisticians", e.g. those working with psychology experiments with hundreds of data points. Though that difference might be diminishing. – xji Feb 15 '18 at 8:43
• python all the way man! I do 4 times the things my colleagues do in one day. And you can use python for all kind of programming tasks, not only machine learning. – Francesco Pegoraro Sep 24 '18 at 20:57

Some real important differences to consider when you are choosing R or Python over one another:

• Machine Learning has 2 phases. Model Building and Prediction phase. Typically, model building is performed as a batch process and predictions are done realtime. The model building process is a compute intensive process while the prediction happens in a jiffy. Therefore, performance of an algorithm in Python or R doesn't really affect the turn-around time of the user. Python 1, R 1.
• Production: The real difference between Python and R comes in being production ready. Python, as such is a full fledged programming language and many organisations use it in their production systems. R is a statistical programming software favoured by many academia and due to the rise in data science and availability of libraries and being open source, the industry has started using R. Many of these organisations have their production systems either in Java, C++, C#, Python etc. So, ideally they would like to have the prediction system in the same language to reduce the latency and maintenance issues. Python 2, R 1.
• Libraries: Both the languages have enormous and reliable libraries. R has over 5000 libraries catering to many domains while Python has some incredible packages like Pandas, NumPy, SciPy, Scikit Learn, Matplotlib. Python 3, R 2.
• Development: Both the language are interpreted languages. Many say that python is easy to learn, it's almost like reading english (to put it on a lighter note) but R requires more initial studying effort. Also, both of them have good IDEs (Spyder etc for Python and RStudio for R). Python 4, R 2.
• Speed: R software initially had problems with large computations (say, like nxn matrix multiplications). But, this issue is addressed with the introduction of R by Revolution Analytics. They have re-written computation intensive operations in C which is blazingly fast. Python being a high level language is relatively slow. Python 4, R 3.
• Visualizations: In data science, we frequently tend to plot data to showcase patterns to users. Therefore, visualisations become an important criteria in choosing a software and R completely kills Python in this regard. Thanks to Hadley Wickham for an incredible ggplot2 package. R wins hands down. Python 4, R 4.
• Dealing with Big Data: One of the constraints of R is it stores the data in system memory (RAM). So, RAM capacity becomes a constraint when you are handling Big Data. Python does well, but I would say, as both R and Python have HDFS connectors, leveraging Hadoop infrastructure would give substantial performance improvement. So, Python 5, R 5.

So, both the languages are equally good. Therefore, depending upon your domain and the place you work, you have to smartly choose the right language. The technology world usually prefers using a single language. Business users (marketing analytics, retail analytics) usually go with statistical programming languages like R, since they frequently do quick prototyping and build visualisations (which is faster done in R than Python).

• R hardly beats python in visualization. I think it's rather the reverse; not only does python have ggplot (which I don't use myself, since there are more pythonic options, like seaborn), it can even do interactive visualization in the browser with packages like bokeh. – Emre Jun 12 '14 at 15:57
• Also R has the ability to interactive viz with Shiny. – stanekam Jun 12 '14 at 22:04
• Librariers - I do not agree at all with that. R is by far the richest tool set, and more than that it provides the information in a proper way, partly by inheriting S, partly by one of the largest community of reputed experts. – rapaio Jun 13 '14 at 6:11
• "Speed: R software initially had problems with large computations (say, like nxn matrix multiplications). But, this issue is addressed with the introduction of R by Revolution Analytics. They have re-written computation intensive operations in C which is blazingly fast. Python being a high level language is relatively slow." I'm not an experienced R user, but as far as I know pretty much everything with low-level implementations in R also has a similar low-level implementation in numpy/scipy/pandas/scikit-learn/whatever. Python also has numba and cython. This point should be a tie. – Dougal Apr 3 '15 at 22:05
• For you "Dealing with Big Data" comment, I would add that python is one of the 3 languages supported by apache spark, which has blazing fast speeds. Your comment about R having a C back end is true, but so does python the scikitlearn library is very fast as well. I think your post has nice balance, but I contend that speed is at least a tie, and scalability (i.e. handling big data) is certainly in favor of python. – j.a.gartner May 12 '15 at 16:31

There is nothing like "python is better" or "R is much better than x".

The only fact I know is that in the industry allots of people stick to python because that is what they learned at the university. The python community is really active and have a few great frameworks for ML and data mining etc.

But to be honest, if you get a good c programmer he can do the same as people do in python or r, if you got a good java programmer he can also do (near to) everything in java.

So just stick with the language you are comfortable with.

• But what about the libraries? There are advanced R packages (think Ranfom Forest or Caret) that would be utterly impractical to reimplement in a general purpose language such us C or Java – Santiago Cepas Jun 13 '14 at 9:35
• mahout i.e. supports random forest for java – Johnny000 Jun 13 '14 at 10:17
• Yeah maybe, but R doesn't bring the performance at all that you need for proccessing big sets of data and most of the time you have really big datasets in industrial use. – Johnny000 Jun 13 '14 at 10:41
• Yes, a good programmer can do the same in C. BUT a bad programmer can do it in Python as fast as an experienced programmer can do it in C. – Pithikos Jan 13 '15 at 13:38
• I don't think that's always true @Pithikos Given the underlying math formulas, I can usually implement them faster myself with VB/T-SQL faster than I can by wading through the unnecessarily arcane syntax for either R or Python libraries. And in the process, make the resulting code far more scalable. I'm glad these libraries exist but there are downsides built into them; in some situations and particular projects it's better to bypass them. – SQLServerSteve May 8 '17 at 20:15

The programming language 'per se' is only a tool. All languages were designed to make some type of constructs more easy to build than others. And the knowledge and mastery of a programming language is more important and effective than the features of that language compared to others.

As far as I can see there are two dimensions of this question. The first dimension is the ability to explore, build proof of concepts or models at a fast pace, eventually having at hand enough tools to study what is going on (like statistical tests, graphics, measurement tools, etc). This kind of activity is usually preferred by researchers and data scientists (I always wonder what that means, but I use this term for its loose definition). They tend to rely on well-known and verified instruments, which can be used for proofs or arguments.

The second dimension is the ability to extend, change, improve or even create tools, algorithms or models. In order to achieve that you need a proper programming language. Roughly all of them are the same. If you work for a company, than you depend a lot on the company's infrastructure, internal culture and your choices diminish significantly. Also, when you want to implement an algorithm for production use, you have to trust the implementation. And implementing in another language which you do not master will not help you much.

I tend to favor for the first type of activity the R ecosystem. You have a great community, a huge set of tools, proofs that these tools works as expected. Also, you can consider Python, Octave (to name a few), which are reliable candidates.

For the second task, you have to think before at what you really want. If you want robust production ready tools, then C/C++, Java, C# are great candidates. I consider Python as a second citizen in this category, together with Scala and friends. I do not want to start a flame war, it's my opinion only. But after more than 17 years as a developer, I tend to prefer a strict contract and my knowledge, than the freedom to do whatever you might think of (like it happens with a lot of dynamic languages).

Personally, I want to learn as much as possible. I decided that I have to choose the hard way, which means to implement everything from scratch myself. I use R as a model and inspiration. It has great treasures in libraries and a lot of experience distilled. However, R as a programming language is a nightmare for me. So I decided to use Java, and use no additional library. That is only because of my experience, and nothing else.

If you have time, the best thing you can do is to spend some time with all these things. In this way you will earn for yourself the best answer possible, fitted for you. Dijkstra said once that the tools influence the way you think, so it is advisable to know your tools before letting them to model how you think. You can read more about that in his famous paper called The Humble Programmer

I would add to what others have said till now. There is no single answer that one language is better than other.

Having said that, R has a better community for data exploration and learning. It has extensive visualization capabilities. Python, on the other hand, has become better at data handling since introduction of pandas. Learning and development time is very less in Python, as compared to R (R being a low level language).

I think it ultimately boils down to the eco-system you are in and personal preferences. For more details, you can look at this comparison here.

• "R has a better community for [...] learning" - I guess this highly depends on the type of learning. How much is going on with neural networks (arbitrary feed-forward architectures, CNNs, RNNs) in R? – Martin Thoma Jul 19 '15 at 14:41
• R is not really that "low level" IMO. It's also a dynamic language. – xji Feb 15 '18 at 8:45

There isn't a silver bullet language that can be used to solve each and every data related problem. The language choice depends on the context of the problem, size of data and if you are working at a workplace you have to stick to what they use.

Personally I use R more often than Python due to its visualization libraries and interactive style. But if I need more performance or structured code I definitely use Python since it has some of the best libraries as SciKit-Learn, numpy, scipy etc. I use both R and Python in my projects interchangeably.

So if you are starting on data science work I suggest you to learn both and it's not difficult since Python also provides a similar interface to R with Pandas.

If you have to deal with much larger datasets, you can't escape eco-systems built with Java(Hadoop, Pig, Hbase etc).

There is no "better" language. I have tried both of them and I am comfortable with Python so I work with Python only. Though I am still learning stuff, but I haven't encounter any roadblock with Python till now. The good thing about Python is community is too good and you can get a lot of help on the Internet easily. Other than that, I would say go with the language you like not the one people recommend.

In my experience, the answer depends on the project at hand. For pure research, I prefer R for two reasons: 1) broad variety of libraries and 2) much of the data science literature includes R samples.

If the project requires an interactive interface to be used by laypersons, I've found R to be too constrained. Shiny is a great start, but it's not flexible enough yet. In these cases, I'll start to look at porting my R work over to Python or js.

Most of the aforementioned wonderful R libraries are GPL (e.g. ggplot2, data.table). This prevents you from distributing your software in a proprietary form.

Although many usages of those libraries do not imply distribution of the software (e.g. to train models offline), the GPL may by itself lure away companies from using them. At least in my experience.

In the python realm, on the other hand, most libraries have business-friendly distribution licenses, such as BSD or MIT.

In academia, licensing issues normally are non-issues.

Not much to add to the provided comments. Only thing is maybe this infographic comparing R vs Python for data science purposes http://blog.datacamp.com/r-or-python-for-data-analysis/

One of real challenges, I faced with R is different packages compatible with different versions.. quite a lot R packages are not available for latest version of R.. And R quite a few time gives error due to library or package was written for older version..

• I'm not sure this is a particular problem with R, or that it answers the question of how Python and R differ. – Sean Owen Oct 21 '14 at 14:02

I haven't tried R (well, a bit, but not enough to make a good comparison). However, here are some of Pythons strengths:

• Very intuitive syntax: tuple unpacking, element in a_list, for element in sequence, matrix_a * matrix_b (for matrix multiplication), ...
• Many libraries:
• scipy: Scientific computations; many parts of it are only wrappers for pretty fast Fortran code
• theano > Lasagne > nolearn: Libraries for neural networks - they can be trained on GPU (nvidia, CUDA is required) without any adjustment
• sklearn: General learning algorithms
• Good community:
• IPython notebooks
• Misc:
• 0-indexed arrays ... I made that error all the time with R.
• Established package structures
• Good support for testing your code

I prefer Python over R because Python is a complete programming language so I can do end to end machine learning tasks such as gather data using a HTTP server written in Python, perform advanced ML tasks and then publish the results online. This can all be done in Python. I actually found R to be harder to learn and the payoffs for learning Python are much greater because it can be used for pretty much any programming task.

• You can do all those 3 things very easily in R – Gaius Aug 25 '17 at 13:04

R: R is the Open source counterpart. which has traditionally been used in academics and research. Because of its open source nature, latest techniques get released quickly. There is a lot of documentation available over the internet and it is a very cost-effective option. Python: With origination as an open source scripting language, Python usage has grown over time. Today, it sports libraries (numpy, scipy and matplotlib) and functions for almost any statistical operation / model building you may want to do. Since introduction of pandas, it has become very strong in operations on structured data.

Python Code

# Import other necessary libraries like pandas, numpy...

from sklearn import linear_model

# Identify feature and response variable(s) and values must be numeric and numpy arrays

x_train=input_variables_values_training_datasets y_train=target_variables_values_training_datasets x_test=input_variables_values_test_datasets

# Create linear regression object

linear = linear_model.LinearRegression()

# Train the model using the training sets and check score

linear.fit(x_train, y_train) linear.score(x_train, y_train)

# Equation coefficient and Intercept

print('Coefficient: \n', linear.coef_) print('Intercept: \n', linear.intercept_)

# Predict Output

predicted= linear.predict(x_test) R Code

# Identify feature and response variable(s) and values must be numeric and numpy arrays

x_train <- input_variables_values_training_datasets y_train <- target_variables_values_training_datasets x_test <- input_variables_values_test_datasets x <- cbind(x_train,y_train)

# Train the model using the training sets and check score

linear <- lm(y_train ~ ., data = x) summary(linear)

# Predict Output

predicted= predict(linear,x_test)

I do not think Python has point-click GUI that turn it into SPSS and SAS. Playing around with those is genuinely fun.

I got this image in a linkedin post. Whenever I get a doubt of using python or R, I look into it and it proves to be very useful.

• So what do you choose? – Serhii Polishchuk Nov 17 '18 at 21:35

## protected by Dawny33♦Aug 24 '17 at 6:57

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).