Page17/22
Model Deployment & Production · Page 1 of 1
From Notebook to Production
Model Deployment
The ML Pipeline (Production)
1. Training (Jupyter Notebook)
↓
2. Serialize Model (Save to file)
↓
3. Load Model in API (Flask/FastAPI)
↓
4. Accept HTTP Requests
↓
5. Preprocess Input Data
↓
6. Make Prediction
↓
7. Return JSON Response
↓
8. Monitor & Retrain (if performance drops)
Saving Models
Pickle (General Python Objects)
import pickle
# Save
with open('model.pkl', 'wb') as f:
pickle.dump(model, f)
# Load
with open('model.pkl', 'rb') as f:
model = pickle.load(f)
Joblib (Large NumPy Arrays)
import joblib
# Save
joblib.dump(model, 'model.joblib')
# Load
model = joblib.load('model.joblib')
Creating an API with Flask
from flask import Flask, request, jsonify
import joblib
app = Flask(__name__)
model = joblib.load('model.joblib')
scaler = joblib.load('scaler.joblib')
@app.route('/predict', methods=['POST'])
def predict():
data = request.json['features'] # e.g., [[5.1, 3.5, 1.4, 0.2]]
# Preprocess
data_scaled = scaler.transform(data)
# Predict
pred = model.predict(data_scaled)
# Return
return jsonify({'prediction': int(pred[0])})
if __name__ == '__main__':
app.run(port=5000)
Containerization with Docker
FROM python:3.9
WORKDIR /app
COPY model.joblib .
COPY app.py .
RUN pip install flask scikit-learn
EXPOSE 5000
CMD ["python", "app.py"]
Build and run:
docker build -t my-ml-api .
docker run -p 5000:5000 my-ml-api
Deployment Platforms
| Platform | Best For | Cost |
|---|---|---|
| Heroku | Hobby projects | Free-$50/month |
| AWS Lambda | Serverless, low traffic | Pay-per-request |
| Google Cloud | High traffic | Flexible |
| Azure | Enterprise | Enterprise pricing |
Production Considerations
- Input validation: Reject bad inputs
- Monitoring: Track prediction accuracy, latency
- Logging: Save all requests for debugging
- Caching: Store recent predictions
- A/B testing: Compare old vs new model
- Retraining: Schedule automatic retraining
main.py
Loading...
OUTPUT
▶Click "Run Code" to execute…