Learn how to build a clean, professional inference script to generate predictions. Master model loading, data processing, and standardized output formats.
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.
You shouldn't perform predictions inside your training notebook. Notebooks are for experimentation; scripts are for reliability. A production-grade inference script ensures that:
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.
Assuming you have already saved your pipeline.joblib file, here is how you structure a clean, production-ready script.
PYTHONimport 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)
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.
predict.py in your project folder.generate_prediction function above into the file.model_path to point to the location where you saved your pipeline from the previous lesson.if __name__ == "__main__": block at the bottom of your script that calls the function with a dummy dictionary of features and prints the output.python predict.py to verify it loads and executes without errors.C:/Users/Name/...). Use relative paths or environment variables so your code works on any machine.predict step. Always validate your input types before passing them to the pipeline.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.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.
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 moreLearn how to instantiate, fit, and generate predictions with your first baseline linear model using Scikit-Learn to establish a performance benchmark.
Creating an Inference Script