AW Dev Rethought

🕵️ Debugging is like being the detective in a crime movie where you are also the murderer - Filipe Fortes

🧠 AI with Python – 🚀 End-to-End ML Workflow Recap Project


Description:

Throughout this AI with Python series, we’ve explored every major stage of a machine learning project—from data preprocessing and model training to deployment strategies and production monitoring.

In this final project of the ML Systems track, we’ll bring these concepts together into a single end-to-end machine learning workflow.

Instead of focusing on one algorithm or technique, this project demonstrates how the individual building blocks fit together to create a production-ready ML pipeline.


Why End-to-End Workflows Matter

Training a machine learning model is only one step in the overall process.

A real-world ML application also requires:

  • clean and consistent data
  • feature preprocessing
  • reliable evaluation
  • reusable pipelines
  • model persistence
  • prediction logging
  • deployment readiness

Combining these stages into one workflow makes machine learning solutions easier to maintain, reproduce, and deploy.


The Complete Workflow

A typical machine learning project follows a sequence like this:

Load Data
      ↓
Preprocess Data
      ↓
Train/Test Split
      ↓
Train Model
      ↓
Evaluate Performance
      ↓
Save Pipeline
      ↓
Predict New Data
      ↓
Deploy & Monitor

Each stage plays an important role in ensuring the model performs reliably in production.


Building the Pipeline

We begin by loading the dataset and creating a reusable pipeline.

pipeline = Pipeline([
    ...
])

The pipeline combines preprocessing and model training into a single workflow, ensuring the same transformations are applied during both training and prediction.

Once the pipeline is created, we split the data into training and testing sets before fitting the model.

pipeline.fit(
    X_train,
    y_train
)

This creates a consistent and reproducible training process.


Evaluating the Model

After training, the pipeline generates predictions for unseen data.

predictions = pipeline.predict(
    X_test
)

The model is then evaluated using multiple metrics such as:

  • Accuracy
  • Precision
  • Recall
  • F1-score

These metrics provide a more complete understanding of model performance than accuracy alone.

A confusion matrix can also be used to visualize correct and incorrect predictions.


Saving and Reusing the Pipeline

Rather than saving only the trained model, we save the complete pipeline.

joblib.dump(
    pipeline,
    "pipeline.pkl"
)

This preserves every preprocessing step along with the trained model, making future predictions consistent and deployment much simpler.

The saved pipeline can later be loaded to generate predictions on new data without repeating the training process.


Preparing for Production

Production systems often log prediction results for monitoring and analysis.

Logging helps teams:

  • monitor model performance
  • investigate prediction errors
  • detect concept drift
  • support future retraining

Although this example is simple, it demonstrates the workflow used by many production machine learning systems.


Key Takeaways

  1. A complete ML workflow extends beyond model training.
  2. Pipelines ensure consistent preprocessing during training and inference.
  3. Multiple evaluation metrics provide a better understanding of model quality.
  4. Saving the full pipeline simplifies deployment and reuse.
  5. End-to-end workflows are the foundation of reliable production ML systems.

Conclusion

Building a machine learning model is only part of creating an AI solution. A robust workflow includes data preparation, preprocessing, evaluation, model persistence, and production readiness. By combining these components into a single pipeline, we create machine learning systems that are easier to deploy, maintain, and scale.

This project concludes the ML Systems track and serves as the capstone of the first AI with Python journey. It demonstrates how the concepts explored throughout the series come together to form a practical, production-style machine learning workflow.


Code Snippet:

# 📦 Import Required Libraries
import pandas as pd
import joblib
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import (
    accuracy_score,
    precision_score,
    recall_score,
    f1_score,
    confusion_matrix,
    classification_report
)


# =========================================================
# 🧩 Load Dataset
# =========================================================

data = load_breast_cancer()

X = pd.DataFrame(
    data.data,
    columns=data.feature_names
)

y = pd.Series(data.target)


# =========================================================
# ✂️ Train-Test Split
# =========================================================

X_train, X_test, y_train, y_test = train_test_split(
    X,
    y,
    test_size=0.30,
    random_state=42,
    stratify=y
)


