Mean Absolute Error (MAE)
For linear regression, another often-used loss function is the Mean Absolute Error (MAE). The average of the absolute differences between the real values and the forecasted values is used to compute it:
[Tex]MAE = (1/n) * Σ|y_{pred} – y_{true}|[/Tex]
Since the MAE does not square the errors, it is less susceptible to outliers than the MSE. MAE handles all mistakes the same way, no matter how big. However, certain optimization techniques may encounter difficulties since the MAE is not differentiable at zero.
Computing Mean Absolute Error in Python
import numpy as np
def mae(y_true, y_pred):
y_true = np.array(y_true)
y_pred = np.array(y_pred)
return np.mean(np.abs(y_true - y_pred))
# Example usage
y_true = [3, 6, 8, 12]
y_pred = [4, 5, 7, 10]
print("Mean Absolute Error:',mae(y_true, y_pred))
Output:
Mean Absolute Error: 1.25
Computing Mean Absolute Error using Sklearn
from sklearn.metrics import mean_absolute_error
# Example usage
y_true = [3, 6, 8, 12]
y_pred = [4, 5, 7, 10]
print("Mean Absolute Error:",mean_absolute_error(y_true, y_pred))
Output:
Mean Absolute Error: 1.25
Loss function for Linear regression in Machine Learning
The loss function quantifies the disparity between the prediction value and the actual value. In the case of linear regression, the aim is to fit a linear equation to the observed data, the loss function evaluate the difference between the predicted value and true values. By minimizing this difference, the model strives to find the best-fitting line that captures the relationship between the input features and the target variable.
In this article, we will discuss Mean Squared Error (MSE) , Mean Absolute Error (MAE) and Huber Loss.
Table of Content
- Mean Squared Error (MSE)
- Computing Mean Squared Error in Python
- Computing Mean Squared Error using Sklearn Library
- Mean Absolute Error (MAE)
- Computing Mean Absolute Error in Python
- Computing Mean Absolute Error using Sklearn
- Huber Loss
- Computing Huber Loss in Python
- Comparison of Loss Functions for Linear Regression
- FAQs on Loss Functions for Linear Regression