VOOZH about

URL: https://www.analyticsvidhya.com/blog/2021/04/bring-devops-to-data-science-with-continuous-mlops/

โ‡ฑ MLOps | Bring DevOps To Data Science With MLOps


India's Most Futuristic AI Conference Is Back โ€“ Bigger, Sharper, Bolder

  • d
  • :
  • h
  • :
  • m
  • :
  • s

Reading list

Bring DevOps To Data Science With MLOps

Amit Last Updated : 28 Oct, 2024
7 min read
This article was published as a part of the Data Science Blogathon.

MLOps is the intersection of Machine Learning, DevOps and Data Engineering.

Introduction:

As the data science and machine learning models are becoming more capable of solving complex business problems, it is evident that many businesses continue to invest in building their capabilities in this field to deliver business value to their users. This trend is also driven by the fact that managing resources such as huge data, infrastructure, compute power, pretrained models, etc have become very inexpensive and on-demand which has enabled teams to go from prototype to production in a very short time.

However, the businesses have also realized that the real challenge isnโ€™t in building machine learning models but on the operational side of things, especially in production. How do we ensure the different pieces of functionalities built by various teams integrate seamlessly? How do make sure that the model in production is not drifting? How do we automatically validate the data from various sources and check for standards? How do we refresh models in production on the fly?

These and many more such questions are not new, and we have seen similar stuff on the typical web development over the years. There have been various methodologies and approaches developed and one such thing is DevOps which has to a large extent addressed these challenges and is the norm currently. So, is there a way to leverage its power in data science?. Letโ€™s explore.

 

DevOps Lifecycle:

DevOps refers to a software development method and a collaborative way of developing and deploying software. It is a practice that allows a single team to manage the entire application development life cycle, that is, development, testing, deployment, operations. DevOps helps to establish cross-functional teams that share responsibility for maintaining the system that runs the software and prepares the software to run on that system with increased quality feedback and automation issues. At a high level, there are different stages of the DevOps lifecycle.

๐Ÿ‘ DevOps Lifecycle - MLOps

Here is how all the above components come together to create a seamless development, integration, testing and deployment.

๐Ÿ‘ MLOps, devops integration

Data Science Lifecycle:

Similar to typical Software Development Lifecycle (SDLC), most data science projects do have a process that outlines the major stages of execution. The lifecycle is not linear meaning each stage may undergo multiple iterations till we reach satisfying results which are acceptable technically and by the business. The various stages of the lifecycle can be summarized below.

๐Ÿ‘ MLOps data science lifecycle

Machine Learning Operations (MLOps):

Now, that we have a fair understanding of both DevOps and the Data science lifecycle, is there a way to leverage the powerful features of DevOps like automation, workflows, and test automation in data science projects?
Certainly, yes and thatโ€™s what we will explore in the next sections where we will bring these approaches together. In todayโ€™s world, this is called Machine Learning Operations (MLOps) and in short, it is the intersection of Machine Learning, DevOps, and Data Engineering. Letโ€™s understand this better with an example.

Any Prerequisites?

To understand MLOps, you will need very basic knowledge of model building in python and a GitHub account. I will be using Visual Studio Code as editor; you can use any editor of your choice as long as you are comfortable with it.

Getting Started:

We will be using Kaggleโ€™s South Africa Heart Disease dataset. Here is the data dictionary for reference. Our objective is to predict chd i.e., coronary heart disease (yes=1 or no=0). A simple binary classification model.

  • sbp: systolic blood pressure
  • tobacco: cumulative tobacco (kg)
  • ldl: low-density lipoprotein cholesterol
  • adiposity:
  • famhist: family history of heart disease (Present=1, Absent=0)
  • typea: type-A behavior
  • obesity
  • alcohol: current alcohol consumption
  • age: age at onset
  • chd: coronary heart disease (yes=1 or no=0)

Letโ€™s load the dataset and take a quick look at data and some basic stats. We will also drop โ€˜famhistโ€˜ variable for now and will experiment with it at a later stage

