Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogCoursesPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Courses
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 38 of the AI/ML Foundations: Core Concepts & First Models course
AI/MLJune 25, 20263 min read

Creating an Inference Script: A Practical Guide for Production

Learn how to build a clean, professional inference script to generate predictions. Master model loading, data processing, and standardized output formats.

AI/MLPythonMachine LearningDeploymentInferenceScikit-Learnaimachine-learning

Previously in this course, we explored Exporting Trained Models: Serialization with Pickle and Joblib to save our progress. Now that you have a serialized model file, the next logical step in the The Machine Learning Workflow: From Data to Deployment is to build an inference script.

An inference script is the bridge between your static model file and the real world. It turns raw input data into actionable insights by handling the loading, pre-processing, and prediction logic in a reproducible way.

Why You Need a Dedicated Inference Script

You shouldn't perform predictions inside your training notebook. Notebooks are for experimentation; scripts are for reliability. A production-grade inference script ensures that:

  1. Consistency: The same preprocessing steps (scaling, encoding) applied during training are applied to new data.
  2. Standardization: Your model outputs are formatted in a way that downstream systems (like a web API or a database) expect.
  3. Error Handling: You can catch malformed input data before it crashes your model.

Designing the Inference Function

A professional inference script should be modular. Instead of writing one giant block of code, we encapsulate the logic in a function that takes raw input and returns a prediction.

Worked Example: Building the Predictor

Assuming you have already saved your pipeline.joblib file, here is how you structure a clean, production-ready script.

PYTHON
import joblib
import pandas as pd
import logging

