Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Batch Prediction vs Real-Time Serving in MLOps
📖 Scenario: You work as a machine learning engineer in a company that predicts customer churn. You want to compare two ways to get predictions from your model: batch prediction and real-time serving.Batch prediction means you run the model on many customers at once, like a nightly job. Real-time serving means you get predictions instantly when a customer interacts with your app.
🎯 Goal: Build a simple Python program that simulates batch prediction and real-time serving using a dummy model. You will create data, configure a threshold, apply prediction logic, and print the results.
📋 What You'll Learn
Create a list of customer IDs and their feature values
Set a prediction threshold variable
Write a function to simulate model prediction
Use batch prediction to predict for all customers
Use real-time serving to predict for one customer
Print both batch and real-time prediction results
💡 Why This Matters
🌍 Real World
Companies use batch prediction to process large amounts of data overnight, saving resources. Real-time serving is used when instant decisions are needed, like fraud detection or personalized recommendations.
💼 Career
Understanding batch vs real-time prediction is key for MLOps engineers to design efficient and responsive machine learning systems.
Progress0 / 4 steps
1
Create customer data list
Create a list called customers with these exact entries: (101, 0.2), (102, 0.8), (103, 0.5), (104, 0.9). Each tuple has a customer ID and a feature value.
MLOps
Hint
Use a list of tuples with the exact customer IDs and feature values.
2
Set prediction threshold
Create a variable called threshold and set it to 0.6.
MLOps
Hint
Just assign 0.6 to the variable named threshold.
3
Write prediction function and batch prediction
Define a function called predict that takes a feature value and returns True if the feature is greater than threshold, otherwise False. Then create a list called batch_results using a list comprehension that applies predict to each customer's feature in customers.
MLOps
Hint
Use a function with a simple if check and a list comprehension to apply it to all customers.
4
Print batch and real-time prediction results
Print the batch_results list. Then print the prediction result for customer ID 105 with feature value 0.7 using the predict function (simulate real-time serving).
MLOps
Hint
Use print statements to show the batch list and the single prediction result.
Practice
(1/5)
1. What is the main difference between batch prediction and real-time serving in machine learning?
easy
A. Batch prediction is faster than real-time serving for single inputs.
B. Real-time serving is used only for training models.
C. Batch prediction processes many inputs at once, while real-time serving processes one input at a time.
D. Batch prediction requires internet connection, real-time serving does not.
Solution
Step 1: Understand batch prediction
Batch prediction processes a large number of inputs together, usually offline or in scheduled jobs.
Step 2: Understand real-time serving
Real-time serving handles one input at a time to provide instant predictions.
Final Answer:
Batch prediction processes many inputs at once, while real-time serving processes one input at a time. -> Option C
Quick Check:
Batch = many inputs, Real-time = one input [OK]
Hint: Batch = many inputs; real-time = one input fast [OK]
Common Mistakes:
Confusing batch with real-time speed
Thinking real-time is for training
Assuming batch needs internet
2. Which of the following is the correct way to describe real-time serving in a sentence?
easy
A. Real-time serving provides predictions instantly for each individual input.
B. Real-time serving delays predictions until batch processing is complete.
C. Real-time serving is only used for model training.
D. Real-time serving processes data in large groups at scheduled times.
Solution
Step 1: Identify real-time serving purpose
Real-time serving is designed to give instant predictions for each input as it arrives.
Step 2: Eliminate incorrect options
Options A, B, and C describe batch or training, not real-time serving.
Final Answer:
Real-time serving provides predictions instantly for each individual input. -> Option A
Quick Check:
Instant prediction per input = real-time serving [OK]
Hint: Real-time = instant single input prediction [OK]
Common Mistakes:
Mixing batch processing with real-time
Thinking real-time is for training
Confusing delay with instant response
3. Consider this Python pseudocode for batch prediction and real-time serving:
def batch_predict(data_list):
return [model.predict(x) for x in data_list]
def real_time_predict(single_input):
return model.predict(single_input)
batch_result = batch_predict([1, 2, 3])
real_time_result = real_time_predict(4)
print(batch_result, real_time_result)
What will be printed?
medium
A. pred1 pred2 pred3 pred4
B. [pred1, pred2, pred3] pred4
C. [pred1, pred2, pred3, pred4] None
D. Error because batch_predict expects a single input
Solution
Step 1: Understand batch_predict output
batch_predict returns a list of predictions for each input in data_list, so batch_result is a list [pred1, pred2, pred3].
Step 2: Understand real_time_predict output
real_time_predict returns a single prediction for the single input 4, so real_time_result is pred4.
Final Answer:
[pred1, pred2, pred3] pred4 -> Option B
Quick Check:
Batch returns list, real-time returns single prediction [OK]
Hint: Batch returns list; real-time returns single value [OK]
Common Mistakes:
Thinking batch returns single prediction
Confusing print output format
Assuming error due to input type
4. You have this code snippet for real-time serving:
def real_time_predict(input):
predictions = []
for x in input:
predictions.append(model.predict(x))
return predictions
result = real_time_predict(5)
print(result)
What is the error and how to fix it?
medium
A. Error: input is not iterable; fix by passing a list like [5].
B. Error: model.predict is undefined; fix by importing model.
C. No error; code runs correctly.
D. Error: predictions list is not returned; fix by adding return statement.
Solution
Step 1: Identify input type issue
The function expects input to be iterable (like a list), but 5 is an integer and not iterable.
Step 2: Fix by passing iterable
Passing [5] (a list with one element) makes the loop work correctly.
Final Answer:
Error: input is not iterable; fix by passing a list like [5]. -> Option A
Quick Check:
Non-iterable input causes error [OK]
Hint: Check if input is iterable for loops [OK]
Common Mistakes:
Passing single value instead of list
Ignoring error message about iteration
Assuming model.predict missing
5. A company wants to predict customer churn. They have 1 million customers and want to update predictions once a day. They also want to offer instant offers to customers calling support. Which approach fits best?
hard
A. Use batch prediction for support calls and real-time serving for daily updates.
B. Use only real-time serving for all predictions to keep data fresh.
C. Use batch prediction only and ignore real-time serving.
D. Use batch prediction once a day for all customers, and real-time serving for support calls.
Solution
Step 1: Analyze batch prediction use case
Predicting churn for 1 million customers once a day fits batch prediction well because it handles large data offline.
Step 2: Analyze real-time serving use case
Instant offers during support calls require quick predictions, so real-time serving is best.
Final Answer:
Use batch prediction once a day for all customers, and real-time serving for support calls. -> Option D
Quick Check:
Batch for bulk daily, real-time for instant [OK]
Hint: Batch for bulk jobs; real-time for instant needs [OK]