Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 05

Statistics

Use mean, median, mode, standard deviation, and the normal distribution to describe and evaluate machine learning data.

Beginner35 min

mean

The mean is the first statistical concept you must master before learning ML.

Mean is the average value of a dataset.

In AI and Machine Learning, the mean is used to summarize data, fill missing values, normalize features, and evaluate model performance.

Intuition: Average house price

Imagine your real estate app has 5 property prices.

  • 50L
  • 60L
  • 70L
  • 80L
  • 90L

Question:

What is the average property price?

The answer is the mean.

Mean Formula

Equation Mean = (Sum of all values)/(Number of values)

Or mathematically:

Equation μ = (Σ x)/(n)
Symbol Meaning
μ Mean
Σ x Sum of all values
n Total number of values

Step-by-step calculation

Dataset:

Equation 50, 60, 70, 80, 90

Step 1: Add all values

Equation 50 + 60 + 70 + 80 + 90 = 350

Step 2: Count values

Equation n = 5

Step 3: Divide

Equation (350)/(5) = 70

Mean = ₹70 Lakhs

Visual intuition

Think of the mean as the balance point.

Diagram

The mean is the center of the data.

Example 2: Student Marks

Student Marks
A 80
B 70
C 90
D 60

Total:

Equation 80 + 70 + 90 + 60 = 300

Mean:

Equation (300)/(4) = 75

Average marks = 75

ML Use Case 1: Data Preprocessing

Suppose your dataset has missing values.

House Area
H1 1200
H2 1500
H3 ?
H4 1800

First calculate the mean.

Equation (1200 + 1500 + 1800)/(3) = 1500

Fill the missing value:

House Area
H1 1200
H2 1500
H3 1500
H4 1800

This is called Mean Imputation.

Many ML pipelines use this before training.

ML Use Case 2: Feature Normalization

Suppose areas are:

House Area
H1 1200
H2 1500
H3 1800

Mean:

Equation 1500

Center the data:

House New Value
H1 -300
H2 0
H3 +300

The data is now centered around 0.

This improves gradient descent.

ML Use Case 3: Model Evaluation

A model predicts errors:

House Error
1 2
2 4
3 6
4 8

Mean Error:

Equation (2 + 4 + 6 + 8)/(4) = 5

Average error = 5

Many evaluation metrics begin with calculating a mean.

Examples:

  • Mean Absolute Error (MAE)
  • Mean Squared Error (MSE)
  • Root Mean Squared Error (RMSE)

ML Use Case 4: ChatGPT Embeddings

An embedding has hundreds of numbers.

Example:

Equation 0.2, 0.4, 0.6, 0.8

Mean:

Equation (0.2 + 0.4 + 0.6 + 0.8)/(4) = 0.5

The mean helps normalize vectors before further processing.

While LLMs use more advanced normalization methods, the underlying idea of centering data comes from the mean.

Python Example

python
import numpy as np

prices = [50, 60, 70, 80, 90]

mean = np.mean(prices)

print(mean)

Output:

text
70.0

Mean vs Sum

Sum Mean
Total of values Average of values
350 70
Depends on dataset size Independent of size

Example:

Dataset A:

50, 60, 70

Sum = 180

Mean = 60

Dataset B:

50, 60, 70, 60, 60

Sum = 300

Mean = 60

Different sums can have the same mean.

Real Estate Scenario

Your property database:

Area Price
1200 65L
1500 82L
1800 98L
2100 1.2Cr

Average area:

Equation 1650 sq.ft

Average price:

Equation 91.25 Lakhs

Your dashboard KPI could display:

  • Average Property Price: ₹91.25L
  • Average Area: 1650 sq.ft

These are simple mean calculations.

Summary

Concept Formula
Mean (Σx) / n
Sum Add all values
n Number of observations
Output Average

Memory Trick

Mean = Total ÷ Count

If someone asks:

"What's the average?"

They're asking for the mean.

Interview Question

Q: Why is the mean important in Machine Learning?

Answer: The mean summarizes the center of a dataset and is widely used for feature engineering, missing value imputation, normalization, and evaluation metrics such as MAE, MSE, and RMSE.