Python Code:

import pandas as pd

df_heart = pd.read_csv('SAheart.csv', index_col=0)
print(df_heart.head(10))
print(df_heart.describe())
df_heart.drop('famhist', axis=1, inplace=True)

print(df_heart.head())

Split the dataset in train and test:

# Set random seed
seed = 52
# Split into train and test sections
y = df_heart.pop('chd')
X_train, X_test, y_train, y_test = train_test_split(df_heart, y, test_size=0.2, random_state=seed)

Model building: A simple binary classification model

model = LogisticRegression(solver='liblinear', random_state=0).fit(X_train, y_train)

Report training/test scoreWe will create a text file by name metrics.txt in which the score will be printed

# Report training set score
train_score = model.score(X_train, y_train) * 100

# Report test set score
test_score = model.score(X_test, y_test) * 100

# Write scores to a file
with open("metrics.txt", 'w') as outfile:
outfile.write("Training variance explained: %2.1f%%n" % train_score)
outfile.write("Test variance explained: %2.1f%%n" % test_score)

Model Metrics โ€“ Confusion Matrix:

# Confusion Matrix and plot
cm = confusion_matrix(y_test, model.predict(X_test))
fig, ax = plt.subplots(figsize=(8, 8))
ax.imshow(cm)
ax.grid(False)
ax.xaxis.set(ticks=(0, 1), ticklabels=('Predicted 0s', 'Predicted 1s'))
ax.yaxis.set(ticks=(0, 1), ticklabels=('Actual 0s', 'Actual 1s'))
ax.set_ylim(1.5, -0.5)
for i in range(2):
 for j in range(2):
 ax.text(j, i, cm[i, j], ha='center', va='center', color='red')
plt.tight_layout()
plt.savefig("cm.png",dpi=120) 
plt.close()

Model Metrics โ€“ ROC curve

# Plot the ROC curve
model_ROC = plot_roc_curve(model, X_test, y_test)
plt.tight_layout()
plt.savefig("roc.png",dpi=120)
plt.close()

Classification report to the console: Letโ€™s print this on console, will be handy.

# Print classification report
print(classification_report(y_test, model.predict(X_test)))

Bringing all pieces together: Our final train.py file will be as below

import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, confusion_matrix, roc_auc_score, plot_roc_curve
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
df_heart = pd.read_csv('SAHeart.csv', index_col=0)
df_heart.head(10)
df_heart.describe()
df_heart.drop('famhist', axis=1, inplace=True)
# Set random seed
seed = 52
# Split into train and test sections
y = df_heart.pop('chd')
X_train, X_test, y_train, y_test = train_test_split(df_heart, y, test_size=0.2, random_state=seed)
# Build logistic regression model
model = LogisticRegression(solver='liblinear', random_state=0).fit(X_train, y_train)
# Report training set score
train_score = model.score(X_train, y_train) * 100
# Report test set score
test_score = model.score(X_test, y_test) * 100
# Write scores to a file
with open("metrics.txt", 'w') as outfile:
 outfile.write("Training variance explained: %2.1f%%n" % train_score)
 outfile.write("Test variance explained: %2.1f%%n" % test_score)
# Confusion Matrix and plot
cm = confusion_matrix(y_test, model.predict(X_test))
fig, ax = plt.subplots(figsize=(8, 8))
ax.imshow(cm)
ax.grid(False)
ax.xaxis.set(ticks=(0, 1), ticklabels=('Predicted 0s', 'Predicted 1s'))
ax.yaxis.set(ticks=(0, 1), ticklabels=('Actual 0s', 'Actual 1s'))
ax.set_ylim(1.5, -0.5)
for i in range(2):
 for j in range(2):
 ax.text(j, i, cm[i, j], ha='center', va='center', color='red')
