Calculate the Mean Absolute Error (MAE) between predicted values and actual values. MAE measures the average magnitude of errors in a set of predictions without considering their direction.
def mean_absolute_error(y_true: list[float], y_pred: list[float]) -> float:
n = len(y_true)
if n == 0:
return 0.0
mae = sum(abs(t - p) for t, p in zip(y_true, y_pred)) / n
return round(mae, 4)|y_true_i - y_pred_i|.MAE is robust to outliers compared to MSE since it does not square the errors. A MAE of 0 means perfect predictions.