Explain the underlying processing of `multioutput=[0.3, 0.7]` in sklearn.metrics.mean_absolute_error

Viewed 415

based on the doc

The mean_absolute_error function computes mean absolute error, a risk metric corresponding to the expected value of the absolute error loss or L1-norm loss.

I understand the process for 2 "vectors"

>>> 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)

which is to add all the absolute difference between peer elements in each vector, and then divided by the length of the vector.

this code is exactly underlying processing of sklearn.mean_absolute_error for 2 "vectors"

res = 0
for t,p in zip(y_true, y_pred):
    res = res + np.abs(t-p)
res/4

what I cannot understand is this processing

>>> mean_absolute_error(y_true, y_pred, multioutput=[0.3, 0.7])
0.85

Could someone please illustrate the underlying processing of multioutput=[0.3, 0.7] part?

2 Answers

It's explained a little higher on that same docs page.

If you want to see explicitly what it does, you can see it in the source: mean_absolute_error() ends by calling numpy's average() function with weights equal to multioutput.

So multioutput is just a way to tell mean_absolute_error() that when calculating the mean error, the errors on some of the quantities should count more towards that averge than others.

As I understand, multioutput=[0.3, 0.7] is the weights for output array.

First, we talk about raw_values

>>> mean_absolute_error(y_true, y_pred, multioutput='raw_values')
array([0.5, 1. ])

The array([0.5, 1. ]) is the original value of mean_absolute_error(). So if you put another weight, it will calculate the average output based on what you puts in multioutput.

If you don't give any value, the default is [1,1].

>>> mean_absolute_error(y_true, y_pred)   # (0.5*1 + 1*1)/(1+1) = 0.75
0.75

OK, now if you give it another values like [0.3, 0.7].

>>> # (0.5*0.3+1*0.7)/(0.3+0.7) = 0.85
>>>mean_absolute_error(y_true, y_pred, multioutput=[0.3, 0.7])
0.85

Note: Depending on the number of dimensions of y_true, y_pred, you must input the match length with multioutput. Example y_pred = [[0, 2], [-1, 2], [8, -5]] is 2-D matrix, it will give you the output with 2 values like [0.5, 1.]. So you must give 2 values as weights for that output like [0.3, 0.7]

Related