Keras
Machine Learning
Function Prediction
Neural Networks
Data Table

How to predict a function/table using Keras?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you have a table of input and output values and want Keras to learn the relationship, you are solving a regression problem. The model does not "discover a symbolic function" in the mathematical sense, but it can approximate the mapping from inputs to outputs well enough to predict unseen values.

This works both for simple one-dimensional functions such as y = sin(x) and for multi-column tabular data where several input features influence one numeric target. The workflow is always the same: prepare x and y, build a regression model, train it, and use predict.

Start with Input and Output Arrays

For a toy example, suppose you want the network to learn a noisy sine curve:

python
1import numpy as np
2import tensorflow as tf
3
4x = np.linspace(0, 2 * np.pi, 500).reshape(-1, 1)
5y = np.sin(x)

In a real table, x might have many columns:

python
1# Example shape: 1000 rows, 4 input features
2x = np.array([
3    [1.0, 2.0, 3.0, 4.0],
4    [2.0, 3.0, 4.0, 5.0],
5    [3.0, 4.0, 5.0, 6.0],
6], dtype=np.float32)
7
8y = np.array([10.5, 12.7, 14.1], dtype=np.float32)

The important rule is that each row in x must align with one target value in y.

Build a Regression Model

For function approximation and small tabular problems, a simple dense network is a good starting point:

python
1model = tf.keras.Sequential([
2    tf.keras.layers.Input(shape=(1,)),
3    tf.keras.layers.Dense(64, activation="relu"),
4    tf.keras.layers.Dense(64, activation="relu"),
5    tf.keras.layers.Dense(1),
6])
7
8model.compile(
9    optimizer="adam",
10    loss="mse",
11    metrics=["mae"],
12)

The final layer has one unit and no activation because this is regression. If you were predicting two continuous outputs, the final layer would use Dense(2).

Train the Model

Now fit the network on the table:

python
1history = model.fit(
2    x,
3    y,
4    epochs=100,
5    batch_size=32,
6    validation_split=0.2,
7    verbose=0,
8)

After training, predict values for inputs the model has not seen:

python
new_x = np.array([[0.5], [1.5], [2.5]], dtype=np.float32)
predictions = model.predict(new_x)
print(predictions)

That is the basic pattern for "predicting a function table" with Keras.

Normalize Tabular Inputs

For real-world tables, feature scaling often matters. Neural networks usually train more reliably when numeric inputs are normalized.

python
1normalizer = tf.keras.layers.Normalization(axis=-1)
2normalizer.adapt(x)
3
4model = tf.keras.Sequential([
5    tf.keras.layers.Input(shape=(x.shape[1],)),
6    normalizer,
7    tf.keras.layers.Dense(64, activation="relu"),
8    tf.keras.layers.Dense(64, activation="relu"),
9    tf.keras.layers.Dense(1),
10])
11
12model.compile(optimizer="adam", loss="mse", metrics=["mae"])

This is especially important when one column contains small values such as 0.01 and another contains large values such as 100000.

Use a Train/Test Split for Honest Evaluation

Do not judge the model only on the same rows it learned from. Hold out some data:

python
1from sklearn.model_selection import train_test_split
2
3x_train, x_test, y_train, y_test = train_test_split(
4    x, y, test_size=0.2, random_state=42
5)
6
7model.fit(x_train, y_train, epochs=50, validation_split=0.2, verbose=0)
8test_loss, test_mae = model.evaluate(x_test, y_test, verbose=0)
9print(test_loss, test_mae)

That tells you whether the network learned the relationship or merely memorized the examples.

Understand the Limits

Neural networks are good at interpolation within the range of data they have seen. They are often much worse at extrapolation beyond that range. If your training table covers inputs from 0 to 10, a prediction at 1000 may be meaningless even if the training loss looked excellent.

For very small, smooth, and well-understood functions, classical regression or interpolation can sometimes outperform a neural network with less complexity. Keras becomes attractive when the relationship is nonlinear, multi-dimensional, or hard to model analytically.

Common Pitfalls

  • Using a classification loss or activation for a regression problem.
  • Forgetting to reshape one-dimensional input into a two-dimensional feature matrix.
  • Training on unnormalized tabular data with wildly different numeric scales.
  • Evaluating only on training data and assuming the model generalizes.
  • Expecting strong extrapolation outside the region covered by the original table.

Summary

  • Predicting a function or numeric table in Keras is a regression task.
  • Prepare aligned input features and target values, then train a dense model.
  • Use a linear output layer and a regression loss such as mse.
  • Normalize tabular inputs and keep a separate test split.
  • Trust interpolation more than extrapolation unless you have strong evidence otherwise.

Course illustration
Course illustration

All Rights Reserved.