How is the R2 value in Scikit learn calculated?

machine-learning, python, scikit-learn, statistics

Solution

The `R^2` in scikit learn is essentially the same as what is described in the wikipedia article on the coefficient of determination (grep for "the most general definition"). It is `1 - residual sum of square / total sum of squares`.

The big difference between a classical stats setting and what you usually try to do with machine learning, is that in machine learning you evaluate your score on unseen data, which can lead to results outside `[0,1]`. If you apply `R^2` to the same data you used to fit your model, it will lie within `[0, 1]`

See also this very similar question

Problem

The R^2 value returned by scikit learn (`metrics.r2_score()`) can be negative. The docs say: "Unlike most other scores, R² score may be negative (it need not actually be the square of a quantity R)." However the wikipedia article on R^2 mentions no R (not squared) quantity. Perhaps it uses absolute differences instead of square differences. I really have no idea

Original source

Related problems