Mean Squared Error (MSE)

One of the most often used loss functions in linear regression is the Mean Squared Error (MSE). The average of the squared difference between the real values and the forecasted values is how it is computed:

[Tex]MSE = (1/n) * Σ(y_{pred}- y_{true})^2[/Tex]

where,

  • y_pred is the projected value
  • y_true is the true value
  • n is the number of data points

Because of the squaring process, the MSE penalizes greater mistakes more severely than smaller ones. Because outliers have the potential to greatly raise the MSE, this makes it susceptible to them. But the MSE is differentiable, which is a desired characteristic for machine learning optimization techniques.

Computing Mean Squared Error in Python

Python

import numpy as np def mse(y_true, y_pred): y_true = np.array(y_true) y_pred = np.array(y_pred) return np.mean((y_true - y_pred) ** 2) # Example usage y_true = [3, 6, 8, 12] y_pred = [4, 5, 7, 10] print("Mean Squared Error:",mse(y_true, y_pred))

Output:

Mean Squared Error: 1.75

Computing Mean Squared Error using Sklearn Library

Python

from sklearn.metrics import mean_squared_error # Example usage y_true = [3, 6, 8, 12] y_pred = [4, 5, 7, 10] print("Mean Squared Error:", mean_squared_error(y_true, y_pred))

Output:

Mean Squared Error: 1.75

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

Similar Reads

Mean Squared Error (MSE)

One of the most often used loss functions in linear regression is the Mean Squared Error (MSE). The average of the squared difference between the real values and the forecasted values is how it is computed:...

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:...

Huber Loss

The MSE and the MAE are combined to get the Huber Loss. It is intended to maintain differentiation but be less susceptible to outliers than the MSE:...

Comparison of Loss Functions for Linear Regression

In this section, we compare different loss functions commonly used in regression tasks: Mean Squared Error (MSE), Mean Absolute Error (MAE), and Huber Loss....

FAQs on Loss Functions for Linear Regression

Why are loss functions required in linear regression calculations?...

Contact Us