KerasRegressor
R^2 `Score`
Machine Learning
Regression Analysis
Python

KerasRegressor Coefficient of Determination R2 `Score`

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

For a Keras-based regression model, the R2 score is usually computed outside the neural network with sklearn.metrics.r2_score. Even if you are using a KerasRegressor wrapper, the idea stays the same: train the model, generate predictions, and compare those predictions to the true targets.

That matters because R2 is a regression evaluation metric, not something special to Keras itself. The wrapper may help integrate with scikit-learn workflows, but the definition of the score is still the usual coefficient of determination.

Compute R2 After Prediction

A straightforward pattern looks like this:

python
1import numpy as np
2from sklearn.metrics import r2_score
3from tensorflow import keras
4
5X = np.random.rand(200, 3)
6y = X[:, 0] * 2.0 - X[:, 1] + 0.5
7
8model = keras.Sequential([
9    keras.layers.Dense(8, activation="relu", input_shape=(3,)),
10    keras.layers.Dense(1)
11])
12
13model.compile(optimizer="adam", loss="mse")
14model.fit(X, y, epochs=20, verbose=0)
15
16predictions = model.predict(X, verbose=0).ravel()
17score = r2_score(y, predictions)
18print(score)

This is the standard answer whether the model came from plain Keras or a scikit-learn style wrapper.

What R2 Actually Means

R2 answers the question: how much of the variance in the target variable is explained by the model compared with a naive baseline that always predicts the mean.

The rough interpretation is:

  • '1.0 means perfect predictions'
  • '0.0 means no better than predicting the target mean'
  • negative values mean worse than the mean baseline

That last case surprises people. Negative R2 is not a bug in the metric; it means the regression model is performing poorly on the evaluated data.

Use R2 in Cross-Validation

If you want scikit-learn style evaluation, use scoring='r2' in cross-validation. The exact wrapper varies by library version, but the concept is the same: the estimator trains on each fold, predicts on the validation fold, and the fold score is R2.

Example with a scikit-learn compatible estimator:

python
1from sklearn.model_selection import cross_val_score
2from scikeras.wrappers import KerasRegressor
3from tensorflow import keras
4
5
6def build_model():
7    model = keras.Sequential([
8        keras.layers.Dense(8, activation="relu", input_shape=(3,)),
9        keras.layers.Dense(1)
10    ])
11    model.compile(optimizer="adam", loss="mse")
12    return model
13
14estimator = KerasRegressor(model=build_model, epochs=20, verbose=0)
15scores = cross_val_score(estimator, X, y, cv=5, scoring="r2")
16print(scores.mean())

This is usually a better estimate of generalization quality than evaluating R2 only on the training data.

Should R2 Be a Keras Metric During Training?

You can define an R2-like metric inside Keras, but it is often simpler and safer to compute R2 after prediction on a validation or test set. Batch-wise training metrics can be misleading for metrics such as R2 because the score depends on the target distribution of the evaluated set.

So for most workflows:

  • train with a loss such as MSE
  • evaluate final predictions with r2_score

That separation keeps the training objective and reporting metric clear.

Common Pitfalls

  • Computing R2 on the training set and assuming it reflects real generalization performance.
  • Expecting R2 to stay between 0 and 1; it can be negative.
  • Using R2 as the training loss instead of as an evaluation metric.
  • Forgetting to flatten prediction arrays when the scoring function expects one-dimensional target output.

Summary

  • For Keras regression, compute R2 with sklearn.metrics.r2_score after generating predictions.
  • The metric interpretation is the usual one: 1.0 is perfect, 0.0 matches the mean baseline, and negative is worse.
  • Cross-validation with scoring='r2' is often a better evaluation workflow than training-set scoring.
  • Use a proper regression loss such as MSE during training.
  • Treat R2 as a reporting metric, not as something uniquely defined by the Keras wrapper.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.