# Set up logging for production visibility
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def generate_prediction(input_data, model_path=CE9178">'model/pipeline.joblib'):
    CE9178">"""
    Loads a model and returns predictions for the given input.
    
    Args:
        input_data(dict or pd.DataFrame): The raw features for prediction.
        model_path(str): Path to the serialized pipeline.
        
    Returns:
        dict: A dictionary containing the prediction and status.
    """
    try:
        # 1. Load the model
        model = joblib.load(model_path)
        
        # 2. Ensure input is a DataFrame
        if isinstance(input_data, dict):
            input_data = pd.DataFrame([input_data])
            
        # 3. Predict (The pipeline handles all scaling/encoding)
        prediction = model.predict(input_data)
        
        return {
            "status": "success",
            "prediction": float(prediction[0]),
            "model_version": "v1.0"
        }
        
    except Exception as e:
        logger.error(f"Inference failed: {e}")
        return {"status": "error", "message": str(e)}

# Example Usage
new_sample = {"feature_1": 0.5, "feature_2": 1.2}
result = generate_prediction(new_sample)
print(result)

Integrating with the Project

For our ongoing project, we will create a file named predict.py in our root directory. This script will import our pipeline and expose the generate_prediction function. By keeping this logic separate, we can easily import this function later when we build a web interface or a scheduled batch job.

Hands-on Exercise

  1. Create a file named predict.py in your project folder.
  2. Copy the generate_prediction function above into the file.
  3. Update the model_path to point to the location where you saved your pipeline from the previous lesson.
  4. Write a small if __name__ == "__main__": block at the bottom of your script that calls the function with a dummy dictionary of features and prints the output.
  5. Run the script from your terminal using python predict.py to verify it loads and executes without errors.

Common Pitfalls to Avoid

  • Feature Mismatch: The most common error is providing a dictionary with different keys than what the model was trained on. Ensure your input keys match the column names of your training data exactly.
  • Hardcoding Paths: Avoid hardcoding absolute file paths (like C:/Users/Name/...). Use relative paths or environment variables so your code works on any machine.
  • Ignoring Data Types: If your model expects a float, passing a string will cause an error during the predict step. Always validate your input types before passing them to the pipeline.
  • Over-processing: Remember that if you saved a Scikit-Learn Pipeline, it already contains the encoders and scalers. Do not manually scale your data before passing it to the pipeline, or you will "double-scale" it and ruin your predictions.

Recap

An inference script is your model's public face. By wrapping the loading and prediction logic in a single function, you ensure that your ML system is modular, error-resistant, and ready for deployment. We’ve moved from training models in a notebook to creating a functional, repeatable tool.

Up next: Building a Simple Web Interface.

Previous lessonExporting Trained ModelsNext lesson Building a Simple Web Interface
Back to Blog

Similar Posts

AI/MLJune 25, 20263 min read

Building a Simple Web Interface for ML Models with Streamlit

Learn how to wrap your trained ML models in a web interface using Streamlit, enabling stakeholders to run predictions via a simple, interactive UI.

Read more
AI/MLJune 25, 20263 min read

Training the Baseline Linear Model: A Practical Guide

Learn how to instantiate, fit, and generate predictions with your first baseline linear model using Scikit-Learn to establish a performance benchmark.

Part of the course

AI/ML Foundations: Core Concepts & First Models

beginner · Lesson 38 of 50

  1. 1

    The Machine Learning Workflow

    4 min
  2. 2

    Setting Up the Python ML Environment

    4 min
  3. 3

    Introduction to NumPy for Data Handling

    4 min
Read more
AI/MLJune 25, 20264 min read

The Mechanics of Classification: Logic and Decision Boundaries

Classification is the foundation of predictive AI. Learn the logic behind categorizing data, defining decision boundaries, and solving real-world problems.

Read more
4

Loading and Inspecting Datasets with Pandas

3 min
  • 5

    Exploratory Data Analysis Fundamentals

    3 min
  • 6

    Handling Missing and Inconsistent Data

    3 min
  • 7

    Feature Selection and Basic Filtering

    3 min
  • 8

    Project Dataset Initialization

    3 min
  • 9

    Mechanics of Linear Regression

    4 min
  • 10

    Mechanics of Classification

    4 min
  • 11

    Loss Functions and Model Objectives

    4 min
  • 12

    Training and Testing Data Splits

    3 min
  • 13

    Data Scaling Techniques

    4 min
  • 14

    Encoding Categorical Variables

    3 min
  • 15

    Building Scikit-Learn Pipelines

    4 min
  • 16

    Training the Baseline Linear Model

    3 min
  • 17

    Training Error vs Generalization Error

    4 min
  • 18

    Overfitting and Underfitting

    4 min
  • 19

    Regression Evaluation Metrics

    4 min
  • 20

    The Confusion Matrix

    3 min
  • 21

    Error Analysis Plots

    4 min
  • 22

    Introduction to Cross-Validation

    4 min
  • 23

    Diagnosing Model Weaknesses

    3 min
  • 24

    Feature Engineering Strategies

    4 min
  • 25

    Handling Outliers

    3 min
  • 26

    The Bias-Variance Tradeoff

    3 min
  • 27

    Hyperparameter Tuning Basics

    4 min
  • 28

    Implementing Grid Search

    3 min
  • 29

    Refining the Project Model

    3 min
  • 30

    Evaluating Feature Importance

    3 min
  • 31

    Advanced Feature Transformation

    3 min
  • 32

    Regularization Techniques

    3 min
  • 33

    Comparing Different Algorithms

    3 min
  • 34

    Managing Model Complexity

    4 min
  • 35

    Understanding Data Drift

    4 min
  • 36

    Version Control for ML Experiments

    3 min
  • 37

    Exporting Trained Models

    3 min
  • 38

    Creating an Inference Script

    3 min
  • 39

    Building a Simple Web Interface

    3 min
  • 40

    Documenting ML Projects

    4 min
  • 41

    Final Project Review

    4 min
  • 42

    Ensemble Methods Overview

    4 min
  • 43

    Feature Selection via Recursive Elimination

    3 min
  • 44

    Model Interpretability Basics

    4 min
  • 45

    Dealing with High Cardinality

    3 min
  • 46

    Handling Multi-Collinearity

    4 min
  • 47

    Introduction to Pipelines with Custom Transformers

    3 min
  • 48

    Evaluating Model Calibration

    4 min
  • 49

    Advanced Hyperparameter Search

    3 min
  • 50

    Model Monitoring in Practice

    4 min
  • View full course