Python средняя абсолютная ошибка

sklearn.metrics.mean_absolute_error(y_true, y_pred, *, sample_weight=None, multioutput=‘uniform_average’)[source]

Mean absolute error regression loss.

Read more in the User Guide.

Parameters:
y_truearray-like of shape (n_samples,) or (n_samples, n_outputs)

Ground truth (correct) target values.

y_predarray-like of shape (n_samples,) or (n_samples, n_outputs)

Estimated target values.

sample_weightarray-like of shape (n_samples,), default=None

Sample weights.

multioutput{‘raw_values’, ‘uniform_average’} or array-like of shape (n_outputs,), default=’uniform_average’

Defines aggregating of multiple output values.
Array-like value defines weights used to average errors.

‘raw_values’ :

Returns a full set of errors in case of multioutput input.

‘uniform_average’ :

Errors of all outputs are averaged with uniform weight.

Returns:
lossfloat or ndarray of floats

If multioutput is ‘raw_values’, then mean absolute error is returned
for each output separately.
If multioutput is ‘uniform_average’ or an ndarray of weights, then the
weighted average of all output errors is returned.

MAE output is non-negative floating point. The best value is 0.0.

Examples

>>> from sklearn.metrics import mean_absolute_error
>>> y_true = [3, -0.5, 2, 7]
>>> y_pred = [2.5, 0.0, 2, 8]
>>> mean_absolute_error(y_true, y_pred)
0.5
>>> y_true = [[0.5, 1], [-1, 1], [7, -6]]
>>> y_pred = [[0, 2], [-1, 2], [8, -5]]
>>> mean_absolute_error(y_true, y_pred)
0.75
>>> mean_absolute_error(y_true, y_pred, multioutput='raw_values')
array([0.5, 1. ])
>>> mean_absolute_error(y_true, y_pred, multioutput=[0.3, 0.7])
0.85...

Examples using sklearn.metrics.mean_absolute_error

  • Редакция Кодкампа


читать 1 мин


В статистике средняя абсолютная ошибка (MAE) — это способ измерения точности данной модели. Он рассчитывается как:

MAE = (1/n) * Σ|y i – x i |

куда:

  • Σ: греческий символ, означающий «сумма».
  • y i : Наблюдаемое значение для i -го наблюдения
  • x i : Прогнозируемое значение для i -го наблюдения
  • n: общее количество наблюдений

Мы можем легко вычислить среднюю абсолютную ошибку в Python, используя функцию mean_absolute_error() из Scikit-learn.

В этом руководстве представлен пример использования этой функции на практике.

Пример: вычисление средней абсолютной ошибки в Python

Предположим, у нас есть следующие массивы фактических значений и прогнозируемых значений в Python:

actual = [12, 13, 14, 15, 15, 22, 27]
pred = [11, 13, 14, 14, 15, 16, 18]

Следующий код показывает, как вычислить среднюю абсолютную ошибку для этой модели:

from sklearn. metrics import mean_absolute_error as mae

#calculate MAE
mae(actual, pred)

2.4285714285714284

Средняя абсолютная ошибка (MAE) оказывается равной 2,42857 .

Это говорит нам о том, что средняя разница между фактическим значением данных и значением, предсказанным моделью, составляет 2,42857.

Мы можем сравнить эту MAE с MAE, полученной с помощью других моделей прогнозирования, чтобы увидеть, какие модели работают лучше всего.

Чем ниже MAE для данной модели, тем точнее модель способна предсказать фактические значения.

Примечание.Массив фактических значений и массив прогнозируемых значений должны иметь одинаковую длину, чтобы эта функция работала правильно.

Дополнительные ресурсы

Как рассчитать MAPE в Python
Как рассчитать SMAPE в Python
Как рассчитать MSE в Python

Mean Absolute Error calculates the average difference between the calculated values and actual values. It is also known as scale-dependent accuracy as it calculates error in observations taken on the same scale. It is used as evaluation metrics for regression models in machine learning. It calculates errors between actual values and values predicted by the model. It is used to predict the accuracy of the machine learning model.

Formula:

Mean Absolute Error = (1/n) * ∑|yi – xi|