plt.tight_layout()
plt.savefig("cm.png",dpi=120) 
plt.close()
# Print classification report
print(classification_report(y_test, model.predict(X_test)))
#roc_auc_score(y_test, model.predict_proba(X_test)[:, 1])
# Plot the ROC curve
model_ROC = plot_roc_curve(model, X_test, y_test)
plt.tight_layout()
plt.savefig("roc.png",dpi=120) 
plt.close()

GitHub Workflows and CML: Create a new workflow directory and a new file under it by name cml.yaml. You can name the file anything you want to but ensure the extension is .yaml

๐Ÿ‘ GitHub Workflows

Define the process in the yaml as below: 

  1. We want to trigger the process every time there is a code push to the repository.
  2. We will use the docker image from CML in this case but, we can also use our own docker if required.
  3. The dependencies that we had defined in requirements.txt file will be installed on the environment.
  4. Our model file train.py will be executed at the end.
name: model-CHD
on: [push]
jobs:
 run:
 runs-on: [ubuntu-latest]
 container: docker://dvcorg/cml-py3:latest
 steps:
 - uses: actions/checkout@v2
 - name: 'Train my model'
 env:
 repo_token: ${{ secrets.GITHUB_TOKEN }}
 run: |
 # Your ML workflow goes here
 pip install -r requirements.txt
 python train.py

We also had generated some metrics like confusion matrix and ROC curve. It would be great to have them displayed in the markdown format which will help us with our experiments and also to measure/track the modelโ€™s performance.

echo "## Model Metrics" >> report.md
cat metrics.txt >> report.md
# Write your CML report
echo "## Model Visualization" >> report.md 
cml-publish cm.png --md >> report.md
cml-publish roc.png --md >> report.md
cml-send-comment report.md

Refer to the complete YAML file from the git repository.

Once you commit the YAML file, the workflow will be triggered as shown below.

๐Ÿ‘ workflow

Expand the Train my model tab and there is our classification report we had planned to print on the console.

๐Ÿ‘ Train my model

Navigate your commits on GitHub and you can see the results and both the plots in markdown format.

๐Ÿ‘ Navigate your commits MLOps

Great, we have been able to load the data, build a model, create a workflow, and execute the whole process on git push.

Now, letโ€™s experiment: Once we make any changes to the code and push it to GitHub, the entire workflow will be triggered and a new markdown report will be generated specific to our experiment.

  • we will change the test size from 0.2 to 0.25
X_train, X_test, y_train, y_test = train_test_split(df_heart, y, test_size=0.25, random_state=seed)
  • We had initially dropped a variable by name famhist, lets dummy it and use it in our model
df_heart = pd.get_dummies(df_heart, columns = ['famhist'], drop_first=True)
๐Ÿ‘ let's experiment MLOps

Ahh!!! As you can see, there is an improvement in the modelโ€™s performance with our changes using MLOps.

Conclusion:

The objective of the blog was to showcase how we can leverage the powerful features of DevOps like CI/CD, automation, workflows and apply them to our data science projects & experiments with MLOps. The CML โ€“ Continuous Machine Learning is a very handy tool have for tracking the experiment results, collaborate with others, and automating the entire workflow.

Happy learnings !!!!

You can connect with me โ€“ Linkedin

You can find the code for reference โ€“ Github

References

https://docs.github.com/en/actions

https://cml.dev

The media shown in this article are not owned by Analytics Vidhya and is used at the Authorโ€™s discretion. 

I am a Data Science enthusiast with experience in building predictive models, data processing, and data mining algorithms to solve challenging business problems. Involved in open source community and passionate about building data apps.

Login to continue reading and enjoy expert-curated content.

Free Courses

Ensemble Learning and Ensemble Learning Techniques

Learn ensemble learning, its techniques, and how it works in this course!

Bagging and Boosting ML Algorithms

Explore Bagging and Boosting to understand advanced ML algorithms.

Naive Bayes from Scratch

Master Naรฏve Bayes for ML: Build classifiers, analyze data, and apply Bayes.

Dimensionality Reduction for Machine Learning

