Saving Models

Python · Machine Learning (Scikit-Learn)

Theory

Training can take minutes or hours, so you save the fitted model and load it later to predict without retraining. scikit-learn models are saved with joblib: joblib.dump(model, 'loan_model.joblib') writes the object to a file and joblib.load('loan_model.joblib') restores it, ready for predict. Save the whole Pipeline, not just the final estimator, so the exact same imputing, encoding and scaling are applied to new data.

Two cautions. A saved file is tied to the library versions that created it, so load it with the same scikit-learn version and record the versions alongside the file. And joblib and pickle files can run arbitrary code when loaded, so never load a model file from an untrusted source. Store useful metadata with the model too, such as the feature names, training date and test score.

Why we use it

Saving separates training from serving: you train once, then a web app, batch job or teammate loads the model and predicts instantly. Saving the pipeline guarantees predictions use exactly the preprocessing that was evaluated.

When to use it

Save after final evaluation, whenever a model will be reused or deployed. Keep the scikit-learn version with the file and retrain or re-save when you upgrade. Do not load model files you did not create or cannot trust.

How it works

  1. Model evaluated and final
  2. Bundle the pipeline with metadata (features, version)
  3. joblib.dump(bundle, 'model.joblib')
  4. Is the file from a trusted source and same sklearn version? Yes: joblib.load and predict. No: Retrain instead of loading.
  5. Predictions without retraining

Syntax

import joblib
joblib.dump(pipeline, 'model.joblib')
model = joblib.load('model.joblib')
model.predict(X_new)

Example

joblib.dump(clf, 'loan_model.joblib')
loaded = joblib.load('loan_model.joblib')
print(loaded.predict(X_test[:5]))

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic