CC Module 2 U
CC Module 2 U
2. Data Preprocessing
The collected data is cleaned and prepared for analysis.
Tasks involved:
• Removing missing values
• Removing duplicate records
• Handling outliers
• Data normalization and formatting
This ensures the data is accurate and consistent.
5. Model Selection
An appropriate machine learning algorithm is selected based on the problem.
Examples:
• Linear Regression
• Decision Tree
• Random Forest
• Support Vector Machine (SVM)
• Neural Networks
The goal is to choose the model that best fits the data.
6. Model Training
The selected algorithm learns patterns from the training dataset by adjusting its internal parameters.
The objective is to minimize prediction errors and improve accuracy.
• Decision Tree
• Random Forest
• Support Vector Machine (SVM)
• Neural Networks
The goal is to choose the model that best fits the data.
6. Model Training
The selected algorithm learns patterns from the training dataset by adjusting its internal parameters.
The objective is to minimize prediction errors and improve accuracy.
7. Model Evaluation
The trained model is tested using the test dataset to measure its performance.
Common Evaluation Metrics:
• Accuracy
• Precision
• Recall
• F1-Score
• Mean Squared Error (MSE)
This step checks whether the model performs well on unseen data.
Objectives of ML Deployment
• Make the trained model available to end users.
• Generate real-time or batch predictions.
• Integrate the model with web, mobile, or cloud applications.
• Improve business decision-making through automated predictions.
• Monitor and update the model for better performance.
Importance of ML Deployment
• Enables real-world usage of ML models.
• Automates decision-making.
• Supports real-time predictions.
• Creates business value.
• Provides scalability for millions of users.
• Allows continuous monitoring and retraining.
Deployment Methods
1. Batch Deployment – Predictions are generated on large datasets at scheduled intervals.
2. Real-Time (Online) Deployment – Instant predictions using APIs.
3. Edge Deployment – Model runs on local devices (IoT, smartphones).
4. Streaming Deployment – Handles continuous real-time data streams.
2. ML Deployment Architecture
Definition
ML Deployment Architecture is the framework that integrates a trained machine learning model into a
production environment. It defines how data flows from users to the model and back while ensuring
scalability, reliability, and efficient prediction serving.
3. Model Registry
Stores trained machine learning models with:
• Version control
• Metadata
• Lifecycle tracking
• Feature engineering
3. Model Registry
Stores trained machine learning models with:
• Version control
• Metadata
• Lifecycle tracking
5. Inference Engine
Receives processed input, executes the ML model, and generates predictions in real-time or batch
mode.
6. Monitoring System
Continuously monitors:
• Model accuracy
• Latency
• Data drift
• Failures
• System performance
7. Logging System
Stores:
• User requests
• Predictions
• Errors
• Performance logs
Useful for debugging and auditing.
8. CI/CD Pipeline
Automates:
• Model testing
• Deployment
• Updates
• Continuous integration and delivery
Workflow Diagram
Data Source
↓
Data Processing
↓
Feature Engineering
↓
Model Training
↓
Hyperparameter Tuning
↓
Model Evaluation
↓
Model Deployment
↓
Prediction (Inference)
↓
Monitoring & Retraining
↓
Model Evaluation
↓
Model Deployment
↓
Prediction (Inference)
↓
Monitoring & Retraining
Major Challenges
1. Data Drift
The statistical properties of input data change over time, reducing model accuracy.
Example: Customer buying behavior changes during festivals.
2. Model Versioning
Managing multiple versions of trained models and enabling rollback when necessary.
3. Scalability Issues
The system must handle increasing numbers of users and prediction requests without affecting
performance.
4. Latency Constraints
Real-time applications require predictions within milliseconds.
Example: Fraud detection during online payments.
5. Integration Complexity
Integrating ML models with existing software, databases, APIs, and cloud platforms can be difficult.
7. Security Risks
ML systems must protect:
• Sensitive user data
• Model APIs
• Unauthorized access
Security measures include authentication, authorization, and encryption.
1. Model Inference
Definition
Model Inference is the process of using a trained machine learning model to make predictions on new,
unseen data. After deployment, the model receives input data, processes it, and returns the predicted
output.
Inference Workflow
1. Input Data Received
New data is received from users, applications, sensors, APIs, or databases.
2. Data Preprocessing
The input data is cleaned, transformed, normalized, and converted into the format required by the
model.
3. Model Prediction
The trained model processes the input data and generates a prediction.
4. Output Generation
The prediction is returned to the user or application.
5. Logging & Monitoring
The input, prediction, response time, and errors are recorded to monitor model performance.
Workflow Diagram
Input Data
↓
Data Preprocessing
↓
Model Prediction
↓
Output Generation
↓
Logging & Monitoring
Serialization
Definition
Serialization is the process of converting a trained machine learning model (including its learned
parameters, architecture, and configuration) into a storable file or byte stream so that it can be saved,
transferred, and reused later without retraining.
Serialization Process
1. Train the ML model.
• Avoids retraining the model
• Saves computation time and cost
• Enables deployment in real-world applications
• Allows sharing of models across different systems
• Makes backup and storage easy
Serialization Process
1. Train the ML model.
2. Convert the model into a byte stream/file.
3. Save it in a file format such as .pkl, .joblib, .h5, or .pt.
4. Store the file for future use.
Example
After training a fraud detection model, it is serialized and saved as [Link]. Later, the same file is
loaded into a banking application without retraining.
Deserialization
Definition
Deserialization is the process of loading a previously saved machine learning model from a file and
converting it back into a usable model in memory for making predictions.
Deserialization Process
1. Read the saved model file.
2. Convert the byte stream back into a model object.
3. Load the model into memory.
4. Use the model for prediction.
Challenges
• Compatibility issues between library versions
• Security risks from untrusted model files
• Missing dependencies during loading
Example
A saved [Link] file is loaded into a web application using Joblib or Pickle, allowing users to get
predictions instantly.
Simple Example
• Serialization: Train a model → Save it as [Link].
• Deserialization: Load [Link] → Use it to predict new data.
Flow:
Training → Serialization → [Link] → Deserialization → Prediction
Simple Example
• Serialization: Train a model → Save it as [Link].
• Deserialization: Load [Link] → Use it to predict new data.
Flow:
Training → Serialization → [Link] → Deserialization → Prediction
Model Retraining
Introduction
Model Retraining is the process of training an already deployed machine learning model again using
new or updated data. It helps the model adapt to changes in data patterns and maintain high prediction
accuracy over time. Retraining is necessary because real-world data changes continuously, causing the
model's performance to degrade (known as data drift).
Prediction
After retraining, the model learns from the latest data and generates more accurate and reliable
predictions on new unseen data.
Example:
• A fraud detection model retrained with recent transaction data can detect new fraud patterns
more effectively.
• A recommendation system retrained with recent user activity provides better product
recommendations.
Accuracy
Over time, model accuracy may decrease because user behavior and data patterns change.
Retraining helps to:
• Improve prediction accuracy.
• Reduce errors caused by data drift.
• Adapt to new trends and patterns.
• Keep the model reliable in production.
Example
A spam email detection model was trained using old email data. As spammers start using new
techniques, the model's accuracy decreases. By retraining the model with recent email data, it learns
the new spam patterns and improves its prediction accuracy.
Characteristics
• Processes large amounts of data at once.
• Predictions are not generated immediately.
• Suitable for scheduled jobs.
Applications
• Monthly sales forecasting
• Daily recommendation generation
• Payroll processing
• Business analytics
Advantages
• Efficient for large datasets.
• Lower operational cost.
• Easy to manage.
Limitations
• High latency.
• Not suitable for real-time applications.
Characteristics
• Instant predictions.
• Very low latency.
• Uses APIs for communication.
Applications
↓
Prediction
Characteristics
• Instant predictions.
• Very low latency.
• Uses APIs for communication.
Applications
• Chatbots
• Fraud detection
• Face recognition
• Recommendation systems
Advantages
• Fast response.
• Real-time decision making.
• Better user experience.
Limitations
• Requires continuous availability.
• Higher infrastructure cost.
3. Edge Deployment
Definition
In Edge Deployment, the ML model runs directly on local devices such as smartphones, IoT devices, or
embedded systems instead of a cloud server.
Workflow
Sensor/Input
↓
Local Device
(ML Model)
↓
Prediction
Characteristics
• Prediction happens on the device itself.
• Does not require continuous internet.
• Very low latency.
Benefits
• Fast response.
• Offline operation.
• Better privacy and security.
• Reduced network usage.
Limitations
• Limited hardware resources.
• Smaller model size required.
• Difficult to update models.
Applications
• Face Unlock
• Self-driving cars
• Smart cameras
• IoT devices
4. Stream Deployment
Definition
In Stream Deployment, the ML model continuously processes incoming data streams and generates
predictions in real time.
Workflow
Continuous Data Stream
↓
Stream Processing Engine
↓
ML Model
↓
Prediction
Characteristics
• Handles continuous real-time data.
• Processes events as they arrive.
• Suitable for high-speed applications.
↓
ML Model
↓
Prediction
Characteristics
• Handles continuous real-time data.
• Processes events as they arrive.
• Suitable for high-speed applications.
Applications
• Sensor monitoring
• Financial transaction analysis
• Stock market prediction
• Real-time traffic monitoring
Advantages
• Continuous prediction.
• Low latency.
• Suitable for live data.
Limitations
• Complex implementation.
• High computational requirements.
1. Monolithic Architecture
Definition
In Monolithic Architecture, all components of the application such as the User Interface (UI), Business Logic
(Backend), ML Model, and Database are bundled into a single application.
Architecture
-----------------------------------------
| UI | Business Logic | ML Model | Database |
-----------------------------------------
Working
• User sends a request.
• The application processes the request.
• The ML model generates a prediction.
• The result is returned to the user.
Characteristics
• Single deployable application.
• All components are tightly coupled.
• Easy communication between components.
Advantages
• Easy to develop and deploy.
• Low latency between components.
• Simple testing and debugging.
• Suitable for small applications.
Disadvantages
• Difficult to scale individual components.
• Any update requires redeploying the entire application.
• Failure in one component can affect the whole system.
• Difficult to maintain as the application grows.
Applications
• Small ML projects
• College projects
• Internal business applications
2. Microservices Architecture
Definition
In Microservices Architecture, the application is divided into small, independent services, where each service
performs a specific task and communicates with others using APIs.
Architecture
Frontend
↓
API Gateway
↓
ML Model Service
↓
Database
Working
• User request goes to the API Gateway.
• The gateway forwards the request to the required service.
• The ML model service processes the input.
• Prediction is returned through the API Gateway.
Characteristics
• Independent services.
• API-based communication.
• Each service can be developed and deployed separately.
Advantages
• User request goes to the API Gateway.
• The gateway forwards the request to the required service.
• The ML model service processes the input.
• Prediction is returned through the API Gateway.
Characteristics
• Independent services.
• API-based communication.
• Each service can be developed and deployed separately.
Advantages
• Easy to scale individual services.
• Better fault isolation.
• Flexible technology choices.
• Faster development and deployment.
Disadvantages
• Complex architecture.
• Higher operational and maintenance cost.
• Network latency between services.
• Difficult service management.
Applications
• E-commerce platforms
• Banking systems
• Large enterprise applications
• Cloud-based ML services
3. Serverless Architecture
Definition
Serverless Architecture allows developers to deploy ML applications without managing servers. The cloud
provider automatically handles infrastructure, scaling, and resource management. Code runs only when
triggered by an event.
Architecture
User Request
↓
Cloud Function (AWS Lambda)
↓
ML Model
↓
Prediction
Working
• User sends a request.
• A cloud function (e.g., AWS Lambda) is triggered.
• The function loads the ML model.
• The prediction is generated and returned.
• Resources are released after execution.
Characteristics
• Event-driven execution.
• Automatic scaling.
• No server management.
• Pay only for execution time.
Advantages
• No infrastructure management.
• Automatic scaling.
• Cost-effective (Pay-as-you-go).
• Fast deployment.
Disadvantages
• Cold start latency.
• Limited execution time.
• Vendor lock-in.
• Limited customization.
Applications
• Image processing
• Document processing
• Chatbots
• Event-driven ML applications
Selection of Architecture
Requirement Recommended Architecture
Small application Monolithic
Large scalable application Microservices
Event-driven or unpredictable workload Serverless
Real-time applications Microservices or Serverless
Cost optimization Serverless
1. Scalability
Definition
Scalability is the ability of an ML system to handle increasing workloads, users, data, or prediction requests
without reducing performance.
Achieved Using
• Cloud Infrastructure
• Auto-Scaling
• Load Balancing
Example
An e-commerce website receives millions of users during a festival sale. The deployment system
automatically increases computing resources to handle the traffic.
Importance
• Handles high traffic
• Improves performance
• Supports business growth
2. Latency
Definition
Latency is the time taken by the deployed model to return a prediction after receiving a request.
Characteristics
• Very important for real-time applications
• Lower latency means faster predictions
Example
A fraud detection system must identify fraudulent transactions within milliseconds.
Importance
• Faster response time
• Better user experience
• Essential for real-time decision-making
• Very important for real-time applications
• Lower latency means faster predictions
Example
A fraud detection system must identify fraudulent transactions within milliseconds.
Importance
• Faster response time
• Better user experience
• Essential for real-time decision-making
3. Reliability
Definition
Reliability is the ability of an ML deployment system to operate continuously without failures and provide
consistent predictions.
Includes
• High Availability
• Fault Tolerance
• Disaster Recovery
Example
A hospital's disease prediction system should remain available 24×7 without downtime.
Importance
• Continuous service
• Reduced downtime
• Consistent performance
4. Security
Definition
Security protects the ML model, data, APIs, and users from unauthorized access and cyber attacks.
Security Measures
• Authentication & Authorization
• Data Encryption
• Secure APIs
• Access Control
Example
A banking ML system encrypts customer data and allows only authorized users to access prediction APIs.
Importance
• Protects sensitive data
• Prevents cyber attacks
• Ensures data privacy
Summary Table
Requirement Description Example
Scalability Handles increasing users and requests E-commerce website during sales
Latency Fast prediction response time Fraud detection
Reliability Continuous operation with minimal downtime Hospital ML system
Security Protects data and APIs Banking applications
2. Container-Based Deployment
Definition
In Container-Based Deployment, the model and all its dependencies are packaged into a container, ensuring
consistent execution across different environments.
Tools
• Docker
• Kubernetes
Benefits
• Easy deployment
• High portability
• Automatic scaling
• Consistent environment
Example
A recommendation system is packaged in a Docker container and deployed on multiple cloud servers.
3. Cloud-Based Deployment
Definition
In Cloud-Based Deployment, the trained model is hosted on cloud platforms that provide scalable
infrastructure and services for prediction.
Common Cloud Platforms
• Amazon Web Services (AWS)
• Microsoft Azure
• Google Cloud Platform (GCP)
Steps
1. Package the trained model.
2. Upload it to the cloud.
3. Deploy using cloud services.
4. Integrate with web or mobile applications.
5. Monitor and update the model.
Example
A hospital deploys its disease prediction model on AWS so doctors can access predictions from anywhere.