Search code examples
pythonscikit-learnxgboost

XGBRegressor score method returning strange values


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


Solution

  • 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.