Simply Explained with Python: Normalized Mean Absolute Error (NMAE)


Normalized Mean Absolute Error (NMAE) is a statistical measure used to assess the accuracy of a prediction model. It’s a variation of the Mean Absolute Error (MAE), which is a common measure for regression models. Here’s how each term breaks down:

Introduction to Mean Absolute Error (MAE)

This is the average of the absolute errors between the predicted values and the actual values. The absolute error is the absolute difference between the predicted value and the actual value for each data point. Mathematically, it’s expressed as:

tex_6318fddbba23309cb661dbbbf1b2e8a7 Simply Explained with Python: Normalized Mean Absolute Error (NMAE) introduction math programming languages python Python Statistics

where tex_ddf2ca8e43b4945736229af23cbc6996 Simply Explained with Python: Normalized Mean Absolute Error (NMAE) introduction math programming languages python Python Statistics is the actual value, tex_7e10038af1e23c9d2e3a1ff4ff1ecf9c Simply Explained with Python: Normalized Mean Absolute Error (NMAE) introduction math programming languages python Python Statistics is the predicted value and n is the number of observations.

Introduction to Normalized Mean Absolute Error (NMAE)

This takes the MAE and normalizes it, usually by the mean or range of the observed data values. The purpose of normalization is to bring the error metric to a scale that is easier to interpret and compare across different datasets or models. The normalization can be done in several ways, but a common approach is:

tex_f247d4fc907ab5d32bfd3b94f0155ce4 Simply Explained with Python: Normalized Mean Absolute Error (NMAE) introduction math programming languages python Python Statistics

mean(y) is the mean of the actual values. This makes the NMAE a relative measure, expressing the average error as a percentage of the average observed value.

The NMAE is particularly useful when comparing the performance of models across datasets with different scales or units, as it provides a unitless and scale-independent metric. It’s important to remember, however, that like all error metrics, NMAE should be interpreted in the context of the specific problem and data.

Compute Normalized Mean Absolute Error (NMAE) in Python

The Python implementation of the Normalized Mean Absolute Error (NMAE) is shown below which uses an example dataset. In this example, the NMAE is approximately 0.174. This value represents the average error between the predicted and actual values, normalized by the mean of the actual values. It provides a relative measure of the prediction accuracy in a scale-independent way. ​

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from sklearn.metrics import mean_absolute_error
import numpy as np
 
def normalized_mean_absolute_error(y_true, y_pred):
    """
    Calculate the Normalized Mean Absolute Error (NMAE).
 
    Parameters:
    y_true (array-like): True values.
    y_pred (array-like): Predicted values.
 
    Returns:
    float: The NMAE value.
    """
    mae = mean_absolute_error(y_true, y_pred)
    return mae / np.mean(y_true)
 
# Example usage
y_true = np.array([3, -0.5, 2, 7])
y_pred = np.array([2.5, 0.0, 2, 8])
 
nmae = normalized_mean_absolute_error(y_true, y_pred)
print(nmae)
from sklearn.metrics import mean_absolute_error
import numpy as np

def normalized_mean_absolute_error(y_true, y_pred):
    """
    Calculate the Normalized Mean Absolute Error (NMAE).

    Parameters:
    y_true (array-like): True values.
    y_pred (array-like): Predicted values.

    Returns:
    float: The NMAE value.
    """
    mae = mean_absolute_error(y_true, y_pred)
    return mae / np.mean(y_true)

# Example usage
y_true = np.array([3, -0.5, 2, 7])
y_pred = np.array([2.5, 0.0, 2, 8])

nmae = normalized_mean_absolute_error(y_true, y_pred)
print(nmae)

To calculate the Normalized Mean Absolute Error (NMAE) in pure Python without using any external libraries like NumPy or scikit-learn, you can follow these steps:

  • Calculate the Mean Absolute Error (MAE): This involves finding the absolute difference between each predicted and actual value, summing these differences, and then dividing by the number of data points.
  • Normalize the MAE: This can be done by dividing the MAE by the mean of the actual values.

Here’s how you can implement it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def mean_absolute_error(y_true, y_pred):
    """ Calculate Mean Absolute Error """
    total_error = 0
    for actual, predicted in zip(y_true, y_pred):
        total_error += abs(actual - predicted)
    return total_error / len(y_true)
 
def normalized_mean_absolute_error(y_true, y_pred):
    """ Calculate Normalized Mean Absolute Error """
    mae = mean_absolute_error(y_true, y_pred)
    mean_actual = sum(y_true) / len(y_true)
    return mae / mean_actual
 
# Example usage
y_true = [3, -0.5, 2, 7]
y_pred = [2.5, 0.0, 2, 8]
 
nmae = normalized_mean_absolute_error(y_true, y_pred)
print(nmae)
def mean_absolute_error(y_true, y_pred):
    """ Calculate Mean Absolute Error """
    total_error = 0
    for actual, predicted in zip(y_true, y_pred):
        total_error += abs(actual - predicted)
    return total_error / len(y_true)

def normalized_mean_absolute_error(y_true, y_pred):
    """ Calculate Normalized Mean Absolute Error """
    mae = mean_absolute_error(y_true, y_pred)
    mean_actual = sum(y_true) / len(y_true)
    return mae / mean_actual

# Example usage
y_true = [3, -0.5, 2, 7]
y_pred = [2.5, 0.0, 2, 8]

nmae = normalized_mean_absolute_error(y_true, y_pred)
print(nmae)

This code defines two functions, mean_absolute_error for calculating MAE and normalized_mean_absolute_error for calculating NMAE. You can test this with any set of actual (y_true) and predicted (y_pred) values.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
851 words
Last Post: Stock Prices of Google and Microsoft Before and After Gemini
Next Post: Teaching Kids Programming - Count Unique Length-3 Palindromic Subsequences

The Permanent URL is: Simply Explained with Python: Normalized Mean Absolute Error (NMAE)

Leave a Reply