October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk17 min

Regression Tutorial with the Keras Deep Learning Library in Python

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Regression with Keras is a practical way to build neural networks that predict continuous values, such as house prices, fuel efficiency, sales revenue, temperature, or customer lifetime value. Instead of assigning inputs to fixed categories, a regression model learns patterns in numerical data and returns a value on a scale.

This tutorial walks through the full workflow for creating a regression model in Python using the Keras deep learning library. You will prepare input data, define a neural network architecture, train the model, track performance, generate predictions, and evaluate how well the model performs on unseen data.

As an Amazon Associate I earn from qualifying purchases.

Along the way, you will also see how tuning choices such as layer size, activation functions, optimizers, normalization, regularization, and training duration can affect accuracy. The goal is to give you a clear foundation for building reliable continuous-value prediction models with Keras.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What Regression Means in Deep Learning

Regression is a supervised learning task where the model predicts a continuous numeric value instead of a class label. In a Keras deep learning workflow, that could mean predicting a house price, tomorrow’s temperature, a customer’s expected spending amount, the fuel efficiency of a car, or the time required to complete a delivery. The training data contains input features, such as square footage or engine size, and a target value, such as sale price or miles per gallon. The neural network learns a function that maps the inputs to the target as accurately as possible.

This differs from classification, where the output is a category such as spam or not spam, cat or dog, or one of several product types. A classification model often uses activation functions such as sigmoid or softmax in the final layer and metrics such as accuracy. A regression model usually has a single output neuron for one target value, often with a linear activation, meaning no activation function is applied to the final layer. This lets the network produce unrestricted numeric predictions such as 247500.0, 31.8, or -2.4.

In deep learning, regression works by adjusting the weights inside the neural network so that predicted values get closer to the true target values in the training set. During training, Keras sends batches of examples through the model, calculates how far the predictions are from the real values, and updates the model parameters using an optimizer such as Adam or stochastic gradient descent. The loss function defines what “far” means. For regression, common choices include mean squared error, mean absolute error, and Huber loss.

  • Mean squared error: Penalizes large errors heavily because each error is squared. It is useful when large mistakes should be strongly discouraged.
  • Mean absolute error: Measures the average absolute difference between predictions and true values. It is easier to interpret because it uses the same units as the target.
  • Huber loss: Combines behavior from mean squared error and mean absolute error, making it useful when the data contains outliers.

A simple regression dataset can be represented as a matrix of input features and a vector of target values. For example, a housing dataset might include bedrooms, bathrooms, lot size, location indicators, property age, and distance to transit as input columns. The target column might be the final sale price. The model does not memorize a direct lookup table; instead, it learns patterns across many examples so it can estimate prices for houses it has not seen before.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Regression models are sensitive to the scale and quality of the data. A feature measured in thousands, such as annual income, can dominate another feature measured between 0 and 1 unless the inputs are standardized or normalized. Missing values, extreme outliers, and inconsistent target units can also make training unstable. For this reason, regression projects in Keras usually spend significant effort on data preparation before defining the neural network architecture. Clean, consistently scaled inputs help the optimizer move toward better weights and make the training loss easier to interpret.

Preparing Data for a Keras Regression Model

Good regression results in Keras usually depend more on clean, well-shaped data than on a complicated neural network. For a continuous-value prediction task, you need a feature matrix X containing the input variables and a target vector or matrix y containing the numeric value to predict. For example, in a house-price model, features might include square footage, number of bedrooms, age of the property, and location-derived values, while the target is the sale price.

A typical workflow starts by loading the dataset with pandas, separating inputs from the target column, handling missing values, encoding categorical variables, and converting the final arrays into numeric types that TensorFlow can process efficiently. Keras models expect tensors or NumPy arrays with consistent shapes, so every row must have the same number of input features after preprocessing.

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

df = pd.read_csv("housing.csv")

X = df.drop("price", axis=1)
y = df["price"]

Missing values should be handled before training. For numeric columns, common choices include replacing missing values with the median or mean. For categorical columns, you can replace missing values with a placeholder such as Unknown before encoding. In a production-quality workflow, use scikit-learn pipelines so the same transformations are applied consistently to training, validation, and future prediction data.

numeric_cols = X.select_dtypes(include=["int64", "float64"]).columns
categorical_cols = X.select_dtypes(include=["object", "category"]).columns

X[numeric_cols] = X[numeric_cols].fillna(X[numeric_cols].median())
X[categorical_cols] = X[categorical_cols].fillna("Unknown")

X = pd.get_dummies(X, columns=categorical_cols, drop_first=True)

