R² asks how much better your regression is than the laziest possible model — one that ignores the inputs and always predicts the mean.
1R² = 1 - SS_res / SS_tot23SS_res = Σ (y - prediction)² your squared error4SS_tot = Σ (y - mean(y))² the mean-predictor's squared error
Task: write r2_score(y_true, y_pred) returning R², rounded to 4 decimal places.
SS_tot is zero (every true value identical) return 0.0.The upper bound is 1, but there's no lower one. Predicting worse than the mean gives a negative R², which is a genuinely useful signal — it means the model isn't just weak, it's actively worse than a constant.