<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sanket M on Medium]]></title>
        <description><![CDATA[Stories by Sanket M on Medium]]></description>
        <link>https://medium.com/@sanketmote55?source=rss-0464ae326504------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*D3Zv24PyPSmwKgi7vx_RZw.jpeg</url>
            <title>Stories by Sanket M on Medium</title>
            <link>https://medium.com/@sanketmote55?source=rss-0464ae326504------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 14 Nov 2026 02:53:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sanketmote55/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Mlops in CDP -wip]]></title>
            <link>https://medium.com/@sanketmote55/mlops-in-cdp-wip-9b3aec7f75d8?source=rss-0464ae326504------2</link>
            <guid isPermaLink="false">https://medium.com/p/9b3aec7f75d8</guid>
            <category><![CDATA[mlops]]></category>
            <dc:creator><![CDATA[Sanket M]]></dc:creator>
            <pubDate>Tue, 06 Aug 2024 06:19:43 GMT</pubDate>
            <atom:updated>2024-08-06T06:19:43.770Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Project: Customer Churn Prediction using MLOps on CDP</strong></p><p><strong>Step 1: Set Up the Environment</strong></p><p><strong>1.1 Create a New Project in CML</strong></p><ol><li><strong>Log into Cloudera Machine Learning (CML):</strong></li></ol><ul><li>Access the CML dashboard through your CDP account.</li></ul><ol><li><strong>Create a New Project:</strong></li></ol><ul><li>On the CML dashboard, click on the “Create Project” button.</li><li>Name the project “Customer Churn Prediction”.</li><li>Select the appropriate workspace or environment.</li></ul><p><strong>1.2 Set Up the Workbench</strong></p><ol><li><strong>Start a New Session:</strong></li></ol><ul><li>In your newly created project, click on “Start Session.”</li><li>Choose Python 3 as the language for the session.</li><li>Configure the session with the necessary resources (CPU, memory).</li></ul><ol><li><strong>Install Required Python Packages:</strong></li></ol><ul><li>Once the session is running, open a terminal or create a new notebook in the session.</li><li>Install the following packages by running the command:</li></ul><pre>!pip install pandas scikit-learn mlflow matplotlib</pre><p><strong>Step 2: Data Preparation</strong></p><p><strong>2.1 Upload Dataset</strong></p><ol><li><strong>Obtain Dataset:</strong></li></ol><ul><li>Download a customer churn dataset, such as the Telco Customer Churn dataset from Kaggle. <a href="https://www.kaggle.com/datasets/blastchar/telco-customer-churn">https://www.kaggle.com/datasets/blastchar/telco-customer-churn</a></li></ul><ol><li><strong>Upload Dataset to CML:</strong></li></ol><ul><li>Go to your project, click on “Data,” and upload the dataset file (e.g., Telco-Customer-Churn.csv).</li></ul><p><strong>2.2 Data Preprocessing</strong></p><ol><li><strong>Load the Dataset:</strong></li></ol><ul><li>In a new notebook, load the dataset using pandas:</li></ul><pre>import pandas as pd<br>df = pd.read_csv(&quot;Telco-Customer-Churn.csv&quot;)</pre><p><strong>Explore the Data:</strong></p><ul><li>Display the first few rows to understand the structure:</li></ul><pre>df.head()</pre><p><strong>Handle Missing Values:</strong></p><ul><li>Identify and handle any missing values in the dataset:</li></ul><pre>df.isnull().sum()<br>df.dropna(inplace=True)</pre><p><strong>Encode Categorical Variables:</strong></p><ul><li>Convert categorical variables into numerical values using one-hot encoding:</li></ul><pre>df = pd.get_dummies(df, drop_first=True)</pre><p><strong>Split the Data:</strong></p><ul><li>Split the data into features (X) and target (y) variables, and then into training and testing sets:</li></ul><pre>from sklearn.model_selection import train_test_split<br><br>X = df.drop(&#39;Churn_Yes&#39;, axis=1)<br>y = df[&#39;Churn_Yes&#39;]<br><br>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)</pre><p><strong>Step 3: Model Training</strong></p><p><strong>3.1 Train the Model</strong></p><ol><li><strong>Train a Logistic Regression Model:</strong></li></ol><ul><li>Train a simple logistic regression model:</li></ul><pre>from sklearn.linear_model import LogisticRegression<br>from sklearn.metrics import accuracy_score, precision_score, recall_score<br><br>model = LogisticRegression()<br>model.fit(X_train, y_train)</pre><p><strong>Evaluate the Model:</strong></p><ul><li>Evaluate the model on the test data:</li></ul><pre>y_pred = model.predict(X_test)<br>accuracy = accuracy_score(y_test, y_pred)<br>precision = precision_score(y_test, y_pred)<br>recall = recall_score(y_test, y_pred)<br><br>print(f&quot;Accuracy: {accuracy}&quot;)<br>print(f&quot;Precision: {precision}&quot;)<br>print(f&quot;Recall: {recall}&quot;)</pre><p><strong>Log Model with MLFlow</strong></p><ol><li><strong>Set Up MLFlow Tracking:</strong></li></ol><ul><li>Initialize MLFlow in your notebook:</li></ul><pre>import mlflow<br>import mlflow.sklearn<br><br>mlflow.set_experiment(&quot;Customer_Churn_Prediction&quot;)</pre><p><strong>Log the Model and Metrics:</strong></p><ul><li>Log the model, metrics, and parameters to MLFlow:</li></ul><pre>with mlflow.start_run():<br>    mlflow.sklearn.log_model(model, &quot;model&quot;)<br>    mlflow.log_metric(&quot;accuracy&quot;, accuracy)<br>    mlflow.log_metric(&quot;precision&quot;, precision)<br>    mlflow.log_metric(&quot;recall&quot;, recall)<br>    mlflow.log_param(&quot;model_type&quot;, &quot;Logistic Regression&quot;)</pre><p><strong>Step 4: Model Deployment</strong></p><p><strong>4.1 Deploy the Model in CML</strong></p><ol><li><strong>Deploy as a REST API:</strong></li></ol><ul><li>In CML, navigate to “Models” and click on “Deploy New Model.”</li><li>Select the model logged in MLFlow from the “Customer_Churn_Prediction” experiment.</li><li>Name the model deployment, and select the appropriate Python function (e.g., predict) to deploy the model as an API endpoint.</li></ul><ol><li><strong>Test the API Endpoint:</strong></li></ol><ul><li>Once deployed, test the API by sending a sample input to the endpoint and verifying the output:</li></ul><pre>import requests<br>import json<br><br>url = &quot;https://&lt;CML_model_endpoint&gt;/predict&quot;<br>payload = json.dumps({&quot;data&quot;: X_test.iloc[0].tolist()})<br>headers = {&#39;Content-Type&#39;: &#39;application/json&#39;}<br><br>response = requests.post(url, data=payload, headers=headers)<br>print(response.json())</pre><p><strong>Step 5: Model Monitoring</strong></p><p><strong>5.1 Set Up Monitoring</strong></p><ol><li><strong>Monitor Performance in CML:</strong></li></ol><ul><li>Use CML’s model monitoring features to track key metrics like accuracy, precision, and recall over time.</li><li>Set up alerts to notify you if the model performance degrades.</li></ul><ol><li><strong>Retraining Pipeline:</strong></li></ol><ul><li>Set up a CI/CD pipeline in GitLab integrated with CML to automate model retraining and deployment when new data is available.</li></ul><p>Step 5: Model Monitoring and CI/CD Pipeline Setup Using GitHub Actions</p><p><strong>5.2 Setting Up a CI/CD Pipeline in GitHub</strong></p><p><strong>Step 5.2.1: Setting Up GitHub Repository</strong></p><ol><li><strong>Create a New GitHub Repository:</strong></li></ol><ul><li>Log in to GitHub and create a new repository for the project. Name it something relevant, like customer-churn-mlops.</li></ul><ol><li><strong>Push Your Project Files:</strong></li></ol><ul><li>Clone the repository to your local machine or directly push the project files from your local environment to the GitHub repository:</li></ul><pre>git clone https://gitlab.com/yourusername/customer-churn-mlops.git<br>cd customer-churn-mlops<br>git add .<br>git commit -m &quot;Initial commit&quot;<br>git push origin main</pre><p><strong>Organize Project Structure:</strong></p><ul><li>Structure your repository with the following files and directories:</li></ul><p>customer-churn-mlops/</p><pre>customer-churn-mlops/<br>├── data/<br>│   └── Telco-Customer-Churn.csv<br>├── models/<br>├── notebooks/<br>│   └── churn_model_training.ipynb<br>├── scripts/<br>│   └── train_model.py<br>├── .gitlab-ci.yml<br>├── requirements.txt<br>└── README.md</pre><p><strong>Step 5.2.2: Create Python Scripts for Training and Deployment</strong></p><ol><li><strong>Create train_model.py Script:</strong></li></ol><ul><li>Create a Python script under the scripts/ directory to handle model training and logging with MLFlow:</li></ul><pre>import pandas as pd<br>from sklearn.model_selection import train_test_split<br>from sklearn.linear_model import LogisticRegression<br>from sklearn.metrics import accuracy_score, precision_score, recall_score<br>import mlflow<br>import mlflow.sklearn<br><br>def load_data():<br>    df = pd.read_csv(&quot;../data/Telco-Customer-Churn.csv&quot;)<br>    df = pd.get_dummies(df, drop_first=True)<br>    return df<br><br>def train_and_log_model():<br>    df = load_data()<br>    X = df.drop(&#39;Churn_Yes&#39;, axis=1)<br>    y = df[&#39;Churn_Yes&#39;]<br>    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)<br><br>    model = LogisticRegression()<br>    model.fit(X_train, y_train)<br><br>    accuracy = accuracy_score(y_test, model.predict(X_test))<br>    precision = precision_score(y_test, model.predict(X_test))<br>    recall = recall_score(y_test, model.predict(X_test))<br><br>    mlflow.set_experiment(&quot;Customer_Churn_Prediction&quot;)<br><br>    with mlflow.start_run():<br>        mlflow.sklearn.log_model(model, &quot;model&quot;)<br>        mlflow.log_metric(&quot;accuracy&quot;, accuracy)<br>        mlflow.log_metric(&quot;precision&quot;, precision)<br>        mlflow.log_metric(&quot;recall&quot;, recall)<br><br>if __name__ == &quot;__main__&quot;:<br>    train_and_log_model()</pre><p><strong>Create requirements.txt:</strong></p><ul><li>List all the dependencies required for the project:</li></ul><pre>pandas<br>scikit-learn<br>mlflow</pre><p><strong>Step 5.2.3: Configure GitHub Actions Workflow</strong></p><ol><li><strong>Create GitHub Actions Workflow:</strong></li></ol><ul><li>Create a .github/workflows/ci-cd.yml file in your repository. This file will define the CI/CD pipeline:</li></ul><pre>image: python:3.9<br><br>stages:<br>  - test<br>  - deploy<br><br>before_script:<br>  - pip install -r requirements.txt<br><br>test_model:<br>  stage: test<br>  script:<br>    - python scripts/train_model.py<br><br>deploy_model:<br>  stage: deploy<br>  script:<br>    - |<br>      mlflow models serve -m &quot;models:/Customer_Churn_Prediction/Production&quot; -h 0.0.0.0 -p 5000</pre><p><strong>Explanation:</strong></p><ul><li><strong>name</strong>: Names the workflow.</li><li><strong>on</strong>: Specifies the trigger events (push, pull requests, or a scheduled run).</li><li><strong>jobs</strong>: Defines the tasks to be executed.</li><li><strong>build-and-deploy</strong>: The main job in the workflow.</li><li><strong>steps</strong>: Lists each step in the pipeline:</li><li><strong>Checkout code</strong>: Checks out the repository code.</li><li><strong>Set up Python</strong>: Sets up the Python environment.</li><li><strong>Install dependencies</strong>: Installs the required Python packages.</li><li><strong>Train and log model</strong>: Runs the training script to log the model in MLFlow.</li><li><strong>Deploy model</strong>: Deploys the model using MLFlow’s model serving functionality.</li></ul><p>· <strong>Configure Secrets in GitHub:</strong></p><ul><li>Go to your GitHub repository, navigate to <strong>Settings &gt; Secrets and variables &gt; Actions</strong>, and add any required secrets:</li><li>MLFLOW_TRACKING_URI: The tracking URI for your MLFlow server.</li><li>MLFLOW_S3_ENDPOINT_URL, AWS_ACCESS_KEY_ID, and AWS_SECRET_ACCESS_KEY if you’re using S3 as a backend for MLFlow.</li></ul><p>· <strong>Trigger the Workflow:</strong></p><ul><li>Push the changes to GitHub, and the pipeline will automatically trigger based on the defined events:</li></ul><pre>git add .<br>git commit -m &quot;Added CI/CD pipeline configuration&quot;<br>git push origin main</pre><p>1.</p><ul><li>Monitor the workflow’s progress under the <strong>Actions</strong> tab in your GitHub repository.</li></ul><p><strong>Step 5.2.4: Automate Model Retraining and Deployment</strong></p><ol><li><strong>Schedule Regular Retraining:</strong></li></ol><ul><li>The cron syntax in the workflow file schedules the pipeline to run at regular intervals. Adjust it as needed for your project (e.g., daily, weekly).</li></ul><ol><li><strong>Trigger Retraining on Data Updates:</strong></li></ol><ul><li>Optionally, set up a process where pushing new data to the repository triggers the retraining pipeline automatically.</li></ul><p><strong>Final Outcome:</strong></p><p>With this GitHub Actions CI/CD pipeline, your MLOps process is automated. The pipeline will:</p><ol><li>Automatically train the model whenever there’s a change in the code or data.</li><li>Log the trained model and metrics to MLFlow.</li><li>Deploy the latest model using MLFlow’s serving feature.</li></ol><p>This setup ensures that your machine learning model is continuously updated and deployed, keeping your production environment aligned with the latest data and code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9b3aec7f75d8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>