Which is better for data analysis?
There have been dozens of articles written comparing Python and R from a subjective standpoint. We’ll add our own views at some point, but this article aims to look at the languages more objectively. We’ll analyze a dataset side by side in Python and R, and show what code is needed in both languages to achieve the same result. This will let us understand the strengths and weaknesses of each language without the conjecture. At Dataquest, we teach both languages, and think both have a place in a data science toolkit.
We’ll be analyzing a dataset of NBA players and their performance in the 2013-2014 season. You can download the file here. For each step in the analysis, we’ll show the Python and R code, along with some explanation and discussion of the different approaches. Without further ado, let’s get this head to head Python vs R matchup started!
Importing a CSV
R
1 |
|
Python
1 |
|
The above code will load the CSV file nba_2013.csv
, which contains data on NBA players from the 2013-2014 season, into the variable nba
in both languages. The only real difference is that in Python, we need to import the pandas library to get access to Dataframes. In R, while we can import the data using the base R function read.csv()
, using the readr
library function read_csv()
has the advantage of greater speed and consistent interpretation of data types. Dataframes are available in both R and Python, and are two-dimensional arrays (matrices) where each column can be of a different datatype. At the end of this step, the CSV file has been loaded by both languages into a dataframe.
Finding the number of rows
R
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
Python
1 |
|
We get very similar plots in the end, but this shows how the R data science ecosystem has many smaller packages (GGally is a helper package for ggplot2, the most-used R plotting package), and many more visualization packages in general. In Python, matplotlib is the primary plotting package, and seaborn is a widely used layer over matplotlib. With visualization in Python, there is usually one main way to do something, whereas in R, there are many packages supporting different methods of doing things (there are at least a half dozen packages to make pair plots, for instance).
Make clusters of the players
One good way to explore this kind of data is to generate cluster plots. These will show which players are most similar.
R
1 |
|
Python
1 |
|
In order to cluster properly, we remove any non-numeric columns, or columns with missing values (NA
, Nan
, etc). In R, we do this by applying a function across each column, and removing it if it has any missing values or isn’t numeric. We then use the cluster package to perform k-means and find 5
clusters in our data. We set a random seed using set.seed
to be able to reproduce our results.
In Python, we use the main Python machine learning package, scikit-learn, to fit a k-means clustering model and get our cluster labels. We perform very similar methods to prepare the data that we used in R, except we use the get_numeric_data
and dropna
methods to remove non-numeric columns and columns with missing values.
Plot players by cluster
We can now plot out the players by cluster to discover patterns. One way to do this is to first use PCA to make our data 2-dimensional, then plot it, and shade each point according to cluster association.
R
1 |
|
Python
1 |
|
Made a scatter plot of our data, and shaded or changed the icon of the data according to cluster. In R, the clusplot
function was used, which is part of the cluster library. We performed PCA via the pccomp
function that is built into R.
With Python, we used the PCA class in the scikit-learn library. We used matplotlib to create the plot.
Split into training and testing sets
If we want to do supervised machine learning, it’s a good idea to split the data into training and testing sets so we don’t overfit.
R
1 |
|
Python
1 |
|
You’ll notice that R has many more data-analysis focused builtins, like floor
, sample
, and set.seed
, whereas these are called via packages in Python (math.floor
, random.sample
, random.seed
). In Python, the recent version of pandas came with a sample
method that returns a certain proportion of rows randomly sampled from a source dataframe – this makes the code much more concise. In R, there are packages to make sampling simpler, but aren’t much more concise than using the built-in sample
function. In both cases, we set a random seed to make the results reproducible.
Univariate linear regression
Let’s say we want to predict number of assists per player from field goals made per player.
R
1 |
|
Python
1 |
|
Scikit-learn has a linear regression model that we can fit and generate predictions from. R relies on the built-in lm
and predict
functions. predict
will behave differently depending on the kind of fitted model that is passed into it – it can be used with a variety of fitted models.
Calculate summary statistics for the model
R
1 |
|
1 |
|
Dep. Variable: astR-squared: 0.568Model: OLSAdj. R-squared: 0.567[output truncated]