Master key dimensionality reduction techniques for ML success!

Responses From Readers

Excellent blog to illustrate the concept of MLOPs . New to this ML field and still learning git and DevOps. Found it extremely useful. clone the rep and then played around with a few changes and checked the build, metrics generation and plots updated accordingly automatically. Thanks so much for this

Nathan Segers

This is a very interesting blog post, but I do miss the integration of this project into existing software. It's good to automatically train this model, but is the trained model ever saved and loaded into production? If you want to check this model with new data while it is already in production, let's say by using an API, how will this be done? I am interested into your insights on that.

Flagship Programs

GenAI Pinnacle Program| GenAI Pinnacle Plus Program| AI/ML BlackBelt Program| Agentic AI Pioneer Program

Free Courses

Generative AI| DeepSeek| OpenAI Agent SDK| LLM Applications using Prompt Engineering| DeepSeek from Scratch| Stability.AI| SSM & MAMBA| RAG Systems using LlamaIndex| Building LLMs for Code| Python| Microsoft Excel| Machine Learning| Deep Learning| Mastering Multimodal RAG| Introduction to Transformer Model| Bagging & Boosting| Loan Prediction| Time Series Forecasting| Tableau| Business Analytics| Vibe Coding in Windsurf| Model Deployment using FastAPI| Building Data Analyst AI Agent| Getting started with OpenAI o3-mini| Introduction to Transformers and Attention Mechanisms

Popular Categories

AI Agents| Generative AI| Prompt Engineering| Generative AI Application| News| Technical Guides| AI Tools| Interview Preparation| Research Papers| Success Stories| Quiz| Use Cases| Listicles

Generative AI Tools and Techniques

GANs| VAEs| Transformers| StyleGAN| Pix2Pix| Autoencoders| GPT| BERT| Word2Vec| LSTM| Attention Mechanisms| Diffusion Models| LLMs| SLMs| Encoder Decoder Models| Prompt Engineering| LangChain| LlamaIndex| RAG| Fine-tuning| LangChain AI Agent| Multimodal Models| RNNs| DCGAN| ProGAN| Text-to-Image Models| DDPM| Document Question Answering| Imagen| T5 (Text-to-Text Transfer Transformer)| Seq2seq Models| WaveNet| Attention Is All You Need (Transformer Architecture) | WindSurf| Cursor

Popular GenAI Models

Llama 4| Llama 3.1| GPT 4.5| GPT 4.1| GPT 4o| o3-mini| Sora| DeepSeek R1| DeepSeek V3| Janus Pro| Veo 2| Gemini 2.5 Pro| Gemini 2.0| Gemma 3| Claude Sonnet 3.7| Claude 3.5 Sonnet| Phi 4| Phi 3.5| Mistral Small 3.1| Mistral NeMo| Mistral-7b| Bedrock| Vertex AI| Qwen QwQ 32B| Qwen 2| Qwen 2.5 VL| Qwen Chat| Grok 3

AI Development Frameworks

n8n| LangChain| Agent SDK| A2A by Google| SmolAgents| LangGraph| CrewAI| Agno| LangFlow| AutoGen| LlamaIndex| Swarm| AutoGPT

Data Science Tools and Techniques

Python| R| SQL| Jupyter Notebooks| TensorFlow| Scikit-learn| PyTorch| Tableau| Apache Spark| Matplotlib| Seaborn| Pandas| Hadoop| Docker| Git| Keras| Apache Kafka| AWS| NLP| Random Forest| Computer Vision| Data Visualization| Data Exploration| Big Data| Common Machine Learning Algorithms| Machine Learning| Google Data Science Agent
๐Ÿ‘ Av Logo White

Continue your learning for FREE

Forgot your password?
๐Ÿ‘ Av Logo White

Enter OTP sent to

Edit

Wrong OTP.

Enter the OTP

Resend OTP

Resend OTP in 45s

๐Ÿ‘ Popup Banner
๐Ÿ‘ AI Popup Banner