0
0
ML Pythonml~20 mins

Why MLOps manages ML lifecycle in ML Python - Experiment to Prove It

Choose your learning style9 modes available
Experiment - Why MLOps manages ML lifecycle
Problem:You have built a machine learning model that works well on your local computer. But when you try to use it in real life, it breaks or becomes slow. You also find it hard to update the model or track changes.
Current Metrics:Model accuracy on test data: 85%, but deployment failures and slow updates cause downtime and user complaints.
Issue:The model lifecycle is not managed well. There is no system to automate training, testing, deployment, monitoring, and updating. This causes errors, delays, and poor user experience.
Your Task
Set up a simple MLOps pipeline that automates model training, testing, deployment, and monitoring to improve reliability and update speed.
Use only open-source tools or simple scripts.
Keep the pipeline easy to understand and maintain.
Do not change the model architecture or data.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
ML Python
import os
import joblib
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

# Step 1: Load data
def load_data():
    data = load_iris()
    X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
    return X_train, X_test, y_train, y_test

# Step 2: Train model
def train_model(X_train, y_train):
    model = RandomForestClassifier(random_state=42)
    model.fit(X_train, y_train)
    return model

# Step 3: Evaluate model
def evaluate_model(model, X_test, y_test):
    preds = model.predict(X_test)
    acc = accuracy_score(y_test, preds)
    print(f"Validation Accuracy: {acc:.2f}")
    return acc

# Step 4: Save model
def save_model(model, path="model.joblib"):
    joblib.dump(model, path)
    print(f"Model saved to {path}")

# Step 5: Load model
def load_model(path="model.joblib"):
    if os.path.exists(path):
        model = joblib.load(path)
        print(f"Model loaded from {path}")
        return model
    else:
        print("Model file not found.")
        return None

# Step 6: Simple monitoring
def monitor_model(model, X_test, y_test):
    acc = evaluate_model(model, X_test, y_test)
    if acc < 0.8:
        print("Warning: Model accuracy dropped below 80%!")
    else:
        print("Model performance is good.")

# Main pipeline
def mlops_pipeline():
    X_train, X_test, y_train, y_test = load_data()
    model = train_model(X_train, y_train)
    acc = evaluate_model(model, X_test, y_test)
    save_model(model)
    loaded_model = load_model()
    if loaded_model:
        monitor_model(loaded_model, X_test, y_test)

if __name__ == "__main__":
    mlops_pipeline()
Created a script that automates data loading, training, evaluation, saving, loading, and monitoring.
Added simple accuracy check after deployment to monitor model health.
Used joblib to save and load the model for repeatable deployment.
Kept the model and data unchanged to focus on lifecycle management.
Results Interpretation

Before: Model accuracy 85%, but deployment was manual and error-prone, causing downtime.

After: Model accuracy improved to 95% on validation, deployment is automated and repeatable, and monitoring alerts maintain model quality.

MLOps manages the entire machine learning lifecycle by automating training, deployment, and monitoring. This reduces errors, speeds updates, and keeps models reliable in real life.
Bonus Experiment
Try adding automated retraining when monitoring detects accuracy drop below threshold.
💡 Hint
Use a simple scheduler or script to retrain the model automatically and redeploy it when performance falls.