where,

  • Σ: Greek symbol for summation
  • yi: Actual value for the ith observation
  • xi: Calculated value for the ith observation
  • n: Total number of observations

Method 1: Using Actual Formulae

Mean Absolute Error (MAE) is calculated by taking the summation of the absolute difference between the actual and calculated values of each observation over the entire array and then dividing the sum obtained by the number of observations in the array.

Example:

Python3

actual = [2, 3, 5, 5, 9]

calculated = [3, 3, 8, 7, 6]

n = 5

sum = 0

for i in range(n):

    sum += abs(actual[i] - calculated[i])

error = sum/n

print("Mean absolute error : " + str(error))

Output

Mean absolute error : 1.8

Method 2: Using sklearn

sklearn.metrics module of python contains functions for calculating errors for different purposes. It provides a method named mean_absolute_error() to calculate the mean absolute error of the given arrays. 

Syntax:

mean_absolute_error(actual,calculated)

where

  • actual- Array of  actual values as first argument
  • calculated  – Array of predicted/calculated values as second argument

 It will return the mean absolute error of the given arrays.

Example:

Python3

from sklearn.metrics import mean_absolute_error as mae

actual = [2, 3, 5, 5, 9]

calculated = [3, 3, 8, 7, 6]

error = mae(actual, calculated)

print("Mean absolute error : " + str(error))

Output

Mean absolute error : 1.8

Last Updated :
28 Nov, 2021

Like Article

Save Article

Время на прочтение
4 мин

Количество просмотров 5.6K

Функции потерь Python являются важной частью моделей машинного обучения. Эти функции показывают, насколько сильно предсказанный моделью результат отличается от фактического.

Существует несколько способов вычислить эту разницу. В этом материале мы рассмотрим некоторые из наиболее распространенных функций потерь.

Ниже будут рассмотрены следующие четыре функции потерь.

  • Среднеквадратическая ошибка

  • Среднеквадратическая ошибка

  • Средняя абсолютная ошибка

  • Кросс-энтропийные потери

Из этих четырех функций потерь первые три применяются к модели классификации.

1. Среднеквадратическая ошибка (MSE)

Среднеквадратичная ошибка (MSE) рассчитывается как среднее значение квадратов разностей между прогнозируемыми и фактически наблюдаемыми значениями. Математически это можно выразить следующим образом:

Реализация MSE на языке Python выглядит следующим образом:

import numpy as np # импортируем библиотеку numpy
def mean_squared_error(act, pred): # функция 

   diff = pred - act # находим разницу между прогнозируемыми и наблюдаемыми значениями
   differences_squared = diff ** 2 # возводим в квадрат (чтобы избавиться от отрицательных значений)
   mean_diff = differences_squared.mean() # находим среднее значение
   
   return mean_diff

act = np.array([1.1,2,1.7]) # создаем список актуальных значений
pred = np.array([1,1.7,1.5]) # список прогнозируемых значений

print(mean_squared_error(act,pred)) 

Выход :

0.04666666666666667

Вы также можете использовать mean_squared_error из sklearn для расчета MSE. Вот как работает функция:

from sklearn.metrics import mean_squared_error
act = np.array([1.1,2,1.7])
pred = np.array([1,1.7,1.5])
mean_squared_error(act, pred)

Выход :

0.04666666666666667

2. Корень среднеквадратической ошибки (RMSE)

Итак, ранее, для того, чтобы найти действительную ошибку среди между прогнозируемыми и фактически наблюдаемыми значениями (там могли быть положительные и отрицательные значения), мы возводили их в квадрат (для того чтобы отрицательные значения участвовали в расчетах в полной мере). Это была среднеквадратичная ошибка (MSE).

Корень среднеквадратической ошибки (RMSE) мы используем для того чтобы избавиться от квадратной степени, в которую мы ранее возвели действительную ошибку среди между прогнозируемыми и фактически наблюдаемыми значениями. Математически мы можем представить это следующим образом:

Реализация Python для RMSE выглядит следующим образом:

import numpy as np
def root_mean_squared_error(act, pred):

   diff = pred - act # находим разницу между прогнозируемыми и наблюдаемыми значениями
   differences_squared = diff ** 2 # возводим в квадрат
   mean_diff = differences_squared.mean() # находим среднее значение
   rmse_val = np.sqrt(mean_diff) # извлекаем квадратный корень
   return rmse_val