After cleaning and encoding, split the dataset into training and test sets. The training set is used to fit the model, while the test set is held back to estimate how well the model performs on unseen data. Many regression projects also use a validation split during training so Keras can report validation loss after each epoch.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)

Feature scaling is especially useful for neural networks. If one feature ranges from 0 to 1 and another ranges from 10,000 to 500,000, gradient-based optimization can become slower and less stable. Standardization transforms each numeric input so it has approximately zero mean and unit variance. Fit the scaler only on the training data, then use that same scaler to transform the test data.

scaler = StandardScaler()

X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

The target variable may also need attention. If the target has a highly skewed distribution, such as home prices or insurance claims, applying a log transformation can make training easier. When you transform the target, remember to reverse the transformation before interpreting predictions in the original units.

import numpy as np

y_train_log = np.log1p(y_train)
y_test_log = np.log1p(y_test)

Before building the Keras model, confirm the final input shape. A dense neural network for tabular regression typically receives data shaped as (number_of_samples, number_of_features). The number of features becomes the input dimension for the first layer of the model.

n_features = X_train_scaled.shape[1]
print(n_features)

At this point, the data is ready for modeling: missing values are handled, categorical columns are encoded, features are scaled, the target is prepared, and the train-test split is in place. These steps create a reliable foundation for the next stage, where you define a Keras neural network that maps the prepared input features to a continuous numeric prediction.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Building a Neural Network Regression Model with Keras

Once the input features have been cleaned, encoded, split, and scaled, the next step is to define the neural network that will learn the relationship between those features and the continuous target value. In Keras, a regression model is usually built as a stack of dense layers. Each dense layer applies learned weights to the input data, adds a bias term, and passes the result through an activation function. For tabular regression problems, this simple feedforward structure is often a strong starting point.

The most direct way to create this model is with the Sequential API. A typical regression network has an input layer that matches the number of training features, one or more hidden layers, and a final output layer with a single neuron. That single neuron represents the predicted numeric value, such as a house price, fuel efficiency score, medical measurement, or monthly sales amount.

Defining a basic regression model

A practical Keras regression model might use two hidden layers with ReLU activations followed by a linear output layer. The input shape should match the number of columns in X_train. For example, if the prepared dataset has 12 input features, the model should receive 12 values per row. The output layer should not use sigmoid or softmax activation because those are suited to classification-style outputs. For unrestricted continuous prediction, the final layer is commonly left linear.

  • Input shape: Set to the number of prepared input features.
  • Hidden layers: Dense layers with activations such as relu are common for regression.
  • Output layer: Use one neuron for a single continuous target.
  • Final activation: Use the default linear output for most regression tasks.

A compact model definition can look like this in structure: a dense layer with 64 neurons, another dense layer with 32 neurons, and a final dense layer with 1 neuron. The first hidden layer receives the input shape, while later layers infer their shape automatically from the previous layer. Larger layers can learn more complex patterns, but they also increase the risk of overfitting, especially when the dataset is small.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choosing the loss function and optimizer

After defining the layers, the model must be compiled. Compilation tells Keras how to measure prediction error and how to update model weights during training. For regression, mean squared error is a common loss function because it penalizes larger errors more heavily. Mean absolute error is also useful when you want a metric that is easier to interpret in the same units as the target variable.

Component Common choice Use in regression
Loss function mse Optimizes the model by reducing squared prediction errors.
Metric mae Reports average absolute error in target units.
Optimizer adam Updates weights efficiently with adaptive learning rates.

The Adam optimizer is a reliable default for many Keras regression workflows because it works well without much initial tuning. A good first compile setup is to optimize mse while tracking mae. This gives the model a strong training objective while still showing a human-readable error measure during training. With the architecture and compile settings in place, the model is ready for the training loop, where it will repeatedly process batches of examples and adjust its weights to reduce prediction error.

Training the Model and Monitoring Performance

After the regression model is defined and compiled, training begins with the Keras fit() method. This method feeds the prepared input features and target values through the network, calculates the loss, updates the weights, and repeats the process for a chosen number of epochs. For regression, the most common loss function is mean squared error, while metrics such as mean absolute error make the results easier to interpret in the original target scale.

A typical training call uses the training arrays, a validation split or separate validation set, a batch size, and the number of epochs. The validation data is especially useful because it shows how well the model performs on data it is not directly learning from. If training loss keeps falling while validation loss starts rising, the model is likely memorizing the training set rather than learning a pattern that generalizes.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Example training workflow

