Know The Best Evaluation Metrics for Your Regression Model !

Raghav Agrawal 11 Jun, 2024
7 min read

Introduction

Evaluation Metrics for regression are essential for assessing the performance of regression models specifically. These metrics help in measuring how well a regression model is able to predict continuous outcomes. Common regression evaluation metrics for regression include Mean Absolute Error (MAE), Mean Squared Error (MSE), Root Mean Squared Error (RMSE), R-squared (Coefficient of Determination), and Mean Absolute Percentage Error (MAPE). By utilizing these regression-specific metrics, data scientists and machine learning engineers can evaluate the accuracy and effectiveness of their metrics for regression models in making predictions.

Regression is also one type of supervised Machine learning and in this tutorial, we will discuss various metrics for evaluating regression Models and How to implement them using the sci-kit-learn library.

Learning Objectives:

  • Understand the importance of evaluation metrics in assessing regression model performance
  • Learn about various regression evaluation metrics like MAE, MSE, RMSE, R-squared, etc.
  • Gain knowledge on implementing these metrics using Python’s scikit-learn library

This article was published as a part of the Data Science Blogathon 

Regression

Regression is a type of Machine learning which helps in finding the relationship between independent and dependent variables.

In simple words, Regression can be defined as a Machine learning problem where we have to predict continuous values like price, Rating, Fees, etc.

Why We Require Evaluation Metrics?

Most beginners and practitioners most of the time do not bother about the model performance. The talk is about building a well-generalized model, Machine learning model cannot have 100 per cent efficiency otherwise the model is known as a biased model. which further includes the concept of overfitting and underfitting.

It is necessary to obtain the accuracy on training data, But it is also important to get a genuine and approximate result on unseen data otherwise Model is of no use.

So to build and deploy a generalized model we require to Evaluate the model on different metrics which helps us to better optimize the performance, fine-tune it, and obtain a better result.

If one metric is perfect, there is no need for multiple metrics. To understand the benefits and disadvantages of regression evaluation metrics for regression because different evaluation metric fits on a different set of a dataset.

Now, I hope you get the importance of Evaluation metrics. let’s start understanding various regression evaluation metrics used for regression tasks.

Dataset

For demonstrating each evaluation metric using the sci-kit-learn library we will use the placement dataset which is a simple linear dataset that looks something like this.

dataset

Now I am applying linear regression on the particular dataset and after that, we will study each evaluation metric and check it on our Linear Regression model.

let’s start Exploring various evaluation metrics for regression.

Mean Absolute Error(MAE)

MAE is a very simple metric which calculates the absolute difference between actual and predicted values.

To better understand, let’s take an example you have input data and output data and use Linear Regression, which draws a best-fit line.

Now you have to find the MAE of your model which is basically a mistake made by the model known as an error. Now find the difference between the actual value and predicted value that is an absolute error but we have to find the mean absolute of the complete dataset.

so, sum all the errors and divide them by a total number of observations And this is MAE. And we aim to get a minimum MAE because this is a loss.

evaluation metrics for regression mae

Advantages of MAE

  • The MAE you get is in the same unit as the output variable.
  • It is most Robust to outliers.

Disadvantages of MAE

  • The graph of MAE is not differentiable so we have to apply various optimizers like Gradient descent which can be differentiable.
from sklearn.metrics import mean_absolute_error
print("MAE",mean_absolute_error(y_test,y_pred))

Now to overcome the disadvantage of MAE next metric came as MSE.

Mean Squared Error(MSE)

MSE is a most used and very simple metric with a little bit of change in mean absolute error. Mean squared error states that finding the squared difference between actual and predicted value.

So, above we are finding the absolute difference and here we are finding the squared difference.

What actually the MSE represents? It represents the squared distance between actual and predicted values. we perform squared to avoid the cancellation of negative terms and it is the benefit of MSE.

MSE(Mean squared error) evaluation metrics for regression

Advantages of MSE

The graph of MSE is differentiable, so you can easily use it as a loss function.

Disadvantages of MSE

  • The value you get after calculating MSE is a squared unit of output. for example, the output variable is in meter(m) then after calculating MSE the output we get is in meter squared.
  • If you have outliers in the dataset then it penalizes the outliers most and the calculated MSE is bigger. So, in short, It is not Robust to outliers which were an advantage in MAE.
from sklearn.metrics import mean_squared_error
print("MSE",mean_squared_error(y_test,y_pred))

Root Mean Squared Error(RMSE)

As RMSE is clear by the name itself, that it is a simple square root of mean squared error.

evaluation metrics for regression | Rmse

Advantages of RMSE

  •  The output value you get is in the same unit as the required output variable which makes interpretation of loss easy.

Disadvantages of RMSE

  • It is not that robust to outliers as compared to MAE.

for performing RMSE we have to NumPy NumPy square root function over MSE.

print("RMSE",np.sqrt(mean_squared_error(y_test,y_pred)))

Most of the time people use RMSE as an evaluation metric and mostly when you are working with deep learning techniques the most preferred metric is RMSE.

Root Mean Squared Log Error(RMSLE)

Taking the log of the RMSE metric slows down the scale of error. The metric is very helpful when you are developing a model without calling the inputs. In that case, the output will vary on a large scale.

To control this situation of RMSE we take the log of calculated RMSE error and resultant we get as RMSLE.

