Adventures in Machine Learning

Calculating Mean Absolute Error in Python for Accurate Predictive Modeling

Mean Absolute Error (MAE) is a popular metric that is used to evaluate the accuracy of models in several fields, including data science, statistics, economics, and machine learning. In this article, we will discuss the concept of MAE along with its formula and calculation in Python.

Understanding Mean Absolute Error:

MAE is a measure of the difference between the actual values and predicted values of a set of observations. In other words, it determines the average absolute difference between the observed values and the predicted values.

The smaller the MAE, the better the accuracy of the model.

Calculation of Mean Absolute Error using formula:

The formula for MAE averages the absolute differences between the predicted values and the observed values.

It can be expressed mathematically as:

MAE = (1/n) * |Yi – Xi|

Where, n is the number of observations, Yi is the predicted value and Xi is the actual value. Example:

Consider a scenario where a model predicts the sales of a store, but the actual values are slightly different from the predicted values.

The following table shows the data:

Actual Values: 100, 120, 95, 110, 105

Predicted Values: 102, 115, 93, 108, 99

To calculate the MAE, we first need to compute the absolute values of the differences between the predicted values and actual values. The results are as follows:

|102 – 100| = 2

|115 – 120| = 5

|93 – 95| = 2

|108 – 110| = 2

|99 – 105| = 6

Next, we add the absolute differences and divide the sum by the number of observations to obtain the MAE:

MAE = (2 + 5 + 2 + 2 + 6) / 5

MAE = 2.8

Therefore, the MAE for this model is 2.8, indicating that the prediction is off by an average of 2.8 units.

Example: Calculating Mean Absolute Error in Python

Python is a popular language for data analytics and machine learning, due to its powerful libraries and easy-to-understand syntax. Let’s dive into an example of calculating MAE in Python using Scikit-learn.

Suppose we have the following Python arrays for actual and predicted values:

Actual = [10, 20, 30, 40, 50]

Predicted = [12, 22, 32, 42, 52]

To calculate the MAE, we can use the Scikit-learn library, which provides a function called mean_absolute_error(). The function takes two arguments: the actual values and the predicted values.

To use this function, we need to import the library as follows:

from sklearn.metrics import mean_absolute_error

After importing the library, we can call the mean_absolute_error() function as follows:

MAE = mean_absolute_error(Actual, Predicted)

print(“The Mean Absolute Error is”, MAE)

Output:

The Mean Absolute Error is 2.0

The above code will return the MAE value of 2.0, which indicates that the prediction is off by an average of 2.0 units. Conclusion:

In conclusion, MAE is a crucial metric to evaluate the accuracy of predictive models.

It determines the average absolute difference between the observed values and the predicted values. A lower MAE value indicates better accuracy of the model.

In Python, we can easily calculate MAE using the Scikit-learn library. It is a useful tool for evaluating predictive models, and it can help us improve the accuracy of our predictions.

Interpretation of Mean Absolute Error:

When we calculate Mean Absolute Error (MAE), we obtain a value which represents the average absolute difference between the actual values and the predicted values. However, it is essential to understand the interpretation of this value to determine the accuracy of the predictive model.

Explanation of Mean Absolute Error value obtained:

The MAE value ranges from 0 to positive infinity, where a lower MAE value indicates a better performance of the model. If the MAE value is zero, it indicates that the model’s predictions are an exact match to the actual values, which is the ideal scenario.

In contrast, a larger MAE value indicates a greater discrepancy between the predicted and actual values, which indicates that the model’s performance is poor. For instance, suppose we have two models A and B with their MAE values calculated as 5 and 7, respectively.

In this case, we can infer that model A is performing better than model B as it has a lower MAE value, indicating that the absolute differences between the predicted and actual values are lesser in model A. Thus, we can conclude that the model with a lower MAE value is a better fit for the data.

Comparison of MAE for different models:

The comparison of the MAE values obtained for different models is a useful technique to access their comparative performance. When we compare the MAE values for different models, we should choose the model with lower MAE values, as it has better accuracy in making predictions.

Suppose we have a dataset consisting of 1000 observations, and we use various models to predict values. We calculate the MAE values for each model, as shown below:

Model A: MAE = 1.5

Model B: MAE = 2.3

Model C: MAE = 1.2

Model D: MAE = 3.8

Model E: MAE = 1.8

In this case, we can see that Model C has the lowest MAE value, which indicates that it has a better predictive performance than other models.

Therefore, we can choose model C as the best-fit model for this dataset. Furthermore, comparing MAE values of different models can help us identify the reason for the differences in the accuracy of these models.

Suppose, when comparing the MAE values of two models, Model A and Model B, we find that Model A has a lower MAE value. In that case, we can conclude that Model A outperforms Model B in terms of better prediction accuracy.

However, when we compare the MAE values of two models, we must consider other factors such as the sample size, data quality, and model complexity, which could influence the MAE values. These factors should be taken into account while selecting the best-fit model for making predictions.

In conclusion, MAE is a powerful metric for evaluating the predictive accuracy of models. It provides insights into the performance of different models by measuring the absolute difference between the actual and predicted values.

A lower MAE value indicates better accuracy, while a higher MAE value suggests that the model’s performance is poor. By comparing the MAE values of different models, we can identify the model with better predictive performance and choose it as the best-fit model for making the predictions.

In conclusion, Mean Absolute Error (MAE) is a crucial metric for evaluating the predictive accuracy of models. MAE measures the absolute difference between the actual and predicted values and is expressed as a single numerical value that represents the average absolute difference.

A lower MAE value indicates better accuracy, while a higher MAE value suggests that the model’s performance is poor. Additionally, comparing MAE values of different models is a useful technique to access their comparative performance and identify the best-fit model for making predictions.

By understanding the interpretation of the MAE value, we can evaluate the accuracy of predictive models and use this information to make better predictions. Therefore, measuring the MAE value is critical for data analysts and machine learning practitioners to create accurate models.

Popular Posts