Median

After Mean, the next most important statistical measure is the Median.

Median is the middle value of a sorted dataset.

Unlike the mean, the median is not affected much by extremely high or low values (outliers). That's why it's widely used in AI, ML, and real estate analytics.

Intuition: House prices

Imagine 5 property prices.

  • 50L
  • 60L
  • 70L
  • 80L
  • 90L

The data is already sorted.

The middle value is 70L.

Median = ₹70 Lakhs

Median Formula

There isn't one single arithmetic formula like the mean. Instead, follow these rules:

If the number of values is odd

Equation Median = Middle Value

Example:

Equation 10, 20, 30, 40, 50

Middle = 30

If the number of values is even

Take the average of the two middle values.

Equation 10, 20, 30, 40

Middle values:

20 and 30

Equation (20 + 30)/(2) = 25

Median = 25

Step-by-step Example (Odd)

Dataset:

Equation 90, 60, 50, 80, 70

Step 1: Sort

Equation 50, 60, 70, 80, 90

Step 2: Find the middle

Equation 70

Median = 70

Step-by-step Example (Even)

Dataset:

Equation 40, 10, 30, 20

Sort:

Equation 10, 20, 30, 40

Middle values:

20 and 30

Equation (20 + 30)/(2) = 25

Median = 25

Visual intuition

The median always splits the dataset into two equal halves.

Diagram

Everything on the left is smaller.

Everything on the right is larger.

Mean vs Median (Most Important)

Suppose one luxury villa enters the dataset.

Prices
50
60
70
80
500

Mean

Equation (50 + 60 + 70 + 80 + 500)/(5) = 152

Mean = 152L

Median

Sorted:

50, 60, 70, 80, 500

Median = 70L

Comparison

Measure Value Meaning
Mean 152L Pulled upward by the luxury villa
Median 70L Still represents the typical property

This is why real estate companies often report median home price instead of average.

ML Use Case 1: Outlier Handling

Imagine property areas.

House Area
H1 1200
H2 1250
H3 1300
H4 1280
H5 12000

The last house is a mansion.

  • Mean becomes unrealistically high.
  • Median stays around 1280.

Many ML preprocessing pipelines use the median to represent skewed data.

ML Use Case 2: Missing Value Imputation

Dataset:

House Bedrooms
H1 2
H2 3
H3 ?
H4 3
H5 10

The value 10 is an outlier.

Sorted:

2, 3, 3, 10

Median:

Equation (3 + 3)/(2) = 3

Fill the missing value with 3, not the mean.

This is called Median Imputation.

ML Use Case 3: Salary Prediction

Employee salaries:

Salary
3 L
4 L
5 L
6 L
80 L
  • Mean = 19.6L ❌
  • Median = 5L ✅

The median better represents a typical employee.

This is common in HR analytics.

ML Use Case 4: Your Real Estate Dashboard

Suppose your portal has 10,000 properties.

Instead of showing:

Average Price = ₹2.1 Cr

You show:

Median Price = ₹82 L

This gives users a more realistic market picture because luxury properties don't distort the statistic.

Python Example

python
import numpy as np

prices = [50, 60, 70, 80, 500]

print(np.median(prices))

Output:

text
70.0

Mean vs Median vs Outlier

Dataset Mean Median
50, 60, 70, 80, 90 70 70
50, 60, 70, 80, 500 152 70

Key observation: The median hardly changes, while the mean changes dramatically.

When should ML use Mean vs Median?

Situation Use
Normal distribution Mean
Skewed data Median
Outliers present Median
Missing values with outliers Median
Average performance metrics Mean

Summary

Concept Meaning
Mean Average value
Median Middle value
Odd dataset Middle element
Even dataset Average of two middle values
Best for outliers Median

Memory Trick

Mean = Balance point

Median = Middle person in the line

If your dataset contains luxury houses, billionaire salaries, or other extreme values, the median is usually the better statistic because it represents the typical observation more accurately.

Interview Question

Q: Why is median preferred over mean in many ML datasets?