To perform RMSLE we have to use the NumPy log function over RMSE.

print("RMSE",np.log(np.sqrt(mean_squared_error(y_test,y_pred))))

It is a very simple metric that is used by most of the datasets hosted for Machine Learning competitions.

R Squared (R2)

R2 score is a metric that tells the performance of your model, not the loss in an absolute sense that how many wells did your model perform.

In contrast, MAE and MSE depend on the context as we have seen whereas the R2 score is independent of context.

So, with help of R squared we have a baseline model to compare a model which none of the other metrics provides. The same we have in classification problems which we call a threshold which is fixed at 0.5. So basically R2 squared calculates how must regression line is better than a mean line.

Hence, R2 squared is also known as Coefficient of Determination or sometimes also known as Goodness of fit.

r2 evaluation metrics for regression
R2 Squared

Now, how will you interpret the R2 score? suppose If the R2 score is zero then the above regression line by mean line is equal means 1 so 1-1 is zero. So, in this case, both lines are overlapping means model performance is worst, It is not capable to take advantage of the output column.

Now the second case is when the R2 score is 1, it means when the division term is zero and it will happen when the regression line does not make any mistake, it is perfect. In the real world, it is not possible.

So we can conclude that as our regression line moves towards perfection, R2 score move towards one. And the model performance improves.

The normal case is when the R2 score is between zero and one like 0.8 which means your model is capable to explain 80 per cent of the variance of data.

from sklearn.metrics import r2_score
r2 = r2_score(y_test,y_pred)
print(r2)

Adjusted R Squared

The disadvantage of the R2 score is while adding new features in data the R2 score starts increasing or remains constant but it never decreases because It assumes that while adding more data variance of data increases.

But the problem is when we add an irrelevant feature in the dataset then at that time R2 sometimes starts increasing which is incorrect.

Hence, To control this situation Adjusted R Squared came into existence.

r2a

Now as K increases by adding some features so the denominator will decrease, n-1 will remain constant. R2 score will remain constant or will increase slightly so the complete answer will increase and when we subtract this from one then the resultant score will decrease. so this is the case when we add an irrelevant feature in the dataset.

And if we add a relevant feature then the R2 score will increase and 1-R2 will decrease heavily and the denominator will also decrease so the complete term decreases, and on subtracting from one the score increases.

n=40
k=2
adj_r2_score = 1 - ((1-r2)*(n-1)/(n-k-1))
print(adj_r2_score)

Hence, this metric becomes one of the most important metrics to use during the evaluation of the model.

Conclusion

Evaluating metrics for regression models using appropriate metrics is crucial for assessing their performance and making informed decisions. By understanding and utilizing metrics like MAE, MSE, RMSE, R-squared, and others, data scientists can quantify the accuracy, goodness of fit, and overall effectiveness of their models. Ultimately, these regression evaluation metrics serve as valuable tools for model selection, optimization, and deployment in real-world regression problems.

Key Takeaways:

  • Evaluation metrics quantify how well a regression model performs on unseen data
  • Different metrics capture different aspects of model performance (error, variance explained, etc.)
  • Interpreting multiple metrics provides a comprehensive understanding of a model’s strengths and limitations

The media shown in this article are not owned by Analytics Vidhya and is used at the Author’s discretion.

Frequently Asked Questions

Q1. What is the evaluation metric for regression?

A. The evaluation metric for regression includes Mean Squared Error (MSE), Root Mean Squared Error (RMSE), Mean Absolute Error (MAE), and R-squared (R²).

Q2. What are the performance metrics for regression classification?

A. Performance metrics for regression classification are MSE, RMSE, MAE, R², and Mean Absolute Percentage Error (MAPE).

Q3. What is the R2 metric of regression?

A. The R² metric, or coefficient of determination, measures the proportion of variance in the dependent variable predictable from the independent variables.

Q4. What is a good RMSE value for regression?

A. A good RMSE value for regression depends on the context, but generally, lower values indicate better model performance. It should be compared to the scale of the data.

Raghav Agrawal 11 Jun, 2024

I am a final year undergraduate who loves to learn and write about technology. I am a passionate learner, and a data science enthusiast. I am learning and working in data science field from past 2 years, and aspire to grow as Big data architect.

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers

Clear

Geoffrey
Geoffrey 09 Aug, 2021

Thank you for the article, interesting especially if the importance of metrics is overshadowed. Just 2 or 3 things : 1/ You said " Regression can be defined as a Machine learning problem where we have to predict discrete values like price, Rating, Fees, etc." It is not discrete, it is continuous values. 2/ I did not fully understand the very last part about adjusted R squared. "It assumes that while adding more data variance of data increases" is that always the case and if so, why ? I would have said that if you add many datapoints with the same "y-value / target-value" the variance will on the contrary decrease ? 3/ I thought that having (linearly ?) dependant features was bad in any case, but at the beginning of the article you seem to say that linear regression is OK with dependant and independant features. Is that the case ? Linked to my question 2, having a redundant feature isn't it almost the same as adding an irrelevant feature or at least it can artificially increase the R2 score while the information used is redundant and we did not really increased the performance of our model Thank you very much ! Geoffrey

Anjana
Anjana 09 Oct, 2023

Hey hi. "In simple words, Regression can be defined as a Machine learning problem where we have to predict discrete values like price, Rating, Fees, etc." Shouldnt it be "continous values"?