DSPython Logo DSPython

Model Deployment: From Notebook to Product

Learn how to save, load, and serve your trained machine learning models.

Machine Learning Advanced 60 min

Topic 1: What is Model Deployment?

Model Deployment is the process of taking your trained machine learning model and making it available to users or other systems in a live environment.

A model that only exists in your Jupyter Notebook is not useful to anyone. Deployment is what turns your data science project into a real-world product.


Analogy: The Restaurant

  • Training a Model: This is like being a chef in a test kitchen, creating and perfecting a new recipe (the `.fit()` process).
  • Deployment: This is opening a restaurant. You take your finished recipe (the trained model artifact) and build a system (the kitchen, waiters, and cash register) so that customers can send in orders (new data) and receive a finished dish (a prediction).

The most common way to deploy a model is as an **API** (Application Programming Interface). Your app sends new data (e.g., `[[5.1, 3.5, 1.4, 0.2]]`) to a URL, and the API sends back the prediction (e.g., `{"species": 0}`).


Topic 2: Saving & Loading Scikit-Learn Models (Joblib)

When you train a scikit-learn model, all its "learning" (the coefficients, the tree structure, etc.) is stored in the model object in your computer's RAM. If you close the program, it's gone.

To use it in a product, you must first **serialize** it—save it to a file. The best library for this is `joblib`.

Why `joblib` and not `pickle`?

You might see `pickle` used in other Python tutorials. `joblib` is a part of the SciPy ecosystem and is **much more efficient** at saving large NumPy arrays, which are the core of scikit-learn models. It's the recommended standard.

Saving (Dumping) a Model

After you've trained your model, you use `joblib.dump()`.

from sklearn.linear_model import LogisticRegression
import joblib

# 1. Train your model as usual
model = LogisticRegression()
model.fit(X_train, y_train)

# 2. Save the trained model to a file
joblib.dump(model, 'my_model.joblib')

Loading (Loading) a Model

In your deployment script (e.g., your Flask API), you load the model *once* when the app starts using `joblib.load()`.

import joblib

# 1. Load the model from the file
loaded_model = joblib.load('my_model.joblib')

# 2. Use it to make new predictions
new_data = [[...]]
prediction = loaded_model.predict(new_data)

Topic 3: Saving the Scaler (The Critical Forgottep Step)

This is the most common mistake in deployment. Your model was trained on *scaled* data. This means your live API **must** scale new, incoming data in the *exact same way*.

You must save your `StandardScaler` object right after you `.fit()` it to your training data.

from sklearn.preprocessing import StandardScaler
import joblib

# 1. Create and FIT the scaler on TRAINING data
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
# X_test_scaled = scaler.transform(X_test) # Note: .transform() on test data

# 2. Save the FITTED scaler
joblib.dump(scaler, 'my_scaler.joblib')

# --- In your API ---
# 1. Load BOTH the model and the scaler
loaded_model = joblib.load('my_model.joblib')
loaded_scaler = joblib.load('my_scaler.joblib')

# 2. Scale the new, raw data using the loaded scaler
new_raw_data = [[5.1, 3.5, 1.4, 0.2]]
new_scaled_data = loaded_scaler.transform(new_raw_data)

# 3. Predict on the scaled data
prediction = loaded_model.predict(new_scaled_data)

Topic 4: Saving & Loading Keras (TensorFlow) Models

Keras models are more complex. They have two parts: the **architecture** (the layers) and the **weights** (the learned parameters). Keras provides a simple, built-in way to save *everything*.

The modern, recommended way is to save to a directory. The older, single-file method is `.h5` (HDF5 format).

Saving a Keras Model

After training, use `model.save()`. It saves the architecture, the trained weights, and even the optimizer's state.

# 1. Build, compile, and fit your Keras model
model.fit(X_train_scaled, y_train, epochs=10)

# 2. Save everything into a single file
model.save('my_keras_model.h5')

Loading a Keras Model

In your API, you use `keras.models.load_model()`.

from tensorflow import keras

# 1. Load the entire model
loaded_model = keras.models.load_model('my_keras_model.h5')

# 2. The model is fully compiled and ready to use
new_scaled_data = [[...]]
prediction_probs = loaded_model.predict(new_scaled_data)