The following structure is common for a Keras regression model after preprocessing and model compilation:

  • Training features: scaled input variables, such as X_train.
  • Training targets: continuous numeric values, such as y_train.
  • Validation data: held-out examples, such as X_val and y_val.
  • Epochs: the maximum number of full passes through the training data.
  • Batch size: the number of samples processed before each weight update.

For example, you might train with model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=100, batch_size=32). The returned History object stores the training loss, validation loss, and any metrics specified during compilation. You can access these values through history.history and plot them to inspect the learning curve.

Using callbacks during training

Callbacks help control training without manually watching every epoch. EarlyStopping can stop training when validation loss stops improving, which reduces overfitting and saves time. ModelCheckpoint can save the best model weights based on validation performance, rather than simply keeping the weights from the final epoch.

  • EarlyStopping: stops training after validation loss fails to improve for a set number of epochs.
  • ModelCheckpoint: saves the best-performing model during training.
  • ReduceLROnPlateau: lowers the learning rate when progress stalls.

A practical setup might monitor val_loss with a patience value of 10 or 15 epochs. This gives the model time to improve while preventing long runs that only fit noise. In many regression projects, using early stopping with restored best weights gives a more reliable final model than training for a fixed number of epochs.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Interpreting the training curves

Pattern What it suggests Possible adjustment
Training and validation loss both decrease The model is learning useful patterns Continue training or allow early stopping to decide
Training loss decreases, validation loss increases The model is overfitting Add regularization, reduce model size, or stop earlier
Both losses remain high The model may be underfitting Add capacity, train longer, or improve features
Loss jumps or becomes unstable The learning rate may be too large Lower the learning rate or use adaptive scheduling

Monitoring performance during training is not just about reaching the lowest possible training loss. For continuous-value prediction, the main goal is reliable performance on unseen data. A smooth validation curve, stable error metrics, and a sensible stopping point usually matter more than squeezing out small gains on the training set.

Evaluating Regression Results and Making Predictions

After training, evaluate the model on data it did not see during fitting, usually the held-out test set. This gives a more honest estimate of how the regression model performs on new examples. In Keras, evaluation commonly uses model.evaluate() for the configured loss and metrics, followed by model.predict() to generate continuous-value predictions that can be compared with the true targets.

For a regression model compiled with mean squared error and mean absolute error, evaluation might look like this: test_loss, test_mae = model.evaluate(X_test, y_test, verbose=0). If the target represents house prices, test_mae is especially useful because it is expressed in the same units as the target. For example, a mean absolute error of 18500 means the model is off by about 18,500 currency units on average. Mean squared error penalizes large misses more strongly, so it is useful when big errors are especially costly.

Common regression evaluation metrics

Metric What it measures When to use it
MAE Average absolute difference between predicted and actual values Best for easy-to-interpret error in target units
MSE Average squared difference between predicted and actual values Useful when large errors should be punished heavily
RMSE Square root of MSE, returned to target units Helpful when you want sensitivity to large errors and readable units
R² Proportion of variance explained by the model Useful for comparing against a simple baseline

To make predictions, pass the prepared feature matrix to model.predict(): predictions = model.predict(X_test). Keras usually returns a two-dimensional array shaped like (num_samples, 1) for single-output regression, so you may flatten it with predictions = predictions.flatten() before building reports or plots. If the target variable was scaled during preprocessing, transform predictions back to the original scale with the same scaler used for y_train. Do not interpret scaled predictions directly unless the scaled unit is meaningful for the task.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A practical evaluation step is to inspect individual predictions beside actual values. For example, create a small comparison table with the true target, predicted target, and absolute error. This often reveals patterns that aggregate metrics hide, such as consistently underestimating expensive homes, overpredicting low-demand products, or struggling with rare high-value cases. A scatter plot of actual versus predicted values is also useful: strong predictions cluster near the diagonal line, while curved patterns or wide spread suggest missing features, inadequate model capacity, or target distribution issues.

  • Check a baseline: compare the neural network against a simple mean, median, or linear regression model.
  • Inspect residuals: calculate residuals = y_test - predictions and look for skew, outliers, or structure.
  • Review error by segment: measure MAE across ranges, categories, regions, or time periods when those fields are available.
  • Validate output scale: confirm predictions are in the expected units and have been inverse-transformed if scaling was applied.

Once the metrics and prediction samples look reasonable, the model can be used on new data that goes through the same preprocessing pipeline as the training data. The order of columns, scaling parameters, encoding scheme, and missing-value handling must match exactly. A regression model is only as reliable as the consistency between training inputs and production inputs, so saving the preprocessing objects together with the trained Keras model is a good practice for repeatable predictions.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Improving Model Accuracy with Tuning and Regularization