Answer: Because the median is robust to outliers. Extreme values can significantly distort the mean, while the median continues to represent the center of the data, making it useful for skewed distributions and missing-value imputation.

mode

After Mean and Median, the third measure of central tendency is Mode.

Mode is the value that appears most frequently in a dataset.

Unlike mean and median, mode doesn't care about the average or the middle—it only looks for the most repeated value.

Intuition: Most common BHK in your property app

Imagine your portal has 10 property listings.

  • 2 BHK
  • 3 BHK
  • 3 BHK
  • 1 BHK
  • 3 BHK
  • 2 BHK
  • 3 BHK
  • 4 BHK
  • 2 BHK
  • 3 BHK

Count each category:

BHK Frequency
1 1
2 3
3 5
4 1

Mode = 3 BHK

Because it appears 5 times.

Mode Formula

There is no mathematical equation like mean.

The rule is simply:

Mode = Value with the highest frequency

Example:

Equation 2, 3, 3, 4, 5, 3, 6

Count:

  • 2 → 1
  • 3 → 3
  • 4 → 1
  • 5 → 1
  • 6 → 1

Mode = 3

Step-by-step Example

Dataset:

Equation 5, 7, 8, 7, 9, 7, 6

Step 1: Count frequency

Value Count
5 1
6 1
7 3
8 1
9 1

Step 2: Find the largest count

Highest frequency = 3

Mode = 7

Visual intuition

Mode is simply the tallest bar.

Diagram

The highest bar represents the mode.

Types of Mode

1. Unimodal (One Mode)

Only one value repeats most.

Equation 1, 2, 2, 3, 4

Mode = 2

2. Bimodal (Two Modes)

Two values have the same highest frequency.

Equation 1, 2, 2, 3, 3, 4

Modes = 2 and 3

3. Multimodal

More than two values repeat equally.

Equation 1, 1, 2, 2, 3, 3

Modes = 1, 2, 3

Mean vs Median vs Mode

Consider this dataset:

Equation 50, 60, 70, 70, 90
Measure Value
Mean 68
Median 70
Mode 70

They answer different questions:

  • Mean → Average
  • Median → Middle
  • Mode → Most common

ML Use Case 1: Missing Value Imputation

Suppose property type is missing.

Property Type
P1 Apartment
P2 Villa
P3 Apartment
P4 ?
P5 Apartment

Most frequent value:

Apartment

Fill the missing category with the mode.

This is called Mode Imputation and is commonly used for categorical features.

ML Use Case 2: Customer Behavior

E-commerce purchases:

Category Frequency
Shoes 50
Bags 20
Watches 15

Mode = Shoes

The business learns the most popular product category.

Recommendation systems often begin with this insight.

ML Use Case 3: Real Estate Analytics

Your dashboard can show:

Property Type Listings
Apartment 520
Villa 180
Plot 95
Penthouse 30

Mode = Apartment

Instead of saying "average property type" (which makes no sense), you report the most common property type.

ML Use Case 4: Image Classification

Suppose an ensemble of 5 AI models predicts:

Model Prediction
M1 Cat
M2 Cat
M3 Dog
M4 Cat
M5 Dog

Predictions:

  • Cat → 3
  • Dog → 2

Mode = Cat

The ensemble chooses the majority prediction.

This is called majority voting.

Python Example

python
from statistics import mode

bhk = [2, 3, 3, 1, 3, 2, 3, 4, 2, 3]

print(mode(bhk))

Output:

text
3

When should ML use Mean, Median, or Mode?

Data Type Best Measure
Numerical (normal) Mean
Numerical (outliers) Median
Categorical Mode
Most frequent class Mode
Missing category Mode

Summary

Concept Meaning
Mean Average value
Median Middle value
Mode Most frequent value
Best for Categorical data

Memory Trick

Mean = Average

Median = Middle

Mode = Most Repeated

Interview Question

Q: Why is mode important in Machine Learning?

Answer: Mode is the preferred measure for categorical variables. It is widely used for missing-value imputation, identifying the most common class, customer behavior analysis, and majority voting in ensemble learning.

standard deviation

After Mean, Median, and Mode, the next essential concept is Standard Deviation.

Standard Deviation measures how spread out the data is from the mean.

In simple words:

  • Low standard deviation → Values are close together.
  • High standard deviation → Values are widely spread apart.

This is one of the most important statistics used in AI and ML for normalization, anomaly detection, and feature scaling.

Intuition: Two classrooms

Both classrooms have the same mean = 70.

Class A

Diagram

Low SD

Class B

Diagram

High SD

Although both averages are 70, their distributions are completely different.

Standard deviation captures that difference.

What does “spread” mean?

Imagine property prices.

Dataset A

Equation 68, 69, 70, 71, 72

Mean = 70

Everything is close.

Dataset B

Equation 20, 40, 70, 100, 120

Mean = 70

Same mean.

But values are spread everywhere.

Therefore:

  • Dataset A → Low SD
  • Dataset B → High SD

Standard Deviation Formula

Population formula:

Equation σ = √((Σ(x - μ)²)/(N))

Don't memorize it immediately. Understand each part.

Symbol Meaning
x Data value
μ Mean
N Number of values
σ Standard deviation

Step-by-step Calculation

Dataset:

Equation 68, 69, 70, 71, 72

Step 1: Find Mean

Equation (68 + 69 + 70 + 71 + 72)/(5) = 70

Step 2: Subtract Mean

Value Difference
68 -2
69 -1
70 0
71 1
72 2

Step 3: Square

Difference Square
-2 4
-1 1
0 0
1 1
2 4

Total:

Equation 4 + 1 + 0 + 1 + 4 = 10

Step 4: Divide

Equation (10)/(5) = 2

Step 5: Square Root

Equation √(2) ≈ 1.41

Standard Deviation = 1.41

A very small value means the data is tightly grouped.

Visual intuition

Diagram

Small spread = Small SD.

ML Use Case 1: Feature Standardization

Suppose your dataset:

Feature Value
Area 2000
Bedrooms 3

These scales are very different.

We standardize using:

Equation z = (x - μ)/(σ)

Example:

Area:

  • Mean = 1500
  • SD = 250
  • Value = 2000
Equation (2000 - 1500)/(250) = 2

The standardized value becomes 2.

Now different features become comparable.

This is called Z-score normalization.

ML Use Case 2: Anomaly Detection

Transaction amounts:

Amount
500
550
600
520
50000

Mean increases.

But SD becomes huge.

The value 50000 is many standard deviations away.

AI flags it as an anomaly.

Banks use this for fraud detection.

ML Use Case 3: Real Estate Price Analysis

Your portal has prices.

Low SD means:

  • Stable neighborhood
  • Consistent pricing
  • Predictable market

Another area:

High SD means:

  • Mixed luxury & budget homes
  • Volatile pricing
  • Larger prediction uncertainty

ML Use Case 4: Image Processing

Pixel brightness:

Equation 120, 122, 121, 119, 123

Low SD:

Image is smooth.

Another image:

Equation 20, 220, 30, 210, 25

High SD:

Large contrast.

Computer vision uses this idea to detect texture and contrast.

Python Example

python
import numpy as np

data = [68, 69, 70, 71, 72]

print(np.std(data))

Output:

text
1.414213562

Mean vs Standard Deviation

Statistic Tells You
Mean Center of data
Median Middle value
Mode Most frequent
Standard Deviation Spread of data

Think of it this way:

  • Mean = Where is the center?
  • Standard Deviation = How far do values move away from that center?

AI & ML Applications

Application Why SD Matters
Feature Scaling Standardize features
Gradient Descent Faster convergence
Fraud Detection Detect outliers
Image Processing Measure contrast
Finance AI Measure volatility
Real Estate AI Price variability

Summary

Concept Meaning
Mean Average
Variance Average squared distance
Standard Deviation Typical distance from mean
Low SD Values close together
High SD Values spread apart

Memory Trick

Mean tells you the center.

Standard Deviation tells you how much the data dances around that center.

Interview Question

Q: Why is standard deviation important in Machine Learning?

Answer: Standard deviation measures feature variability. It is essential for feature standardization (Z-score), detecting outliers, improving optimization during training, and understanding the spread and stability of real-world datasets.

normal distribution

Normal Distribution is the most important statistical distribution in Machine Learning. Many ML algorithms assume data follows (or approximately follows) this bell-shaped pattern.

Normal Distribution = A probability distribution where most values cluster around the mean, and fewer values appear as you move away from the mean.

It is also called the Bell Curve or Gaussian Distribution.

Intuition: Student heights

Imagine measuring the heights of 10,000 students.

Most students will be around the average height.

Very short and very tall students are much fewer.

Diagram

The center of the curve is the mean.

The farther you move from the mean, the fewer observations you find.

Why is it called a Bell Curve?

Because its shape looks like a bell.

  • Highest point = Mean
  • Left and right sides are symmetric
  • Tails become thinner
Diagram

The Normal Distribution Formula

Don't memorize it yet—understand what it represents.

Equation normal distribution PDF
Symbol Meaning
μ Mean (center)
σ Standard deviation
x Data value
π Constant (3.14159)
e Mathematical constant

For beginners, the important idea is:

Mean decides the center, Standard Deviation decides the width.

Mean vs Standard Deviation

Imagine two bell curves.

Diagram

Both have the same mean.

But:

  • Blue → Small SD (narrow)
  • Orange → Large SD (wide)

The standard deviation controls how spread the data is.

The 68–95–99.7 Rule

This is the most important rule.

Diagram
Range Data Inside
μ ± 1σ 68%
μ ± 2σ 95%
μ ± 3σ 99.7%

Example:

Mean height = 170 cm

SD = 10 cm

Range Percentage
160–180 68%
150–190 95%
140–200 99.7%

Almost everyone falls within 3 standard deviations.

ML Use Case 1: Feature Standardization

Suppose house areas:

House Area
H1 1200
H2 1500
H3 1800

Mean = 1500

SD = 300

Z-score:

Equation z = (x - μ)/(σ)

For 1800:

Equation (1800 - 1500)/(300) = 1

The value is 1 SD above the mean.

This makes different features comparable.

ML Use Case 2: Anomaly Detection

Credit card transactions:

Transaction Amount
T1 500
T2 550
T3 520
T4 600
T5 50,000

The ₹50,000 transaction is far from the mean.

It may be 4 or 5 SD away.

Banks flag such transactions as suspicious.

ML Use Case 3: Image Processing

Pixel brightness usually follows an approximately normal distribution.

Diagram

Computer vision models normalize pixel values assuming similar distributions.

This improves training stability.

ML Use Case 4: Your Real Estate AI

Suppose apartment prices in Whitefield:

Range Count
40–60L 8
60–80L 22
80–100L 35
100–120L 20
120–140L 7

Most listings are around ₹80–100L.

Few are very cheap or very expensive.

A pricing model can learn this natural distribution.

Python Example

python
import numpy as np

heights = [160, 165, 170, 175, 180]

print(np.mean(heights))
print(np.std(heights))

Output:

text
170.0
7.07

The mean is 170, and values typically vary by about 7 cm.

Normal Distribution vs Uniform Distribution

Normal

Diagram

Most values in the center.

Uniform

Diagram

Every value is equally likely.

Why is Normal Distribution important in ML?

ML Area Why it's Used
Feature Scaling Standardization uses mean & SD
Linear Regression Often assumes normally distributed errors
Anomaly Detection Detect values far from the mean
Computer Vision Normalize pixel distributions
Finance AI Model returns and volatility
Quality Control Detect abnormal measurements

Summary

Concept Meaning
Mean (μ) Center of the curve
Standard Deviation (σ) Width of the curve
Bell Curve Symmetric normal distribution
68–95–99.7 Rule Percentage within 1σ, 2σ, 3σ

Memory Trick

Mean decides where the bell stands.

Standard deviation decides how wide the bell spreads.

Interview Question

Q: Why is normal distribution important in Machine Learning?

Answer: Many ML algorithms assume data or model errors are approximately normally distributed. It enables feature standardization, anomaly detection, statistical inference, confidence intervals, and more stable model training through Z-score normalization.