Neural Sync Active
Learning Objectives
Registry Synced
Learning Objectives
315 words
2 min read
Learning Objectives
- Save and load trained model
- Create prediction API with Flask
- Deploy model to cloud
python# Save model import joblib joblib.dump(best_model, 'models/model.pkl') joblib.dump(scaler, 'models/scaler.pkl') joblib.dump(le, 'models/label_encoder.pkl') # Prediction API (app.py) from flask import Flask, request, jsonify import joblib import pandas as pd import numpy as np app = Flask(__name__) model = joblib.load('models/model.pkl') scaler = joblib.load('models/scaler.pkl') @app.route('/predict', methods=['POST']) def predict(): data = request.get_json() df = pd.DataFrame([data]) df_scaled = scaler.transform(df) prediction = model.predict(df_scaled) probability = model.predict_proba(df_scaled)[0] return jsonify({ 'prediction': int(prediction[0]), 'probability': float(probability[1]), 'class': 'Positive' if prediction[0] == 1 else 'Negative' }) if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)
Q1: What is serialization and why needed?Saving trained model state (parameters, structure) to file. Enables reloading without retraining. joblib (sklearn), pickle, ONNX, PMML. Q2: How to handle model versioning?Use MLflow or DVC. Tag models with version (v1.0), track parameters, metrics, and training data. Can rollback to previous versions. Q3: What is A/B testing for models?Serve old and new model simultaneously. Compare metrics (conversion, accuracy). Gradually shift traffic to better model. Validates performance in production. Q4: How to monitor model in production?Track prediction distribution, feature drift, label drift, response time, error rate. Set alerts for anomalies. Retrain when performance degrades. Q5: Deployment options?Flask API on Heroku/AWS/GCP, FastAPI, Streamlit (demo), MLflow serving, Docker container, serverless (AWS Lambda). Q6: Docker deployment:dockerfileFROM python:3.9-slim WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . EXPOSE 5000 CMD ["python", "app.py"]Q7: Test the API:bashcurl -X POST http://localhost:5000/predict \ -H "Content-Type: application/json" \ -d '{"feature1": 5.1, "feature2": 3.5, "feature3": 1.4}'