0
$\begingroup$

The function cross_val_score uses the estimator’s default scorer (if available) and LinearRgression (the estimator I use) uses The coefficient of determination (which is defined as $R^2 = 1 - \frac{u}{v}$, where $u$ is the residual sum of squares ((y_true - y_pred)** 2).sum() and $v$ is the total sum of squares ((y_true - y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a score of 0.0.)

Is y_true.mean the y_true.mean of the training set or the testing set? If it's the one of the testing set, isn't it "cheating" i.e. we compare our predictions to a method that has inferred something from the testing set?

So doing better than a baseline wouldn't be having $R^2 > 0$ but rather $R^2 > -0.01$ or something like this?

$\endgroup$
2
  • $\begingroup$ You have model.score(x_test, y_test). Then it computes $R^2$ according to $\hat{y}=model(x_\text{test})$. $u$ accounts for the predictions, whereas $v$ represents an ideal situation in which $\hat{y}$ is the same as y_test (i.e. no errors). You do the ratio of that, so what's the issue? $\endgroup$ Commented May 31, 2023 at 20:14
  • $\begingroup$ From my understanding $v$ represents the situation where I predict the mean, not a situation with no errors? (otherwise $v$ would be $0$ and you'd be dividing by $0$) $\endgroup$ Commented May 31, 2023 at 21:30

2 Answers 2

0
+50
$\begingroup$

The cross_val_score function of the sci-kit-learn library uses the test dataset's mean to calculate the R2 score during each cross-validation fold. This is not called cheating since the R2 score is designed to evaluate how well the model is performing generalized on new unseen datasets and using the test set's mean is a standard part of the R² calculation.

you are right about it y_true.mean is from the testing dataset but it is not cheating because R2 > 0 means the model performs better than a constant baseline that predicts the mean of the test dataset.

If your model can't beat this simple baseline, it means the model isn't effectively learning the relationship between the input features and the target.

The baseline comparison is not meant to "cheat" by using information from the testing set; rather, it is a fair baseline that checks if your model is doing better than simply predicting the average of the testing set’s target values.

Should R² > -0.01 Be the Baseline?

Not really. The baseline you are competing against is R² = 0, which is the score of a constant model that predicts the mean of y_test. A score below 0 means the model is performing worse than simply predicting the mean of the test data, but R² > 0 is the standard for doing better than a baseline model.

I am not going much deep into the interpretation of the r2 score like what the value of r2 suggests about the model results since Luca Anzalone has explained well in above answer

for the question So doing better than a baseline wouldn't be having R2>0 but rather R2>−0.01 or something like this?

=> No, doing better than the baseline is indicated by having an R² score greater than 0. it is due to following reasons

An R² score of 0 means that your model performs as well as a constant model that predicts the mean of the target values from the test set, ignoring the input features.

An R² score greater than 0 means your model is doing better than the baseline (constant mean prediction).

An R² score less than 0 means your model is doing worse than the baseline.

If your model had an R² score of something like -0.01, that would imply that the model's predictions are slightly worse than just predicting the mean of the test set. But the true goal is to have R² > 0, which indicates that your model is leveraging the features to make predictions that outperform the simple mean baseline.

if you have still doubt feel free to ask and also ignore some notation mistakes since i am new at typing notation on stack :)

$\endgroup$
8
  • $\begingroup$ but if my R² is exactly 0 it means I was able to do as well as if I had access to the mean of the test set, without having access to the test set, so I should expect a "pat on the back", not a score of 0, right? $\endgroup$ Commented Oct 24 at 7:53
  • $\begingroup$ R² = 0 means your model’s predictions are no better than simply predicting the mean of the test set . While it’s true that you didn't "cheat" by using the test set directly, R² = 0 means your model’s predictions are no better than simply predicting the mean of the test set While it’s true that you didn't "cheat" by using the test set directly since the R² score is designed to answer: "How well does my model use the input features to predict the target variable? R² = 0 doesn't punish you, but it reflects that your model hasn't added value beyond predicting the test mean. $\endgroup$ Commented Oct 24 at 8:14
  • $\begingroup$ Ok, so one should still not be unhappy if R² = 0, it would mean that one has learnt $\epsilon > 0$ about the task at hand with the training set. I would hence expect that the strategy of 'taking the mean of the output variable of the training set to predict the output variable in the test set' would on average get me R² < 0. Is this really desirable? $\endgroup$ Commented Oct 24 at 9:15
  • $\begingroup$ Yes you are right Achieving R² = 0 does indicate that your model is doing at least as well as predicting the test set’s mean, even though you don't have access to it. That means your model has indeed learned something about the task at hand using the training data. So, in a practical sense, you're not in a bad spot if R² = 0 — it just reflects that you're not yet improving over the baseline using the input features. $\endgroup$ Commented Oct 24 at 9:39
  • $\begingroup$ If you use the strategy of predicting the mean of the output variable from the training set for the test set, you're right to suspect that this could often result in an R² less than 0. because R2 means the generalization of the model its formula compares the model results with the baseline of the mean of the test set R² < 0 indicates that your model is worse than simply predicting the mean of the test set. This happens because your model's training-set-based predictions don't fit the test set as well as the constant mean baseline would $\endgroup$ Commented Oct 24 at 9:41
0
$\begingroup$

Let's clarify this with a numerical example. Let's assume y_true = [0, 1, 1] to be the true class labels of $N=3$ test points. Therefore, y_true.mean() = 2/3.

Case 1: everthing is wrongly predicted, so y_pred = [1, 0, 0]. Let's compute $u$ and $v$. $$ \begin{align} u &= \sum(y_\text{true}-y_\text{pred})^2 = (0-1)^2+(1-0)^2 +(1-0)^2 = 3 \\ v &= \sum \Big(y_\text{true}-\frac23\Big)^2 = (-2/3)^2+(1/3)^2+(1/3)^2 = \frac69 = \frac23 \end{align}$$ Now we compute $R^2$, so: $$R^2 = 1 - \frac{u}{v} = 1 - \frac{3}{2/3} = 1 - 2 = \mathbf{-1}$$ You get -1 because y_pred is always wrong. so you can think of $u$ as counting the number of errors (it's so if you predict the label to be exactly zero or one), whereas $v$ is the ratio of positive labels.

Case 2: perfect predictions, so y_pred = y_true. $$ \begin{align} u &= (0-0)^2+(1-1)^2 +(1-1)^2 = 0\quad\quad\text{(no errors)} \\ R^2 &= 1 -\frac{0}{2/3} = 1 \end{align}$$ $R^2=1$ because every label is correctly predicted, so there are no errors and the correlation is the most possible.

Case 3: some errors, say y_pred=[0, 1, 0]. Again: $$ \begin{align} u &= (0-0)^2+(1-1)^2 +(1-0)^2 = 1 \\ R^2 &= 1 -\frac{1}{2/3} = 1 - \frac32 = -\frac12 \end{align}$$ $R^2=-0.5$, so negative correlation. From this it seems that $R^2$, in general, gives more importance to correctly predicting the positive class.

So, there is no issue about using the mean of the true labels because that info is not used during training but only to compute an evaluation metric.

$\endgroup$
5
  • $\begingroup$ Maybe my post wasn't clear but I'm talking about cross_val_score which evaluates the coefficient of determination on the testing set? $\endgroup$ Commented Jun 1, 2023 at 22:48
  • $\begingroup$ I see. cross_val_score takes a model and a dataset ($X$, $y$). It trains the model $k$ times, each time leaves out (for validation) a "fold", and then computes the score (e.g. $R^2$) on that fold. You use cross_val_score to validate your model, so $(X, y)$ is actually the training set not the test set (otherwise you "cheat"). Once you determined the best model you retrain it on all the train-set, and do the final evaluation on test (e.g. by computing $R^2$). Read here for more. $\endgroup$ Commented Jun 2, 2023 at 10:12
  • $\begingroup$ I see. But when computing the score (e.g. $R^2$), it does use y_true.mean in v, right? y_true.mean being the mean of the testing fold. $\endgroup$ Commented Jun 2, 2023 at 15:18
  • $\begingroup$ You perform cross_val_score on $(X_\text{train},y_\text{train})$, so for each fold (there are $k$ of them) you have $(X_\text{train}^k,y_\text{train}^k)$ and $(X_\text{val}^k,y_\text{val}^k)$. Evaluation is performed on the latter pair, and y_true.mean refers to $y_\text{val}^k$. You do this, see the results and if satisfied you finally evaluate on the test-set: you don't use cross_val_score otherwise it would train on test data which is wrong, but instead use r2_score $\endgroup$ Commented Jun 2, 2023 at 18:00
  • $\begingroup$ I see, you don't use cross_val_score to get the the coefficient of determination of your method. But, to get the coefficient of determination of your method, you calculate it on the test-set. However, I believe my questions still stand. The y_true.mean is now $y_{test}$ right? So when calculating the coefficient of determination on the test-set, v depends on the mean of the test set. So if $R^2_{test}$ is equal to 0 it means we do as well as a method that inferred a little bit something from the test set (y_true.mean) so we learned a little more than just noise? $\endgroup$ Commented Jun 3, 2023 at 13:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .