# Calculating the Mean Absolute Error

## Prerequisites

### Calculating the Mean Absolute Error

#### Definition

• The mean absolute error (mae) is a metric for determining the similarity between two sets.
• The error between two numbers is simply the difference between them.
• The absolute error is the absolute difference.
• To find the mean absolute error:
• Find the absolute error between corresponding values in the sets
• Then find the mean of those errors.

#### Example

• Find the mean absolute error of the following two sets of numbers:
S1 = [2, 5, 9, 2]
S2 = [6, 3, 6, 1]
1. First we calculate the differences between these numbers:
D = [2 - 6, 5 - 3, 9 - 6, 2 - 1]
D = [-4, 2, 3, 1]
2. Now we must make these numbers absolute:
D = [4, 2, 3, 1]
3. Finally, we find the mean of these numbers:
mae = (4 + 2 + 3 + 1) / 4
mae = 10 / 4
mae = 2.5

#### Mathematical Definition

LaTeX formula:mae = (\frac{1}{n})\sum_{i=1}^{n}\left | y_{i} - x_{i} \right |

#### Code (Python)

import sklearn.metrics

S1 = [2, 5, 9, 2]
S2 = [6, 3, 6, 1]

mae = sklearn.metrics.mean_absolute_error(S1, S2)
print(mae)