Once a Keras regression model is training and producing predictions, the next step is to improve generalization: the model should perform well on unseen data, not only on the training set. Start by comparing the training loss and validation loss across epochs. If both losses remain high, the model may be underfitting and needs more capacity, better features, a different learning rate, or more training time. If training loss keeps falling while validation loss rises, the model is overfitting and needs regularization, simpler architecture, or more data.

Adjusting the model architecture

For tabular regression problems, a practical starting point is a small dense network, then expand only when validation performance improves. Try changing the number of hidden layers, the number of units per layer, and the activation function. For example, a model with two hidden layers of 64 and 32 units may work better than one large layer of 256 units because it can learn hierarchical patterns without adding too many parameters. ReLU is a common activation for hidden layers, while the final regression layer usually has one unit and no activation so it can output any continuous value.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • More units or layers: can help when the model underfits and cannot capture the relationship between features and target values.
  • Fewer units or layers: can help when the model memorizes the training data and performs poorly on validation data.
  • Batch size: smaller batches can add useful noise to training, while larger batches may train faster but sometimes generalize less well.
  • Learning rate: one of the most sensitive settings; values such as 0.001, 0.0005, and 0.0001 are common candidates with Adam.

Using regularization techniques

Regularization reduces overfitting by limiting how freely the network can adapt to the training data. In Keras, L2 regularization adds a penalty for large weights, which encourages smoother models. Dropout randomly disables a fraction of units during training, forcing the network to avoid relying too heavily on any single path. For regression on structured data, dropout values between 0.1 and 0.3 are often a reasonable range to test. Too much dropout can hurt accuracy, especially on smaller networks.

Technique Use case Typical setting
L2 regularization Validation loss rises while training loss falls 0.001 or 0.0001
Dropout Dense network overfits on tabular data 0.1 to 0.3
Early stopping Best validation score occurs before final epoch Patience of 10 to 30 epochs
Learning rate reduction Validation loss plateaus Reduce by 0.2 to 0.5

Callbacks are especially useful during tuning. EarlyStopping can stop training when validation loss stops improving and restore the best weights from the run. ReduceLROnPlateau lowers the learning rate when progress stalls, allowing the optimizer to make smaller updates near a good solution. These callbacks often improve results without changing the model architecture.

Feature engineering can be just as effective as neural network tuning. Check for skewed numeric variables, missing values, outliers, and weak predictors. Standardizing numeric inputs is usually necessary for stable training, and transforming the target can help when values are highly skewed. For example, predicting the logarithm of house prices instead of raw prices can reduce the effect of extreme values; predictions can then be converted back with the inverse transform. Track each experiment with its validation MAE, RMSE, and final test score so model changes are based on measured performance rather than guesswork.

Frequently Asked Questions

Do I need to scale my input data for Keras regression?

Yes, scaling is usually necessary because neural networks train more reliably when input features are on similar ranges. StandardScaler or MinMaxScaler from scikit-learn are common choices. Fit the scaler only on the training data, then apply the same transformation to validation and test data.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What should the output layer look like for a Keras regression model?

For a single continuous target, use one output neuron with no activation function, such as Dense(1). For mulle continuous targets, use one neuron per target, such as Dense(3) for three values. Avoid classification activations like sigmoid or softmax unless your target has a bounded range and you have handled the target scaling accordingly.

Which loss function should I use for regression in Keras?

Mean squared error is a common default because it penalizes large errors strongly. Mean absolute error can be better when your dataset has outliers because it is less sensitive to very large mistakes. Huber loss is a practical middle ground and often works well when you want stable training with some resistance to outliers.

How can I tell if my regression model is overfitting?

Overfitting usually appears when training loss keeps decreasing while validation loss stops improving or starts increasing. You can monitor this by plotting the training history returned by model.fit(). Common fixes include early stopping, dropout, L2 regularization, reducing model size, or collecting more training data.

How do I convert Keras regression predictions back to the original units?

If you scaled the target variable before training, the model predictions will also be in the scaled target range. Use the target scaler’s inverse_transform() method to convert predictions back to the original units. Keep a separate scaler for the target if you scaled both features and labels.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Bottom Line

Regression with Keras gives you a flexible way to predict continuous values by combining clean data preparation, a suitable neural network architecture, and a reliable training workflow. Once you can load data, scale features, train a model, evaluate error metrics, and generate predictions, you have the core skills needed for many real-world forecasting and estimation tasks.

Your next step is to experiment with the model: adjust layers and neurons, tune the learning rate, add regularization, try callbacks like early stopping, and compare results against a simple baseline. Keep measuring performance on unseen data so every improvement is guided by evidence rather than guesswork.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.