I N F O A R Y A N

Performance metrics in Regression Models - Explanation with Code

In the vast landscape of regression models, assessing performance is a critical step to ensure the model’s effectiveness in making predictions. Various metrics exist to gauge the accuracy and reliability of regression models. In this blog post, we’ll explore several essential metrics, unraveling their mathematical foundations and demonstrating their implementation using Python. We will use various libraries for implementing these errors such as numpy, scikit-learn library.

You may also want to explore Linear Regression, Transfer Learning using Regression, or Validation Techniques.

 

1. Mean Absolute Error (MAE)

Explanation:

Mean Absolute Error is a straightforward metric measuring the average absolute difference between predicted and actual values. It provides a clear picture of how far off the predictions are from the actual values.

Formula:

Use Case: Consider a weather forecasting model predicting daily temperatures. MAE could be employed to measure the average absolute difference between the predicted and actual temperatures. This metric provides insights into the model’s accuracy in predicting temperature variations.

Python Code: 

from sklearn.metrics import mean_absolute_error

mae = mean_absolute_error(y_true, y_pred)
print(f’Mean Absolute Error: {mae}’)

2. Mean Squared Error (MSE)

Explanation:

Mean Squared Error squares the differences between predicted and actual values, emphasizing larger errors. It is widely used but sensitive to outliers.

Formula:

Use Case: In financial forecasting, such as predicting stock prices, MSE could be utilized to evaluate the average squared difference between predicted and actual stock values. MSE gives more weight to larger errors, which is beneficial when extreme predictions have significant consequences.

Python Code: 

from sklearn.metrics import mean_squared_error

mse = mean_squared_error(y_true, y_pred)
print(f’Mean Squared Error: {mse}’)

3. Root Mean Squared Error (RMSE):

Explanation:

Root Mean Squared Error is a variant of MSE, offering an interpretable metric by taking the square root. It provides a measure of the average magnitude of errors in the predicted values.

Formula:

RMSE = Square_root( MSE )

Use Case: Suppose you are developing a housing price prediction model. RMSE would be valuable in assessing how well the model predicts actual housing prices. It provides a measure of the typical size of prediction errors, offering a more interpretable metric for stakeholders.

Python Code: 

import numpy as np

rmse = np.sqrt(mse)
print(f’Root Mean Squared Error: {rmse}’)

4. R-squared (R2)

Explanation:

R-squared represents the proportion of the variance in the dependent variable that is predictable from the independent variables. It ranges from 0 to 1, with higher values indicating a better fit.

Formula: 

Use Case: In marketing analytics, R-squared can be applied to assess the effectiveness of a sales prediction model. For instance, a model predicting monthly sales based on advertising expenditure and market trends. R2 indicates the proportion of variability in sales that can be explained by the model.

Python Code: 

from sklearn.metrics import r2_score

r2 = r2_score(y_true, y_pred)
print(f’R-squared: {r2}’)

5. Adjusted R-squared

Explanation:

Adjusted R-squared accounts for the number of predictors in the model, providing a more accurate measure when dealing with multiple features.

Formula: 

Use Case: Imagine you are working on a project to predict student performance based on various factors like study time, attendance, and socioeconomic background. Adjusted R-squared would be beneficial here, especially when dealing with multiple predictors, helping you gauge the model’s fit more accurately.

Python Code: 

n = len(y_true)
k = number_of_predictors # Replace with the actual number of predictors

adjusted_r2 = 1 – ((1 – r2) * (n – 1) / (n – k – 1))
print(f’Adjusted R-squared: {adjusted_r2}’)

6. Mean Absolute Percentage Error (MAPE)

Explanation:

Mean Absolute Percentage Error calculates the average percentage difference between predicted and actual values, providing a percentage representation of the error.

Formula: 

Use Case: In the energy sector, forecasting electricity consumption is crucial. MAPE can be employed to evaluate how well a model predicts energy consumption, providing a percentage representation of the average error. This metric is valuable for understanding the relative magnitude of errors in percentage terms.

Python Code: 

mape = np.mean(np.abs((y_true – y_pred) / y_true)) * 100
print(f’Mean Absolute Percentage Error: {mape}’)

These real-world use cases illustrate how each metric serves a specific purpose in evaluating different aspects of regression model performance. The choice of metric depends on the nature of the data, the goals of the modeling task, and the preferences of stakeholders. It’s essential to select metrics that align with the objectives of the analysis and provide meaningful insights for decision-making in the given context.