XGBRegressor score method returning strange values

Question:

I’ve tried to use XGBRegressor’s score method from the Python API and It’s returning a result of 0.917. I am expecting this to be the r2 score of the regression.

However, trying r2_score from sklearn on the same package, it returns a different value (0.903)

xgbr.score(x_test, y_test) # Returns 0.917
y_pred = xgbr.predict(x_test)
r2_score(y_pred, y_test) # Returns 0.903

What’s going on? I couldn’t find any documentation on XGBoost’s score method. I’m using v0.7

Asked By: i712345

||

Answers:

When you call xgbr.score() this code is actually called:

    ...
    return r2_score(y, self.predict(X), sample_weight=None,
                    multioutput='variance_weighted')

But when you are calling the r2_score explicitly, the default value of multiouput param is “uniform_average”.

Try the below code:

r2_score(y_pred, y_test, multioutput='variance_weighted')

And you will get identical results.

Answered By: Vivek Kumar

You meant sample weight = None is replaced "uniform_average" and not multiouput param!

Answered By: gulshan madhur
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.