Making Predictions
After a neural network has been trained, we can give it new input data and ask it to make predictions. In Keras, we usually use model.predict() for this.
What Is a Prediction?
A prediction is the answer produced by a trained neural network when we give it new input data.
For example, suppose we trained a model to predict whether a student will pass an exam based on study hours.
Study Hours
↓
Trained Neural Network
↓
Prediction
↓
Pass / Fail
If a new student studies for 6 hours, we can ask the model:
Input = 6 hours
Model
↓
Prediction = 1
If 1 means Pass, the model predicts that the student will pass.
Training vs Prediction
Do not confuse these two operations.
Training
model.fit()
↓
Learn weights
↓
Improve the model
Prediction
model.predict()
↓
Use learned weights
↓
Produce an answer
During prediction, we normally do not update the model's weights. The trained model is simply being used to produce an output.
Using model.predict()
The basic syntax is:
predictions = model.predict(X_new)
Here:
X_new
↓
New input data
model.predict()
↓
Ask the trained model for predictions
predictions
↓
Model's output
Simple Example
Suppose we have already trained a model using study hours.
import tensorflow as tf
import numpy as np
# Create model
model = tf.keras.Sequential([
tf.keras.layers.Input(shape=(1,)),
tf.keras.layers.Dense(
8,
activation="relu"
),
tf.keras.layers.Dense(
1,
activation="sigmoid"
)
])
# Compile model
model.compile(
optimizer="adam",
loss="binary_crossentropy",
metrics=["accuracy"]
)
# Training data
X_train = np.array([
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8]
])
y_train = np.array([
0,
0,
0,
1,
1,
1,
1,
1
])
# Train
model.fit(
X_train,
y_train,
epochs=20,
verbose=0
)
# New data
X_new = np.array([
[6]
])
# Make prediction
prediction = model.predict(X_new)
print(prediction)
The important part is:
X_new = np.array([
[6]
])
prediction = model.predict(X_new)
We are asking:
"The student studied for 6 hours.
What is the probability that the student will pass?"
Why Does Prediction Return a Number Like 0.9?
Our output layer uses:
activation="sigmoid"
Sigmoid produces a value between 0 and 1.
0.0 ─────────────── 1.0
↓ ↓
Low probability High probability
Suppose the model returns:
[[0.92]]
We can interpret this as approximately:
92% probability of class 1
If class 1 means "Pass", the model is strongly predicting Pass.
Converting Probability Into a Class
A sigmoid model gives us a probability. Sometimes we want to convert that probability into a simple class such as 0 or 1.
A common threshold is 0.5.
Prediction >= 0.5
↓
Class 1
Prediction < 0.5
↓
Class 0
For example:
Prediction = 0.90
0.90 >= 0.5
Result = 1
Another example:
Prediction = 0.20
0.20 < 0.5
Result = 0
Convert Prediction to 0 or 1 in Python
prediction = model.predict(X_new)
class_prediction = (
prediction >= 0.5
).astype(int)
print(class_prediction)
Suppose:
prediction = [[0.92]]
Then:
0.92 >= 0.5
↓
True
↓
1
So the final result becomes:
[[1]]
Making Multiple Predictions
We don't have to predict only one example at a time. We can give the model multiple examples.
X_new = np.array([
[2],
[4],
[6],
[8]
])
predictions = model.predict(X_new)
print(predictions)
The model might produce something like:
[[0.08]
[0.35]
[0.82]
[0.96]]
These values represent the model's estimated probability for class 1 for each input.
2 hours → 0.08
4 hours → 0.35
6 hours → 0.82
8 hours → 0.96
Using a threshold of 0.5:
2 hours → 0
4 hours → 0
6 hours → 1
8 hours → 1
Real-World Example — Spam Detection
Imagine a neural network trained to classify emails as spam or not spam.
0 = Not Spam
1 = Spam
After training, we receive a new email.
New Email
↓
Convert Email Into Features
↓
Trained Neural Network
↓
Prediction = 0.91
Since the prediction is 0.91:
0.91 >= 0.5
↓
Class = 1
↓
Spam
The model is saying that this email has a high probability of belonging to the spam class.
Prediction in Regression
Not every neural network produces a probability.
In regression, the output can be a continuous number.
For example, suppose we train a model to predict house prices.
House Features
↓
Neural Network
↓
Predicted Price
The output might be:
325000.50
This is not a probability. It is the model's predicted numerical value.
The same model.predict() method is used:
prediction = model.predict(X_new)
print(prediction)
Complete Example — Binary Classification
import tensorflow as tf
import numpy as np
# -----------------------------
# Training data
# -----------------------------
X_train = np.array([
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8]
])
y_train = np.array([
0,
0,
0,
1,
1,
1,
1,
1
])
# -----------------------------
# Create model
# -----------------------------
model = tf.keras.Sequential([
tf.keras.layers.Input(shape=(1,)),
tf.keras.layers.Dense(
8,
activation="relu"
),
tf.keras.layers.Dense(
1,
activation="sigmoid"
)
])
# -----------------------------
# Compile
# -----------------------------
model.compile(
optimizer="adam",
loss="binary_crossentropy",
metrics=["accuracy"]
)
# -----------------------------
# Train
# -----------------------------
model.fit(
X_train,
y_train,
epochs=20,
verbose=0
)
# -----------------------------
# New data
# -----------------------------
X_new = np.array([
[6],
[2],
[8]
])
# -----------------------------
# Make predictions
# -----------------------------
predictions = model.predict(X_new)
print("Probabilities:")
print(predictions)
# -----------------------------
# Convert to classes
# -----------------------------
classes = (
predictions >= 0.5
).astype(int)
print("Classes:")
print(classes)
The important flow is:
Training Data
↓
model.fit()
↓
Model learns
↓
New Data
↓
model.predict()
↓
Probability
↓
Threshold
↓
Class 0 or 1
Understand the Python Code
X_new = np.array([
[6]
])
Creates new input data that the trained model has to classify.
prediction = model.predict(X_new)
Sends the new input through the trained neural network and gets its output.
prediction >= 0.5
Checks whether the predicted probability is at least 0.5.
.astype(int)
Converts Boolean values such as True and
False into 1 and 0.
print(classes)
Displays the final predicted class.
Important: Input Shape
The shape of new input data must match what the model expects.
If the model was created with:
tf.keras.layers.Input(shape=(1,))
it expects one feature for each example.
So this is appropriate:
X_new = np.array([
[6],
[8]
])
Each row represents one example, and each example has one feature.
[
[6], ← Example 1
[8] ← Example 2
]
Don't randomly reshape prediction data. The input shape must match the model's expected feature structure.
Prediction Is Not Guaranteed to Be Correct
A neural network prediction is an output from a learned model, not a guaranteed fact.
For example:
Prediction = 0.80
This means the model assigns a high probability to class 1. It does not mean the model is guaranteed to be correct.
The model can still make mistakes, especially when the new input is very different from the data it learned from.
Complete Neural Network Workflow
1. Create Model
↓
2. Add Layers
↓
3. Compile Model
↓
4. Train Model
↓
5. Make Predictions
↓
6. Evaluate Model
We have now reached the prediction stage.
model.fit()
↓
Learn
model.predict()
↓
Use what was learned
Remember This
model.predict(X_new)
This is the main function you need to remember for making predictions.
Training
model.fit()
↓
Model learns weights
Prediction
model.predict()
↓
Model uses learned weights
to produce an output
For binary classification with a sigmoid output:
0.0 → Strongly toward class 0
0.5 → Threshold commonly used for classification
1.0 → Strongly toward class 1
The complete idea is:
Train
↓
Learn Patterns
↓
Give New Data
↓
model.predict()
↓
Get Prediction
Check Your Understanding
Which function is used to make
predictions?
model.predict().
What is the difference between fit() and
predict()?
fit() trains the model and updates its
weights. predict() uses the trained model
to produce outputs.
Why might a binary classifier return
0.92?
Because a sigmoid output produces a value between 0 and
1, which can be interpreted as the model's estimated
probability for class 1.
How can 0.92 become class 1?
By applying a threshold such as 0.5:
0.92 >= 0.5, so the result is 1.
Can model.predict() guarantee the answer is
correct?
No. It produces the model's prediction based on what it
learned during training.
Can we make multiple predictions at once?
Yes. Pass multiple rows of input data to
model.predict().