Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Machine Learning Notes
Topic #323

Mean

The mean (average) is the sum of all values divided by how many there are. It's the single most-used summary statistic in ML — and also one of the easiest to misuse, because it hides how the data is actually spread out.

What the Mean Actually Represents

Geometrically, the mean is the balance point of your data — if you placed every data point as a weight on a number line, the mean is exactly where you'd need to place a fulcrum for the line to balance perfectly. This is a more useful mental model than "just add and divide," because it explains, intuitively, why one extreme value can drag the mean so far.

Formula — Population Mean vs Sample Mean

\[ \mu = \frac{1}{N}\sum_{i=1}^{N}x_i \qquad \text{(population mean)} \] \[ \bar{x} = \frac{1}{n}\sum_{i=1}^{n}x_i \qquad \text{(sample mean)} \]

\(x_i\) is the \(i\)-th value, \(N\) is the size of the entire population, \(n\) is the size of the sample you actually have. The formulas are mechanically identical — the distinction matters because in almost every real ML project, you only ever have a sample (your training data) and are using \(\bar{x}\) as an estimate of the true, unknown \(\mu\) of the underlying process generating that data.

Graphical Intuition — The Balance Point

mean = balance point

The mean is the point where the data "balances" — it factors in every value's exact position, not just rank.

Step-by-Step Numerical Example

Seven students' exam scores: \(62, 68, 68, 72, 75, 80, 85\).

StepCalculation
1. Sum all values\(62+68+68+72+75+80+85 = 510\)
2. Count the values\(n = 7\)
3. Divide\(\bar{x} = 510 / 7 \approx 72.86\)

The average score is about 72.86 — notice this isn't any actual student's score, which is completely normal for a mean.

Second Example — Where the Mean Gets Misleading

Five people's monthly incomes (in rupees): \(30000, 32000, 35000, 40000, 500000\).

\[ \bar{x} = \frac{30000+32000+35000+40000+500000}{5} = \frac{637000}{5} = 127400 \]

The mean, ₹127,400, is higher than four out of five actual incomes in the dataset — a single high earner pulled the average far above what's "typical." This is exactly why the mean alone can be a misleading summary for skewed data, and why it's almost always reported alongside the median in practice.

Python Implementation

# From scratch
scores = [62, 68, 68, 72, 75, 80, 85]
mean_from_scratch = sum(scores) / len(scores)
print(mean_from_scratch)   # 72.857142857...

# NumPy / Pandas — what you'd actually use
import numpy as np
import pandas as pd

print(np.mean(scores))              # 72.857142857...
print(pd.Series(scores).mean())      # 72.857142857...

# On a full DataFrame, per column
df = pd.DataFrame({"score": scores, "income": [30000, 32000, 35000, 40000, 500000, 45000, 38000]})
print(df.mean(numeric_only=True))

Mean vs Median vs Mode — At a Glance

MeanMedianMode
Uses every value's exact position?YesNo (only rank)No (only frequency)
Sensitive to outliers?VeryBarelyNo
Works on categorical data?NoNo (needs order)Yes
Best summary forSymmetric, outlier-free numeric dataSkewed numeric dataCategorical / discrete data

Why This Matters for ML

  • Mean imputation is one of the most common ways to fill missing numeric values
  • Standardization centers every feature around its mean (subtracting \(\mu\)) before scaling by \(\sigma\)
  • A model's average error across all predictions (like MSE or MAE) is literally a mean, applied to per-sample errors instead of raw data values
  • Baseline models often start by predicting the mean for every input — a useful "can my real model beat just guessing the average?" sanity check

Advantages

  • Uses every data point, not just rank or frequency, so it reflects the full dataset's magnitude
  • Has convenient mathematical properties (e.g. it's what minimizes total squared distance to every point) that make it useful inside optimization and loss functions

Limitations

  • Extremely sensitive to outliers — a single extreme value can shift it dramatically, as the income example shows
  • Not meaningful for purely categorical data (you can't "average" city names)
  • Can misrepresent a skewed distribution as if it were centered, when most of the actual data sits elsewhere

Common Mistakes

  • Reporting only the mean for a skewed feature (like income or house prices) without checking the median alongside it.
  • Mean-imputing missing values in a skewed or outlier-heavy column, silently injecting unrealistic values — see Missing Value Imputation.
  • Computing the mean of a categorical column that's been label-encoded as if the resulting number were meaningful — it usually isn't.
  • Confusing population mean (\(\mu\)) and sample mean (\(\bar{x}\)) notation in a way that misleads about whether you're describing your whole dataset or estimating a broader truth from a sample.

Interview Relevance

Q: "When would you prefer the median over the mean?" When the data is skewed or contains outliers — the mean gets pulled toward extreme values while the median stays representative of a "typical" observation, as shown directly by the income example above.

Q: "Why is mean imputation risky for a skewed feature?" Because the mean of a skewed feature isn't representative of most actual values — filling missing entries with it systematically biases the distribution toward the extreme that's pulling the mean, distorting whatever pattern a model would otherwise learn.

Practice Question

Given the dataset \([12, 15, 14, 10, 90]\), compute the mean by hand. Then explain, without recomputing, why the median would likely give a more "typical" summary of this data.

Related ML Notes

Want to go beyond the notes?

Join CodingNow's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →