Skip to main content
Machine Learning (ML) is a branch of Artificial Intelligence where systems learn patterns from data and improve predictions or decisions without being explicitly programmed for every rule.

What is Machine Learning?

Traditional programming:
Machine Learning:
Example: Spam Email Detection Traditional approach:
Machine Learning approach:

Why Machine Learning?

Machine learning is useful when:
  • Rules are too complex
  • Huge amounts of data exist
  • Patterns continuously change
  • Manual programming becomes difficult
Examples:
  • Netflix recommendations
  • Self-driving cars
  • Fraud detection
  • Face recognition
  • ChatGPT
  • YouTube recommendations

Machine Learning Pipeline

Typical workflow:

Types of Machine Learning

Machine Learning mainly has three types:

1. Supervised Learning

What is Supervised Learning?

The model learns from labeled data. Labeled data:
Example:
The model learns:
After learning:
Model predicts answer.

Types of Supervised Learning

Two major types:

Regression

Predicts continuous numerical values. Examples:
  • House price prediction
  • Stock price prediction
  • Temperature prediction
  • Sales forecasting
Output:
Algorithms:
  • Linear Regression
  • Random Forest Regressor
  • XGBoost
  • Decision Tree Regressor

Classification

Predicts categories/classes. Examples:
  • Spam or Not Spam
  • Disease Detection
  • Fraud Detection
  • Cat vs Dog
Output:
Algorithms:
  • Logistic Regression
  • Decision Trees
  • Random Forest
  • SVM
  • Neural Networks

Supervised Learning Flow

Example: Student marks prediction: Predict:

Advantages

  • Easy to evaluate
  • High accuracy with enough labeled data
  • Clear target output

Disadvantages

  • Requires large labeled datasets
  • Labeling data is expensive
  • Can overfit

2. Unsupervised Learning

What is Unsupervised Learning?

Learns from unlabeled data. No correct outputs exist. Input:
Example: Customer purchase data:
Model discovers hidden patterns.

Types of Unsupervised Learning

Main categories:

Clustering

Groups similar data points. Example: Customer segmentation:
Algorithms:
  • K-Means
  • DBSCAN
  • Hierarchical Clustering
Applications:
  • Customer segmentation
  • Fraud detection
  • Image segmentation

Dimensionality Reduction

Reduces features while preserving information. Example: Dataset:
Some columns may be highly related. Reduce:
Algorithms:
  • PCA
  • t-SNE
Applications:
  • Faster training
  • Visualization
  • Noise reduction

Association Rule Learning

Finds relationships among items. Example:
Applications:
  • Product recommendation
  • Market basket analysis
Algorithms:
  • Apriori
  • FP Growth

Advantages

  • No labeled data needed
  • Finds hidden patterns

Disadvantages

  • Hard to evaluate
  • Interpretation may be difficult

3. Reinforcement Learning (RL)

What is Reinforcement Learning?

Learning by interacting with an environment. Agent learns through:

RL Components

Agent

The learner Examples:

Environment

Where agent acts. Examples:

State

Current situation. Example:

Action

What agent can do. Example:

Reward

Feedback after action. Example:

Reinforcement Learning Flow


Example: Self-driving Car

State:
Action:
Reward:
Wrong action:
Reward:

Algorithms

  • Q-Learning
  • Deep Q Network (DQN)
  • SARSA
  • PPO
  • Policy Gradient

Applications

  • Robotics
  • Self-driving vehicles
  • Game AI
  • Trading systems
  • Recommendation systems

Advantages

  • Learns automatically
  • Handles complex environments

Disadvantages

  • Requires large training time
  • High computational cost

Comparison of ML Types


Important ML Terms

Features

Input variables used for prediction. Example:

Labels

Expected output. Example:

Dataset

Collection of records. Example:

Training Data

Data used to train model.

Test Data

Data used to evaluate model.

Model

Mathematical representation learned from data.

Prediction

Output produced by model.

Overfitting

Model memorizes training data. Problem:

Underfitting

Model fails to learn patterns. Problem:

Train / Validation / Test, Overfitting, Bias–Variance Tradeoff


Why Do We Split Data?

Suppose we train a model using all available data:
Problem:
The model can show:
but for new unseen data:
To avoid this problem, data is split.

Dataset Split

Usually:
Common split:
or

Train Dataset

What is it?

Training data is used to teach the model patterns. Example: House Price Dataset: The model learns:

Purpose

  • Learn relationships
  • Update model weights
  • Fit the model

Example


Validation Dataset

What is it?

Validation data is used during training to check model performance and tune settings. Model does not learn from this data.

Purpose

Used for:
  • Hyperparameter tuning
  • Model selection
  • Detecting overfitting
Examples of hyperparameters:

Example

Try different values:
Choose the best validation performance.

Test Dataset

What is it?

Test data evaluates final performance. The model should never see test data during training.

Purpose

Measures:

Example:
Prediction:

Complete Flow


Train-Test Split Using Scikit-Learn

Code:
Possible Output:

Train / Validation / Test Split

Code:
Output:

Overfitting

What is Overfitting?

The model memorizes training data instead of learning general patterns.
Example: Training accuracy:
Testing accuracy:

Graph intuition:
Model becomes too complex and starts fitting noise.

Causes

  • Very complex model
  • Small dataset
  • Too many epochs
  • Too many features

Symptoms


Example

Training data: Model learns:
Instead of:

Reduce Overfitting

1. More data


2. Regularization

Adds penalty. Examples:

3. Dropout

Randomly removes neurons. Used in:

4. Reduce complexity

Example:

5. Early stopping

Stop training when validation performance stops improving.
Example code:

Underfitting

What is Underfitting?

Model fails to learn patterns.
Example: Training accuracy:
Testing accuracy:

Symptoms:

Causes:
  • Model too simple
  • Too few features
  • Insufficient training

Solutions:

Bias–Variance Tradeoff

This is one of the most important concepts in ML.

What is Bias?

Bias measures error due to overly simple assumptions. High bias:
Example: Trying to fit:
using:

Characteristics:

What is Variance?

Variance measures sensitivity to small data changes. High variance:
Characteristics:

Relationship


Bias–Variance Curve

Explanation: Left side:
Middle:
Right side:

Example

Suppose actual data:

High Bias

Model:
Too simple.

Balanced

Model:

High Variance

Model:

Visual Example with Polynomial Regression

Code:
Output:
If degree becomes very high:
Model starts overfitting.

Training vs Validation Loss

Good model:

Overfitting:
Training improves but validation worsens.

Quick Notes


PTR

Why validation set?
Why not use test data for training?
Signs of overfitting?
How to reduce overfitting?
Bias vs Variance