How to score mean absolute error using cross_val_score and KFold from sklearn

Viewed 553

I'm wondering how to compute the mean_absolute_error using cross_val_score of sklearn:

I have X,y and I'm writing

cross_val_score(X,y, cv=KFold(n_splits=5, shuffle=True))

Maybe I should try to split the data and instantiating a for loop.. something like

from sklearn.model_selection import KFold as kf

for i in kf.split(X,y):

...

?
2 Answers

You should be able to use the scoring parameter like this:

scores = cross_val_score(X,y, cv=KFold(n_splits=5, shuffle=True), scoring='neg_mean_absolute_error')

print('Mean score over all folds: ', scores.mean())

I'm answering to my own question as I think that I have found another way in order to obtain the mean_absolute_error straight. Hope to receive some feedback on this.

kf = KFold(n_splits=5, shuffle=True)
xgb = XGBoostRegressor()
scores = []

for train_index, test_index in kf.split(X,y):
    xgb.fit(X.iloc[train_index].values, y.iloc[train_index].values)
    preds = xgb.predict(X.iloc[test_index].values)
    mean_abs = mean_absolute_error(y.iloc[test_index].values, preds)
    scores.append(mean_abs)
Related