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 :)
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 asy_test
(i.e. no errors). You do the ratio of that, so what's the issue? $\endgroup$