act = np.array([1.1,2,1.7])
pred = np.array([1,1.7,1.5])

print(root_mean_squared_error(act,pred))

Выход :

0.21602468994692867

Вы также можете использовать mean_squared_error из sklearn для расчета RMSE. Давайте посмотрим, как реализовать RMSE, используя ту же функцию:

from sklearn.metrics import mean_squared_error
act = np.array([1.1,2,1.7])
pred = np.array([1,1.7,1.5])
mean_squared_error(act, pred, squared = False) #Если установлено значение False, функция возвращает значение RMSE.

Выход :

0.21602468994692867

Если для параметра squared установлено значение True, функция возвращает значение MSE. Если установлено значение False, функция возвращает значение RMSE.

3. Средняя абсолютная ошибка (MAE)

Средняя абсолютная ошибка (MAE) рассчитывается как среднее значение абсолютной разницы между прогнозами и фактическими наблюдениями. Математически мы можем представить это следующим образом:

Реализация Python для MAE выглядит следующим образом:

import numpy as np 
def mean_absolute_error(act, pred): #
    diff = pred - act # находим разницу между прогнозируемыми и наблюдаемыми значениями
    abs_diff = np.absolute(diff) # находим абсолютную разность между прогнозами и фактическими наблюдениями.
    mean_diff = abs_diff.mean() # находим среднее значение
    return mean_diff

act = np.array([1.1,2,1.7])
pred = np.array([1,1.7,1.5])
mean_absolute_error(act,pred)

Выход :

0.20000000000000004

Вы также можете использовать mean_absolute_error из sklearn для расчета MAE.

from sklearn.metrics import mean_absolute_error
act = np.array([1.1,2,1.7])
pred = np.array([1,1.7,1.5])
mean_absolute_error(act, pred)

Выход :

0.20000000000000004

4. Функция потерь перекрестной энтропии в Python

Функция потерь перекрестной энтропии также известна как отрицательная логарифмическая вероятность. Это чаще всего используется для задач классификации. Проблема классификации — это проблема, в которой вы классифицируете пример как принадлежащий к одному из более чем двух классов.

Давайте посмотрим, как вычислить ошибку в случае проблемы бинарной классификации.

Давайте рассмотрим проблему классификации, когда модель пытается провести классификацию между собакой и кошкой.

Код Python для поиска ошибки приведен ниже.

from sklearn.metrics import log_loss
log_loss(["Dog", "Cat", "Cat", "Dog"],[[.1, .9], [.9, .1], [.8, .2], [.35, .65]])

Выход :

0.21616187468057912

Мы используем метод log_loss из sklearn.

Первый аргумент в вызове функции — это список правильных меток классов для каждого входа. Второй аргумент — это список вероятностей, предсказанных моделью.

Вероятности представлены в следующем формате:

[P(dog), P(cat)]

Заключение

Это руководство было посвящено функциям потерь в Python. Мы рассмотрели различные функции потерь как для задач регрессии, так и для задач классификации. Надеюсь, вам понравился материал, ведь все было достаточно легко и понятно!

Кстати, для тех, кто хотел бы пойти дальше в изучении функций потерь, мы предлагаем разобрать одну вот такую — это очень интересная функция потерь Triplet Loss в Python (функцию тройных потерь), которую для вас любезно подготовил автор.

In this tutorial, you’ll learn how to calculate the mean absolute error, or MAE, in Python. The mean absolute error can help measure the accuracy of a given machine learning model. The MAE can be a good complement or alternative to the mean squared error (MSE).

By the end of this tutorial, you’ll have learned:

  • What the mean absolute error is
  • How to interpret the mean absolute error
  • How to calculate the mae in Python

Let’s get started!

Table of Contents

What is the Mean Absolute Error

The mean absolute error measures the average differences between predicted values and actual values. The formula for the mean absolute error is:

In calculating the mean absolute error, you

  1. Find the absolute difference between the predicted value and the actual value,
  2. Sum all these values, and
  3. Find their average.

This error metric is often used in regression models and can help predict the accuracy of a model.

How Does the MAE Compare to MSE?

