zzfima
zzfima

Reputation: 1565

sklearn.metrics mean_absolute_error calculation

I use manual MAE calculations and sklearn.metrics and got different results. Why?

from sklearn.metrics import mean_absolute_error as MAE

cnt = 0
error = 0
len_y = len(y)
len_y_pred = len(y_pred)
if len_y == len_y_pred:
      for i in range(len_y):
            if y_pred[i] != y.values[i]:
                  cnt += 1
                  error += abs(y.values[i] - y_pred[i])
      print('manual MAE = ', error / cnt)

# MAE from sklearn
print('sklearn MAE = ', MAE(y, y_pred))

output:

manual MAE =  96189.48047877151
sklearn MAE =  15074.239113119293

why so different?

thanks

Upvotes: 3

Views: 11419

Answers (1)

StupidWolf
StupidWolf

Reputation: 46898

The mean absolute error is the sum of absolute errors over the length of observations / predictions. You do not exclude the observation from n even if they happen to be the same. So modifying your code:

from sklearn.metrics import mean_absolute_error as MAE
import pandas as pd

y = pd.DataFrame({'values':[3,6,5,8,9,4,2]})
y_pred = [4,8,7,3,2,4,2]

error = 0
for i in range(len_y):
    error += abs(y.values[i] - y_pred[i])

print('manual MAE = ', error / len(y))
print('sklearn MAE = ', MAE(y, y_pred))

manual MAE =  [2.42857143]
sklearn MAE =  2.4285714285714284

Upvotes: 6

Related Questions