# =========================================================
# 🏗️ Build End-to-End ML Pipeline
# =========================================================

pipeline = Pipeline([

    (
        "imputer",
        SimpleImputer(strategy="median")
    ),

    (
        "scaler",
        StandardScaler()
    ),

    (
        "classifier",
        RandomForestClassifier(
            n_estimators=200,
            random_state=42
        )
    )

])


# =========================================================
# 🤖 Train Pipeline
# =========================================================

pipeline.fit(
    X_train,
    y_train
)

print("Pipeline trained successfully.\n")


# =========================================================
# 📊 Generate Predictions
# =========================================================

predictions = pipeline.predict(
    X_test
)

prediction_probabilities = pipeline.predict_proba(
    X_test
)[:, 1]


# =========================================================
# 📈 Evaluate Model
# =========================================================

accuracy = accuracy_score(
    y_test,
    predictions
)

precision = precision_score(
    y_test,
    predictions
)

recall = recall_score(
    y_test,
    predictions
)

f1 = f1_score(
    y_test,
    predictions
)

print("=== Model Evaluation ===")

print(
    f"Accuracy : {accuracy:.4f}"
)

print(
    f"Precision: {precision:.4f}"
)

print(
    f"Recall   : {recall:.4f}"
)

print(
    f"F1 Score : {f1:.4f}"
)


# =========================================================
# 🔍 Confusion Matrix
# =========================================================

cm = confusion_matrix(
    y_test,
    predictions
)

print("\nConfusion Matrix:\n")
print(cm)


# =========================================================
# 📋 Classification Report
# =========================================================

print("\nClassification Report:\n")

print(
    classification_report(
        y_test,
        predictions
    )
)


# =========================================================
# 💾 Save Complete Pipeline
# =========================================================

joblib.dump(
    pipeline,
    "breast_cancer_pipeline.pkl"
)

print(
    "\nPipeline saved successfully."
)


# =========================================================
# 📂 Load Saved Pipeline
# =========================================================

loaded_pipeline = joblib.load(
    "breast_cancer_pipeline.pkl"
)

print(
    "Pipeline loaded successfully."
)


# =========================================================
# 🚀 Predict New Samples
# =========================================================

new_samples = X_test.iloc[:5]

new_predictions = loaded_pipeline.predict(
    new_samples
)

new_probabilities = loaded_pipeline.predict_proba(
    new_samples
)[:, 1]


# =========================================================
# 📝 Prediction Logs
# =========================================================

prediction_logs = pd.DataFrame({

    "Actual": y_test.iloc[:5].values,

    "Prediction": new_predictions,

    "Probability": new_probabilities.round(4)

})

print("\nPrediction Logs:\n")
print(prediction_logs)


# =========================================================
# 📊 Prediction Summary
# =========================================================

correct_predictions = (
    prediction_logs["Actual"]
    ==
    prediction_logs["Prediction"]
).sum()

incorrect_predictions = (
    len(prediction_logs)
    -
    correct_predictions
)

print("\nPrediction Summary")

print(
    f"Correct Predictions  : {correct_predictions}"
)

print(
    f"Incorrect Predictions: {incorrect_predictions}"
)


# =========================================================
# 💾 Save Prediction Logs
# =========================================================

prediction_logs.to_csv(
    "prediction_logs.csv",
    index=False
)

print(
    "\nPrediction logs saved successfully."
)


# =========================================================
# 📂 Load Prediction Logs
# =========================================================

saved_logs = pd.read_csv(
    "prediction_logs.csv"
)

print("\nSaved Prediction Logs:\n")
print(saved_logs)


# =========================================================
# 🚀 Workflow Completed
# =========================================================

print("\n=====================================")
print(" End-to-End ML Workflow Completed")
print("=====================================")

print("✔ Data Loaded")
print("✔ Data Split")
print("✔ Pipeline Created")
print("✔ Model Trained")
print("✔ Model Evaluated")
print("✔ Pipeline Saved")
print("✔ Pipeline Reloaded")
print("✔ Predictions Generated")
print("✔ Prediction Logs Saved")
print("✔ Workflow Ready for Deployment")

Link copied!

Comments

Add Your Comment

Comment Added!