๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #40

Variance & Standard Deviation

If the expected value tells you the center of a distribution, variance and standard deviation tell you its spread โ€” how far values typically land from that center. This spread is exactly what feature normalization and batch normalization are designed to control.

Formulas

\[ \text{Var}(X) = \mathbb{E}\big[(X-\mathbb{E}[X])^2\big], \qquad \sigma = \sqrt{\text{Var}(X)} \]

Variance is the expected squared deviation from the mean โ€” squaring ensures negative and positive deviations don't cancel out. Standard deviation \(\sigma\) is the square root of variance, which brings the units back to match the original data (variance is in squared units, which is harder to interpret directly).

Numerical Example

Data: \([2, 4, 4, 4, 5, 5, 7, 9]\). Mean \(=5\). Deviations from the mean: \([-3,-1,-1,-1,0,0,2,4]\). Squared deviations: \([9,1,1,1,0,0,4,16]\). Variance \(= \frac{9+1+1+1+0+0+4+16}{8} = \frac{32}{8}=4\). Standard deviation \(=\sqrt{4}=2\).

Code

import numpy as np
data = np.array([2, 4, 4, 4, 5, 5, 7, 9])
print(np.var(data))   # 4.0
print(np.std(data))    # 2.0

Why This Matters for Feature Scaling

Features with very different scales (e.g. "age in years" vs "income in dollars") have very different variances, which can make gradient descent converge unevenly โ€” the loss surface becomes elongated in some directions and steep in others. Standardizing a feature to zero mean and unit variance, \(z=\frac{x-\mu}{\sigma}\), is exactly what removes this imbalance before training, and it's the same formula batch normalization applies internally to layer activations (see Batch Normalization).

Population vs Sample Variance

\[ \text{Population: } \sigma^2 = \frac{1}{n}\sum_i (x_i-\mu)^2 \qquad \text{Sample: } s^2 = \frac{1}{n-1}\sum_i (x_i-\bar{x})^2 \]

When estimating variance from a data sample (which describes essentially all real training data), dividing by \(n-1\) instead of \(n\) corrects a small bias โ€” this is why np.var(data, ddof=1) exists as an option, though deep learning code frequently just uses the population formula (\(n\)) for simplicity in large-batch settings, where the difference is negligible.

Common Mistakes

  • Comparing variances across features measured in different units directly โ€” standard deviation is more interpretable since it's in the same units as the original feature, and normalized comparisons (like the coefficient of variation) are needed for cross-feature comparison.
  • Forgetting to compute normalization statistics (mean, std) only from the training set, then applying those same fixed values to the validation/test set โ€” computing them separately per split leaks information and gives an unrealistic performance estimate.

Interview Relevance

Q: "Why do we standardize features before training a neural network?" Features on very different scales create a loss surface with very different curvature in different directions, making gradient descent converge slowly or unevenly. Standardizing every feature to zero mean and unit variance keeps gradients well-scaled across all input dimensions, letting a single learning rate work well for the whole network.

Practice Question

Two features have the same mean but standard deviations of 1 and 50 respectively. Explain, using the standardization formula, what problem this could cause if left unaddressed before training.

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Variance & Standard Deviation โ€“ FAQs

Quick answers about learning Variance & Standard Deviation in Deep Learning.

This free note from CodingNow 2.0 explains Variance & Standard Deviation in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Variance & Standard Deviation, is 100% free with no signup required.
With focused practice, most students grasp Variance & Standard Deviation in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now