Keras
Regressor
R^2 `Score`
Coefficient of Determination
Machine Learning

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

If you are training a neural network for regression, accuracy is usually the wrong metric to watch. A better fit statistic is often the coefficient of determination, usually written as , which measures how much variance in the target your model explains.

When people ask about KerasRegressor and , the real issue is usually where that metric should be computed. In practice, the simplest and most reliable answer is to fit the model, generate predictions, and then compute with scikit-learn on the full evaluation set.

What Tells You

compares your model against a naive baseline that always predicts the mean target value.

  • 'R² = 1.0 means perfect predictions.'
  • 'R² = 0.0 means the model is no better than predicting the mean.'
  • Negative means the model performs worse than that naive baseline.

This metric is useful for regression because it gives you a quick sense of explanatory power. It does not tell you everything, but it is easy to compare across experiments when the target variable stays the same.

Computing After Training

In modern code, KerasRegressor is commonly used through SciKeras, which follows the scikit-learn estimator interface. That makes it straightforward to evaluate with r2_score.

python
1from scikeras.wrappers import KerasRegressor
2from sklearn.datasets import make_regression
3from sklearn.metrics import r2_score
4from sklearn.model_selection import train_test_split
5from tensorflow import keras
6
7X, y = make_regression(
8    n_samples=1000,
9    n_features=20,
10    noise=15.0,
11    random_state=42,
12)
13
14X_train, X_test, y_train, y_test = train_test_split(
15    X, y, test_size=0.2, random_state=42
16)
17
18
19def build_model():
20    model = keras.Sequential(
21        [
22            keras.layers.Input(shape=(20,)),
23            keras.layers.Dense(64, activation="relu"),
24            keras.layers.Dense(32, activation="relu"),
25            keras.layers.Dense(1),
26        ]
27    )
28    model.compile(optimizer="adam", loss="mse")
29    return model
30
31
32regressor = KerasRegressor(
33    model=build_model,
34    epochs=20,
35    batch_size=32,
36    verbose=0,
37)
38
39regressor.fit(X_train, y_train)
40predictions = regressor.predict(X_test)
41
42print("R2:", r2_score(y_test, predictions))

This approach is clear and correct because is computed over the entire test set. That matters: batch-by-batch metrics during training can be misleading for regression, especially when the final metric depends on the mean and total variance across the whole dataset.

Using in Cross-Validation

If you want model selection or hyperparameter search, let scikit-learn handle the scoring. Pass scoring="r2" to the evaluation function instead of writing your own training loop metric.

python
1from sklearn.model_selection import cross_val_score
2
3scores = cross_val_score(regressor, X, y, cv=5, scoring="r2")
4print("Fold scores:", scores)
5print("Mean R2:", scores.mean())

That is usually better than trying to force into model.compile(metrics=[...]). Keras metrics run batch-wise during training, while is most meaningful when computed across the complete validation or test split.

When a Custom Keras Metric Makes Sense

You can implement a custom metric in Keras, but it is mainly useful for monitoring and not as the final authoritative evaluation. Stateful metric implementations are easy to get wrong, and the result may differ from scikit-learn's r2_score if you average per-batch values instead of aggregating globally.

For most workflows, a practical pattern is:

  • train the model with a regression loss such as mse
  • use validation loss to monitor training
  • compute final with scikit-learn after prediction

That separation keeps training stable and evaluation honest.

Common Pitfalls

  • Using classification metrics like accuracy for a regression problem. They do not describe regression quality.
  • Expecting a positive by default. A poorly tuned model can easily produce a negative score.
  • Computing on the training set only. That often hides overfitting.
  • Treating a custom batch-wise Keras metric as identical to dataset-level r2_score. They are not always the same.
  • Ignoring preprocessing. Unscaled features, noisy labels, or a badly chosen learning rate can destroy even when the code is technically correct.

Summary

  • ' is a regression evaluation metric, not a training loss.'
  • With KerasRegressor, the safest path is to predict on a held-out set and call sklearn.metrics.r2_score.
  • For model selection, use scikit-learn tools with scoring="r2".
  • A custom Keras metric can be useful for monitoring, but it should not replace final evaluation.
  • If is negative, inspect data quality, scaling, model capacity, and whether you are evaluating on unseen data.

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.