Machine Learning
Unit 6: Advanced Topics & MLOps (Bonus) / MLOps & Model Deployment
4. MLOps & Model Deployment
A machine learning model is useless if it sits isolated in a Jupyter Notebook. MLOps (Machine Learning Operations) focuses on reliably deploying, maintaining, and monitoring models in production environments.
1. Model Serialization
Once trained, a model must be saved to disk so it can be loaded later without retraining. In Python, this is typically done using the pickle or joblib libraries to serialize the model object into a binary `.pkl` file.
Deep Dive: Serving the Model via REST API
To let other applications (like web or mobile frontends) use the model, it is wrapped in an API (commonly using Flask or FastAPI).
from flask import Flask, request, jsonify
app = Flask(__name__)
model = pickle.load(open('model.pkl', 'rb'))
@app.route('/predict', methods=['POST'])
def predict():
data = request.get_json()
prediction = model.predict([data['features']])
return jsonify({'prediction': prediction.tolist()})
2. Containerization (Docker)
Deploying raw Python scripts can lead to "it works on my machine" errors due to dependency mismatches. Docker encapsulates the Model, API, Python runtime, and exact library versions (e.g., scikit-learn v1.0.2) into an isolated Container that runs identically anywhere.
3. Model Monitoring
Models degrade over time as the real world changes. Concept Drift occurs when the statistical properties of the target variable change (e.g., a fraud detection model trained in 2019 fails in 2021 due to new fraud tactics). Constant monitoring and retraining pipelines are essential.