How to Run Batch Predictions with a Mock Model in Python

Build a lightweight mock model class and run predictions across a batch of samples, returning results as a plain Python list.

Easy Python 3.9+ Aug 9, 2026 ML engineering pipelines 14 views 0 copies

Requires third-party packages — install first
pip install numpy

Python code

23 lines
Python 3.9+
import numpy as np

class MockModel:
    def __init__(self, weights):
        self.weights = np.array(weights)

    def predict(self, X):
        return X @ self.weights

def predict_batch(model, batch):
    """Run predictions for a batch of samples and return results as a list."""
    return model.predict(np.array(batch)).tolist()

if __name__ == "__main__":
    model = MockModel([0.5, -1.2, 0.8])
    batch = [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]
    ]
    results = predict_batch(model, batch)
    print("Predictions:", results)
    print("Batch size:", len(results))

Output

stdout
Predictions: [0.5, 1.4, 2.3000000000000003]
Batch size: 3

How it works

The MockModel class wraps a simple linear transformation using NumPy's matrix multiplication. The predict_batch function converts the input batch into a NumPy array, applies the model's predict method, and then converts the result back to a list for easy consumption. This pattern mirrors how real ML models accept a matrix of features and return an array of predictions. Running the script under if __name__ == '__main__' ensures the demo only executes when the file is run directly, not when imported.

Common mistakes

  • Forgetting to convert the batch to a NumPy array before matrix multiplication
  • Assuming the predict method returns a list instead of a NumPy array
  • Mixing up the shape of weights — weights must match the number of features in each sample

Variations

  1. Use a library like scikit-learn's predict method for a more realistic model object
  2. Batch predictions in chunks to avoid loading all data into memory at once

Real-world use cases

  • Running inference on a batch of user feature vectors in an online recommendation system.
  • Scoring multiple input rows from a CSV before writing predictions back to storage.
  • Wrapping a mock or stub model in unit tests to verify batch data flow without heavy dependencies.

Sponsored

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from ML engineering pipelines

Related tutorials and quizzes for this topic.