Mean Squared Error (MSE)

Mean Squared Error (MSE) is a common error function used in regression problems.

#Getting Started

#Introduction

  • It is also known as the L2 loss function.
  • It is the average of the squared difference between the predicted and actual values.
  • It is used to measure how close the predicted values are to the actual values.
  • It is used in regression problems.
  • https://en.wikipedia.org/wiki/Mean_squared_error Wikipedia

#algorithms using MSE

  • Linear Regression
  • Logistic Regression
  • Support Vector Machines (SVMs)
  • Neural Networks
  • Generalized Linear Models (GLMs)
  • Gradient Boosting Machines (GBMs)

#formula

  • is the actual value.
  • is the predicted value.
  • is the number of samples.

#Example

  • Let's say we have a dataset of 5 samples.
  • The actual values are .
  • The predicted values are .
  • The MSE is calculated as follows:

#Properties

  • It is always non-negative.
  • It is 0 if and only if the predicted and actual values are equal.
  • It is not robust to outliers.
  • It is differentiable.
  • It is convex.
  • It is continuous.
  • It is symmetric.
  • It is scale-dependent.
  • It is not robust to outliers.

#Advantages & Disadvantages

  • Advantages
    • It is easy to implement.
    • It is easy to interpret.
    • It is differentiable.
    • It is convex.
    • It is continuous.
    • It is symmetric.
    • It is scale-dependent.
  • Disadvantages
    • It is not robust to outliers.

#Implementation

#Python Implementation with NumPy

def mse(y_true, y_pred):
    return np.mean((y_true - y_pred)**2)

#Python Implementation with scikit-learn

from sklearn.metrics import mean_squared_error

mse = mean_squared_error(y_true, y_pred)

#Python Implementation with TensorFlow


import tensorflow as tf

mse = tf.keras.losses.MeanSquaredError()
mse(y_true, y_pred).numpy()

#Python Implementation with PyTorch

import torch.nn as nn

mse = nn.MSELoss()
mse(y_true, y_pred).item()

#Python Implementation with MXNet

import mxnet as mx

mse = mx.metric.MSE()
mse.update(mx.nd.array(y_true), mx.nd.array(y_pred))
mse.get()

#Python Implementation with Apache Spark

from pyspark.ml.evaluation import RegressionEvaluator

evaluator = RegressionEvaluator(labelCol="label", predictionCol="prediction", metricName="mse")
mse = evaluator.evaluate(predictions)