R2 Score, also known as R-Squared is an evaluation matrix used to evaluate the performance of a regression model in Machine Learning. A Regression model is a model that is used to predict any continuous output value. Some of the regression models in Machine Learning are Linear Regression, Decision Trees Regressor, Extra Trees Regressor, and many more. In this article, we are going to discuss what is the r2 Score and how to find it in Python. Mainly we will use the Sklearn module to find the R-square value.
Apart from the r2 score, some other evaluation matrices used to evaluate the performance of regression models are Mean Absolute Error and Mean Squared Error.
What is the R2 Score in Python?
R2 score is a regression model’s evaluation matrix that helps us to know how accurate or close the predictions are to the actual values. R2 score and R-Square are the same thing so don’t get confused when you saw R-square. A simple formula for r2 Score is:
1 − sum squared regression (SSR) total sum of squares (SST) , = 1 − ∑ ( y i − y i ^ ) 2 ∑ ( y i − y ¯ ) 2
So in other words, the r2 score is the sum squared regression is the sum of the residuals squared, and the total sum of squares is the sum of the distance the data is away from the mean all squared.
One of the biggest reasons to prefer the R2 score over other evaluation matrices is that it is less sensitive to outliers. This means if there are outliers in your dataset, they will directly impact other evaluation scores but will have very less effect on the r2 score.
Key Features of R2 Score in Python
Apart from being, less sensitive to outliers, there are many other important features of the R2 Score that makes it one of the best performance evaluator for regression models.
- R2-score measures the goodness of the regression model.
- It also helps to explain the proportion of variance in the model.
- It is easy to compare models using R2-score.
- There are no limitations
- Non-sensitive to outliers
- Gives values from 0 to 1.
- Easy to interpret the results.
- Simple formula
- Commonly used in Machine learning
- And many more
How to Find the R2 Score in Python?
Well, there can be various methods and approaches to find the R2 score in Python. One of the commonly used methods is using the Sklearn module. Sklearn is a Python module that contains a lot of important methods for Machine Learning Developers. It helps with data preprocessing, model training, model evaluation, and tunning of the model. Here we will see how we can use the Sklearn module to find the R2 Score.
The Sklearn modules contain many submodules which contain different methods. One of the submodules is metrics which contains evaluation metrics. We will use this submodule to find the R2 Score. First, we need to import the R2 Score from the Sklearn module.
Before it, don’t forget to install the Sklearn Module on your System.
# importing the sklearn module from sklearn.metrics import r2_score
Now, we can call the function and provide the actual and the predicted outputs:
# r2 score print(r2_score(actual, predictions))
As you can see, it is very simple to get the R2 score in Python.
R2_score in Python is the score that helps us to find the performance of a regression model. The interpretation of the R2 score is very simple as it returns values from 0 to 1. A simple way to understand it is that if you are getting the R2 score of 0.78, it means your model is 78% accurate. In this post, we discussed what is the R2 score and how we can calculate it in Python using the Sklearn module.