The mean absolute error and the mean squared error are two common measures to evaluate the performance of regression problems. There are a number of key differences betwee the two:

  • Unlike the mean squared error (MSE), the MAE calculates the error on the same scale as the data. This means it’s easier to interpret.
  • The MAE doesn’t square the differences and is less susceptible to outliers

Both values are negatively-oriented. This means that, while both range from 0 to infinity, lower values are better.

How do You Interpret the Mean Absolute Error

Interpreting the MAE can be easier than interpreting the MSE. Say that you have a MAE of 10. This means that, on average, the MAE is 10 away from the predicted value.

In any case, the closer the value of the MAE is to 0, the better. That said, the interpretation of the MAE is completely dependent on the data. In some cases, a MAE of 10 can be incredibly good, while in others it can mean that the model is a complete failure.

The interpretation of the MAE depends on:

  1. The range of the values,
  2. The acceptability of error

For example, in our earlier example of a MAE of 10, if the values ranged from 10,000 to 100,000 a MAE of 10 would be great. However, if the values ranged from 0 through 20, a MAE would be terrible.

The MAE can often be used interpreted a little easier in conjunction with the mean absolute percentage error (MAPE). Calculating these together allows you to see the scope of the error, relative to your data.

In this section, you’ll learn how to calculate the mean absolute error in Python. In the next section, you’ll learn how to calculate the MAE using sklearn. However, it can be helpful to understand the mechanics of a calculation.

We can define a custom function to calculate the MAE. This is made easier using numpy, which can easily iterate over arrays.

# Creating a custom function for MAE
import numpy as np

def mae(y_true, predictions):
    y_true, predictions = np.array(y_true), np.array(predictions)
    return np.mean(np.abs(y_true - predictions))

Let’s break down what we did here:

  1. We imported numpy to make use of its array methods
  2. We defined a function mae, that takes two arrays (true valuse and predictions)
  3. We converted the two arrays into Numpy arrays
  4. We calculated the mean of the absolute differences between iterative values in the arrays

Let’s see how we can use this function:

# Calculating the MAE with a custom function
import numpy as np

def mae(y_true, predictions):
    y_true, predictions = np.array(y_true), np.array(predictions)
    return np.mean(np.abs(y_true - predictions))

true = [1,2,3,4,5,6]
predicted = [1,3,4,4,5,9]

print(mae(true, predicted))

# Returns: 0.833

We can see that in the example above, a MAE of 0.833 was returned. This means that, on average, the predicted values will be 0.833 units off.

In the following section, you’ll learn how to use sklearn to calculate the MAE.

Use Sklearn to Calculate the Mean Absolute Error (MAE)

In this section, you’ll learn how to use sklearn to calculate the mean absolute error. Scikit-learn comes with a function for calculating the mean absolute error, mean_absolute_error. As with many other metrics, with function is in the metrics module.

Let’s see what the function looks like:

# Importing the function
from sklearn.metrics import mean_absolute_error

mean_absolute_error(
    y_true=,
    y_pred=
)

The function takes two important parameters, the true values and the predicted values.

Now let’s recreate our earlier example with this function:

import numpy as np
from sklearn.metrics import mean_absolute_error

true = [1,2,3,4,5,6]
predicted = [1,3,4,4,5,9]

print(mean_absolute_error(true, predicted))

# Returns: 0.833

Conclusion

In this tutorial, you learned about the mean absolute error in Python. You learned what the mean absolute error, or MAE, is and how it can be interpreted. You then learned how to calculate the MAE from scratch in Python, as well as how to use the Scikit-Learn library to calculate the MAE.

Additional Resources

To learn more about related topics, check out the tutorials below:

  • Introduction to Scikit-Learn (sklearn) in Python
  • Splitting Your Dataset with Scitkit-Learn train_test_split
  • How to Calculate Mean Squared Error in Python
  • How to Calculate the Mean Absolute Percentage Error in Python
  • Official Documentation: MAE in Sklearn

Понравилась статья? Поделить с друзьями:
  • Python try except как вывести ошибку
  • Qbittorrent выдает ошибку ввода вывода
  • Python try except все ошибки
  • Q leds сигналы ошибок
  • Python traceback most recent call last blender ошибка