Skip to main content

Chapter 34: Introduction to MLOps

MODULE 6 | MLOps​

Chapter 34: Introduction to MLOps​

This chapter is the foundation of the whole module. We will see what MLOps is , why a notebook model does not work in production , what the whole life (lifecycle) of an ML project looks like, and which problem each tool in this module solves. At the end we will learn how to save and load a model, because that is the first step of deployment.

TOPIC CHECKLIST (tick them off as you go)​

  • What MLOps is and why it matters

  • ☐

  • ☐ The difference between a notebook and production

  • ☐ The ML lifecycle: data, training, deployment, monitoring

  • ☐ Map of this module's tools (roadmap)

  • ☐ Saving and loading a model with joblib

  • ☐ Setting up the project folder and virtual environment

34.1 What is MLOps and why does it matter?​

MLOps (Machine Learning Operations) is the name for the methods and tools by which an ML model is built, run and maintained in a reliable way . It is a blend of three worlds: Machine Learning (building the model), DevOps (running software without problems) and Data Engineering (keeping data clean and ready).

34.2 The Difference Between a Notebook and Production​

ThingNotebook (your computer)Production (the real world)
Who uses itOnly youThousands of people, apps, other systems
How the model runsRun a cell, see the answerThrough an API,24 hours a day, automatically
EnvironmentYour library versionsMust bethe sameeverywhere (Docker)
When something goes
wrong
Run the cell againCheck the logs, get an alert, roll back
When the model gets
outdated
You never find outYou find out throughmonitoring
Record of experimentsMemory or notebook commentsParameters and metrics inMLflow

The most famous sentence: "It was working on my computer." The goal of MLOps is to put an end to this sentence.

34.3 The ML Lifecycle​

An ML project is never built once and finished . It keeps running in a cycle . Here are its stages:

DIAGRAM​
DATA -> TRAINING -> EVALUATION -> DEPLOYMENT -> MONITORING
DATA -> TRAINING -> EVALUATION -> DEPLOYMENT -> MONITORING
^ |
|________________ new data, new model ___________________|
StageWhat happensTool in this module
DataCollect, clean and version the dataModules 1 to 4 (pandas, sklearn)
TrainingTrain the model, keep a record of experimentsMLflow(Chapter 37)
EvaluationCheck accuracy on test data, compare modelsMLflow UI, pytest (Chapters 37, 38)
DeploymentRun the model as an API, pack it in a boxFastAPI,Docker(Chapters 35, 36)
AutomationAutomatic tests and build on every changeGitHub Actions(Chapter 38)
MonitoringHealth of the live model, logs, data driftlogging, drift check (Chapter 39)

34.4 The First Step: Saving and Loading a Model​

Before running a model in an API or Docker, you have to save it to a file . Otherwise training would have to be repeated every time. For scikit-learn models, the most common way is joblib :

import joblib
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
X, y = load_iris(return_X_y=True)
model = RandomForestClassifier(n_estimators=50, random_state=42)
model.fit(X, y)
joblib.dump(model, "iris_model.joblib")
print("Model saved")
loaded = joblib.load("iris_model.joblib")
sample = [[5.1, 3.5, 1.4, 0.2]]
print("Prediction:", loaded.predict(sample)[0])
OUTPUT​
Model saved
Prediction: 0

Prediction 0 means setosa (the first species of Iris). Note this: the model file is trained only once , and after that the API or Docker only loads it.

34.5 Project Setup: Folder and Virtual Environment​

Make a separate virtual environment for every project so that libraries do not clash with each other. Do this in the terminal:

TERMINAL​
mkdir mlops-practice
cd mlops-practice
python -m venv venv
# Windows:
venv\Scripts\activate
# Mac / Linux:
source venv/bin/activate
pip install fastapi uvicorn scikit-learn joblib pandas httpx pytest mlflow
pip freeze > requirements.txt
VERSION NOTE​

This module uses Python 3.12 . Some libraries (such as TensorFlow ) do not yet support very new Python versions (for example 3.14), and newer library versions can bring small changes. If any code gives an error, first check python --version and pip list. In this module, the code was tested with FastAPI 0.159, scikit-learn 1.8, MLflow 3.17, pandas 3.0 .

34.6 Common Mistakes and Hands-on​

Hands-on​

  • Save the model of one of your old projects with joblib, and load it in a new notebook and make one prediction.

  • Make a mlops-practice folder and a virtual environment, and run pip freeze > requirements.txt.

  • For one of your old projects, write: which stage of the lifecycle has it reached (data, training, deployment, monitoring)?

  • Write in one sentence: why is "It works on my computer" not enough in production?

KEY TAKEAWAYS​
  • MLOps = ML + DevOps + Data Engineering: building, running and maintaining a model in a reliable way .

  • In a notebook only you run the model; production needs an API, 24 hours a day, and the same environment .

  • The ML lifecycle is a cycle : Data, Training, Evaluation, Deployment, Monitoring, then back to Data.

  • joblib.dump / joblib.load save and load the model; load only trusted files .

  • Write versions in requirements.txt so that the same environment is built everywhere.