Skip to content

[Machine Learning] Introduction To MAE Metric (With Example)

Last Updated on 2021-11-24 by Clay

Mean Absolute Error (MAE) is a standard and famous evaluation metric, it usually appears in the first class or the Nth class of the machine learning course… it depends on how your teacher arranges the textbook. But to me, it’s like Mario’s first mushroom monster after starting level 1-1.

As the name implies, the Mean Absolute Error metric is to subtract our predicted value and actual value at each time point to obtain the absolute value, and then average it out.

If the description is not clear, the formula is as follows:

Where n is the total number of time points we calculate, that is, the total number of predicted values, and the total number of actual values.

Since it is an error value, the larger the score, the worse the effect of our prediction.

In machine learning tasks, MAE metric is used for regression problems rather than classification problems.

In terms of indicator characteristics, MAE adopts absolute values rather than squares (refer to mean square error (MSE)), so the more extreme errors are not emphasized, but the squared MSE will.

But because of this, assuming that we are very concerned about the wrong predictions of the model today, then MAE is not an appropriate indicator. Just like with candidates, we assume that students who fail 59 on the test and those who fail 9 on the test need to receive the same intensity of after-school tutoring-obviously this is not necessary!

Generally speaking, we still need to choose MAE or MSE according to different tasks.

Generally speaking, we still need to choose MAE or MSE according to different tasks.


Python Program

The following method used numpy package.

# coding: utf-8
import numpy as np


def MAE(preds, targets):
    preds = np.array(preds)
    targets = np.array(targets)
    return np.sum(np.abs(preds-targets)) / targets.size


def main():
    preds = [1.1, 3.4, 9.7, 4.5]
    targets = [1.3, 3.7, 8.9, 4.3]
    print("MAE: {:.4}".format(MAE(preds, targets)))


if __name__ == "__main__":
    main()



Output:

MAE: 0.375



(Additional) PyTorch version

Of course you need to have torch in your environment.

# coding: utf-8
import torch


def MAE(preds, targets):
    preds = torch.tensor(preds)
    targets = torch.tensor(targets)
    return torch.abs(preds-targets).sum() / len(targets)


def MAE_built_in(preds, targets):
    preds = torch.tensor(preds)
    targets = torch.tensor(targets)
    return torch.nn.L1Loss()(preds, targets)


def main():
    preds = [1.1, 3.4, 9.7, 4.5]
    targets = [1.3, 3.7, 8.9, 4.3]
    print("MAE: {:.4}".format(MAE(preds, targets)))
    print("MAE: {:.4}".format(MAE_built_in(preds, targets)))


if __name__ == "__main__":
    main()



Output:

MAE: 0.375
MAE: 0.375


You will see the same score.


References


Read More

Leave a Reply