Keras for Regression

Keras Basics

2 min read

Published Nov 17 2025, updated Aug 17 2026


11
0
0
0

KerasNeural NetworksPythonTensorFlow

Regression models predict continuous numeric values.

Examples:

  • Predicting house prices
  • Predicting temperature
  • Forecasting sales
  • Predicting age
  • Predicting a score or rating

For this section, we will use the California Housing dataset, a real tabular dataset commonly used in ML.

We will cover:

  1. Loading the dataset
  2. Normalising the inputs
  3. Building a regression network
  4. Training it
  5. Evaluating it (MAE/MSE/RMSE)
  6. Predicting new values





Load the California Housing Dataset

The dataset comes from scikit-learn.

from sklearn.datasets import fetch_california_housingimport numpy as npdata = fetch_california_housing()# featuresX = data.data # target values  y = data.target 

Check shapes:

print(X.shape)  # (20640, 8)print(y.shape)  # (20640,)

There are 8 numerical features per house (median income, rooms, population, etc.).






Train/Test Split

We’ll split the dataset manually:

from sklearn.model_selection import train_test_splitX_train, X_test, y_train, y_test = train_test_split(    X, y, test_size=0.2, random_state=42)





Normalise the Inputs

Neural networks must work with normalised features for stability.

Keras has a built-in preprocessing layer:

from tensorflow.keras import layersfrom tensorflow import kerasnormaliser = layers.Normalization()normaliser.adapt(X_train)

This layer:

  • Computes mean and variance on training data
  • Applies normalisation consistently during training and inference





Build a Regression Model

Simple MLP for regression:

model = keras.Sequential([    normaliser,    layers.Dense(64, activation='relu'),    layers.Dense(32, activation='relu'),    # Output = 1 numeric value    layers.Dense(1)  ])

Important details:

  • No activation on final layer
  • Regression networks predict raw values





Compile the Model

Regression uses:

  • Loss: mean squared error (MSE)
  • Metrics: mean absolute error (MAE)
model.compile(    optimizer='adam',    loss='mse',    metrics=['mae'])

MAE is the easiest to interpret (units are same as target).






Train the Model

history = model.fit(    X_train, y_train,    epochs=10,    batch_size=32,    validation_split=0.1)

This dataset trains very quickly.






Evaluate on Test Set

mse, mae = model.evaluate(X_test, y_test)print("MAE:", mae)print("RMSE:", np.sqrt(mse))

Typical results:

  • MAE ≈ 0.40–0.55
  • RMSE ≈ 0.55–0.70

This means your predictions are off by about:

  • $0.40–$0.55 median house price units
  • The dataset target is in $100,000s, so MAE ≈ 0.5 means ≈ $50,000 error





Making Predictions

sample = X_test[:1]pred = model.predict(sample)print("Predicted:", pred[0][0])print("Actual:", y_test[0])





Visualising Loss Curves

import matplotlib.pyplot as pltplt.plot(history.history['loss'])plt.plot(history.history['val_loss'])plt.legend(['Train Loss', 'Validation Loss'])plt.show()






Improving Regression Models (Quick Tips)

Add more layers / units - Regression tasks often need deeper networks.


Add regularisation

layers.Dense(64, activation='relu', kernel_regularizer='l2')

Use Dropout

layers.Dropout(0.2)

Use learning rate schedules

keras.optimizers.Adam(learning_rate=0.001)

Train for more epochs - But watch for overfitting.


Try the Functional API - Useful for complex tabular models.






Full Working Script

from sklearn.datasets import fetch_california_housingfrom sklearn.model_selection import train_test_splitimport numpy as npfrom tensorflow import kerasfrom tensorflow.keras import layers# Load datasetdata = fetch_california_housing()X = data.datay = data.target# Train-test splitX_train, X_test, y_train, y_test = train_test_split(    X, y, test_size=0.2, random_state=42)# Normalisation layernormalizer = layers.Normalization()normalizer.adapt(X_train)# Build modelmodel = keras.Sequential([    normalizer,    layers.Dense(64, activation='relu'),    layers.Dense(32, activation='relu'),    layers.Dense(1)])# Compilemodel.compile(    optimizer='adam',    loss='mse',    metrics=['mae'])# Trainhistory = model.fit(    X_train, y_train,    epochs=10,    batch_size=32,    validation_split=0.1)# Evaluatemse, mae = model.evaluate(X_test, y_test)print("MAE:", mae)print("RMSE:", np.sqrt(mse))# Predictprint("Predicted:", model.predict(X_test[:1])[0][0])print("Actual:", y_test[0])
© 2025 SimpleSteps.guide
AboutFAQPoliciesContact
Keras Basics | Keras for Regression | SimpleSteps.guide