Foundations of Data Science 📂 Descriptive Statistics · 3 of 11 94 min read

What is Standard Deviation?

Standard deviation is the square root of variance and measures spread in the same unit as your data. Learn how to calculate it, what the 68-95-99.7 rule means, and how it powers machine learning through real stories.

🌐
SectionSection第 01 节

The Story That Explains Standard Deviationवह कहानी जो Standard Deviation समझाती है一个讲清标准差的故事

You are a quality control manager at a biscuit factory. Every biscuit should weigh exactly 20 grams. At the end of the day, two machines have both made biscuits with a mean weight of 20 g. Your boss is happy. But you check the individual weights.

आप एक biscuit factory में quality control manager हैं। हर biscuit का वज़न ठीक 20 grams होना चाहिए। दिन के अंत में दोनों machines के biscuits का mean वज़न 20 g है। आपके boss ख़ुश हैं। पर आप एक-एक biscuit का वज़न जाँचते हैं।

你是一家饼干厂的质检经理。每块饼干都应该正好重 20 克。一天结束时,两台机器生产的饼干平均重量都是 20 g。 老板很满意。但你去查看了每块饼干的重量。

BiscuitBiscuit饼干Machine A (g)Machine A (g)机器 A(g)Machine B (g)Machine B (g)机器 B(g)
119.814.0
220.126.0
320.018.0
419.922.0
520.220.0

Machine A: mean = 20 g, standard deviation = 0.16 g — nearly perfect every time.
Machine B: mean = 20 g, standard deviation = 4.47 g — all over the place.

Machine A: mean = 20 g, standard deviation = 0.16 g — हर बार लगभग perfect.
Machine B: mean = 20 g, standard deviation = 4.47 g — पूरी तरह बिखरा हुआ।

机器 A:均值 = 20 g,标准差 = 0.16 g —— 几乎每次都完美。
机器 B:均值 = 20 g,标准差 = 4.47 g —— 忽大忽小。

⚠️
Machine B Needs FixingMachine B को ठीक करना होगा机器 B 需要维修

A 14 g biscuit is underweight — customers complain. A 26 g biscuit breaks the packet and wastes ingredients. The mean looks fine, but the standard deviation shows that Machine B is broken. This is why standard deviation is at the heart of quality-control methods such as Six Sigma, used by many large manufacturers.

14 g का biscuit कम वज़न का है — customers शिकायत करते हैं। 26 g का biscuit packet तोड़ देता है और सामग्री बर्बाद करता है। Mean ठीक दिखता है, पर standard deviation बताता है कि Machine B ख़राब है। इसीलिए standard deviation Six Sigma जैसे quality-control तरीकों का केंद्र है, जिन्हें कई बड़े manufacturers इस्तेमाल करते हैं।

14 g 的饼干分量不足 —— 顾客会投诉;26 g 的饼干会撑破包装、浪费原料。均值看起来没问题,但标准差揭示了机器 B 坏了。 这就是为什么标准差是 六西格玛(Six Sigma)等质量控制方法的核心,许多大型制造企业都在使用。

LIVELIVE互动Run the Two Machines — Count the Rejectsदोनों Machines चलाएँ — Rejects गिनें开动两台机器 — 数一数不合格品

Each machine makes biscuits around 20 g. Anything outside 19–21 g is rejected (red). Change Machine B's standard deviation and press Play.

हर machine 20 g के आसपास biscuits बनाती है। 19–21 g से बाहर का हर biscuit reject होता है (लाल)। Machine B का standard deviation बदलें और Play दबाएँ।

每台机器生产约 20 g 的饼干,超出 19–21 g 的都算不合格(红色)。调整机器 B 的标准差,然后点击播放。

Machine A rejectsMachine A rejects机器 A 不合格率
—
Machine B rejectsMachine B rejects机器 B 不合格率
—


SectionSection第 02 节

What Standard Deviation Actually MeansStandard Deviation का असली मतलब标准差到底是什么意思

Standard deviation answers one simple question: "How far are the values from the mean, typically?" Small SD = values bunch together. Large SD = values spread out.

Standard deviation एक आसान सवाल का जवाब देता है: "Values आमतौर पर mean से कितनी दूर हैं?" छोटा SD = values पास-पास। बड़ा SD = values फैली हुई।

标准差回答一个简单的问题:“数值通常离均值有多远?”标准差小 = 数值挤在一起;标准差大 = 数值很分散。

🖼️ STANDARD DEVIATION IN TWO PICTURESदो तस्वीरों में STANDARD DEVIATION两幅图看懂标准差
Standard deviation in pictures SAME MEAN, DIFFERENT SDवही MEAN, अलग SD均值相同,标准差不同 Machine A: small SDMachine A: छोटा SD机器 A:标准差小 Machine B: large SDMachine B: बड़ा SD机器 B:标准差大 both centred on 20 gदोनों का केंद्र 20 g中心都在 20 g SD = THE SIDE OF THE VARIANCE SQUARESD = VARIANCE SQUARE की भुजा标准差 = 方差正方形的边长 area = varianceक्षेत्रफल = variance面积 = 方差 152.57 marks²152.57 marks²152.57 分² side = SD = √152.57 = 12.35 marksभुजा = SD = √152.57 = 12.35 marks边长 = 标准差 = √152.57 = 12.35 分 squared unitsquared unit平方单位 (hard to read)(पढ़ना मुश्किल)(难理解) same unit as thedata की ही与数据 data (easy)unit (आसान)同一单位(易懂)
Population Std DevPopulation Std Dev总体标准差
σ = √( Σ(x − μ)² / N )
Square root of the population variance. Use for complete datasets.
Population variance का square root. पूरे datasets के लिए।
总体方差的平方根。用于完整的数据集。
Sample Std DevSample Std Dev样本标准差
s = √( Σ(x − x̄)² / (n − 1) )
Square root of the sample variance. Use for sampled data (the usual case).
Sample variance का square root. Sample data के लिए (आम स्थिति)।
样本方差的平方根。用于抽样数据(最常见的情况)。
💡
Std Dev vs Variance — The Key DifferenceStd Dev vs Variance — मुख्य अंतर标准差与方差 — 关键区别

Standard deviation is the square root of variance. That one step brings the unit back to the unit of your data. Machine A's variance is 0.025 g² — hard to picture. Its standard deviation is 0.16 g, so you can say "the biscuits typically differ from 20 g by about 0.16 g". (Strictly, SD is the root-mean-square distance from the mean, so it is a little larger than the plain average distance — the next simulator shows both.)

Standard deviation variance का square root है। यह एक कदम unit को आपके data की unit में वापस ले आता है। Machine A का variance 0.025 g² है — समझना मुश्किल। इसका standard deviation 0.16 g है, तो आप कह सकते हैं "biscuits आमतौर पर 20 g से लगभग 0.16 g अलग होते हैं"। (सटीक रूप से, SD mean से root-mean-square दूरी है, इसलिए यह सीधी औसत दूरी से थोड़ा बड़ा होता है — अगला simulator दोनों दिखाता है।)

标准差就是方差的平方根。这一步把单位变回了数据本身的单位。机器 A 的方差是 0.025 g² —— 很难想象;它的标准差是 0.16 g,于是你可以说“饼干通常与 20 g 相差约 0.16 g”。 (严格地说,标准差是到均值距离的均方根,所以比普通的平均距离略大 —— 下一个模拟器会同时显示两者。)

LIVELIVE互动Drag the Scores — Watch the ±1 SD BandScores खींचें — ±1 SD पट्टी देखें拖动分数 — 观察 ±1 个标准差的范围带

The 8 exam scores from the next section. Blue dots are inside mean ± 1 SD, red dots outside. Drag a score far away, or add a 20, and see how the SD reacts.

अगले section के 8 exam scores. नीले dots mean ± 1 SD के अंदर हैं, लाल बाहर। किसी score को दूर खींचें, या 20 जोड़ें, और देखें SD कैसे बदलता है।

下一节的 8 个考试分数。蓝点在均值 ± 1 个标准差以内,红点在外面。把某个分数拖远一点,或加一个 20 分,看看标准差如何变化。

Sample SD (marks)Sample SD (marks)样本标准差(分)
—


SectionSection第 03 节

Step-by-Step CalculationStep-by-Step गणना分步计算

Dataset — exam scores of 8 students: [52, 74, 68, 90, 61, 85, 72, 78]

Dataset — 8 students के exam scores: [52, 74, 68, 90, 61, 85, 72, 78]

数据集 —— 8 名学生的考试分数:[52, 74, 68, 90, 61, 85, 72, 78]

🧮 Calculating Standard DeviationStandard Deviation निकालना计算标准差
Step 1
Calculate the mean.
(52 + 74 + 68 + 90 + 61 + 85 + 72 + 78) / 8 = 580 / 8 = 72.5
Mean निकालें।
(52 + 74 + 68 + 90 + 61 + 85 + 72 + 78) / 8 = 580 / 8 = 72.5
计算均值。
(52 + 74 + 68 + 90 + 61 + 85 + 72 + 78) / 8 = 580 / 8 = 72.5
Step 2
Subtract the mean and square each difference.
(52−72.5)² = 420.25   (74−72.5)² = 2.25   (68−72.5)² = 20.25   (90−72.5)² = 306.25
(61−72.5)² = 132.25   (85−72.5)² = 156.25   (72−72.5)² = 0.25   (78−72.5)² = 30.25
Mean घटाएँ और हर अंतर का square करें।
(52−72.5)² = 420.25   (74−72.5)² = 2.25   (68−72.5)² = 20.25   (90−72.5)² = 306.25
(61−72.5)² = 132.25   (85−72.5)² = 156.25   (72−72.5)² = 0.25   (78−72.5)² = 30.25
减去均值,再把每个差值平方。
(52−72.5)² = 420.25   (74−72.5)² = 2.25   (68−72.5)² = 20.25   (90−72.5)² = 306.25
(61−72.5)² = 132.25   (85−72.5)² = 156.25   (72−72.5)² = 0.25   (78−72.5)² = 30.25
Step 3
Add the squared differences.
420.25 + 2.25 + 20.25 + 306.25 + 132.25 + 156.25 + 0.25 + 30.25 = 1068
Squared अंतरों को जोड़ें।
420.25 + 2.25 + 20.25 + 306.25 + 132.25 + 156.25 + 0.25 + 30.25 = 1068
把平方差相加。
420.25 + 2.25 + 20.25 + 306.25 + 132.25 + 156.25 + 0.25 + 30.25 = 1068
Step 4
Divide by (n − 1) to get the variance.
1068 / (8 − 1) = 1068 / 7 = 152.57
Variance के लिए (n − 1) से भाग दें।
1068 / (8 − 1) = 1068 / 7 = 152.57
除以 (n − 1) 得到方差。
1068 / (8 − 1) = 1068 / 7 = 152.57
Step 5
Take the square root to get the standard deviation.
√152.57 = 12.35
Standard deviation के लिए square root लें।
√152.57 = 12.35
开平方得到标准差。
√152.57 = 12.35
✅
Interpretationमतलब解读

The mean score is 72.5 and the standard deviation is 12.35 marks: a typical student is about 12 marks above or below the mean. The band mean ± 1 SD is 60.2 to 84.9, and 5 of the 8 students (61, 68, 72, 74, 78) are inside it.

Mean score 72.5 है और standard deviation 12.35 marks: एक आम student mean से लगभग 12 marks ऊपर या नीचे है। Mean ± 1 SD की पट्टी 60.2 से 84.9 है, और 8 में से 5 students (61, 68, 72, 74, 78) इसके अंदर हैं।

平均分是 72.5,标准差是 12.35 分:一个典型学生大约比均值高或低 12 分。 均值 ± 1 个标准差的范围是 60.2 到 84.9,8 名学生中有 5 名(61、68、72、74、78)落在其中。


SectionSection第 04 节

Python ImplementationPython ImplementationPython 实现

Manual calculationहाथ से गणना手动计算

scores = [52, 74, 68, 90, 61, 85, 72, 78]

n    = len(scores)
mean = sum(scores) / n

squared_diffs = [(x - mean) ** 2 for x in scores]
variance      = sum(squared_diffs) / (n - 1)
std_dev       = variance ** 0.5

print(f"Mean:     {mean}")
print(f"Variance: {variance:.2f}")
print(f"Std Dev:  {std_dev:.2f}")
OUTPUT
Mean: 72.5 Variance: 152.57 Std Dev: 12.35

Using the statistics modulestatistics module से使用 statistics 模块

import statistics

scores = [52, 74, 68, 90, 61, 85, 72, 78]

print(statistics.mean(scores))                 # mean
print(round(statistics.variance(scores), 2))   # sample variance (n - 1)
print(round(statistics.stdev(scores), 2))      # sample std dev
print(round(statistics.pstdev(scores), 2))     # population std dev (n)
OUTPUT
72.5 152.57 12.35 11.55

Using NumPyNumPy से使用 NumPy

import numpy as np

scores = [52, 74, 68, 90, 61, 85, 72, 78]

print(np.mean(scores))
print(np.std(scores, ddof=1).round(2))   # sample std dev
print(np.std(scores).round(2))           # population std dev — NumPy's default!
print(np.var(scores, ddof=1).round(2))   # sample variance
OUTPUT
72.5 12.35 11.55 152.57
⚠️
NumPy Default: Population SDNumPy का Default: Population SDNumPy 默认:总体标准差

np.std() divides by n unless you write ddof=1: 11.55 instead of 12.35 here. pandas .std() uses ddof=1 by default, and scikit-learn's StandardScaler uses ddof=0. Write ddof explicitly so everyone knows which one you mean.

np.std() n से भाग देता है जब तक आप ddof=1 न लिखें: यहाँ 12.35 की जगह 11.55. pandas .std() default में ddof=1 लेता है, और scikit-learn का StandardScaler ddof=0. ddof साफ़ लिखें ताकि सबको पता हो कि आपका मतलब कौन सा है।

除非写 ddof=1,否则 np.std() 除以 n:这里得到 11.55 而不是 12.35。pandas 的 .std() 默认 ddof=1,scikit-learn 的 StandardScaler 用的是 ddof=0。请显式写出 ddof,让大家都知道你用的是哪一个。

Complete comparison — the two machinesपूरी तुलना — दोनों machines完整对比 —— 两台机器

import numpy as np

machine_a = [19.8, 20.1, 20.0, 19.9, 20.2]
machine_b = [14.0, 26.0, 18.0, 22.0, 20.0]

for name, data in [("Machine A", machine_a), ("Machine B", machine_b)]:
    mean     = np.mean(data)
    variance = np.var(data, ddof=1)
    std_dev  = np.std(data, ddof=1)
    print(f"{name}: mean={mean:.1f} g  variance={variance:.3f} g²  std_dev={std_dev:.2f} g")
OUTPUT
Machine A: mean=20.0 g variance=0.025 g² std_dev=0.16 g Machine B: mean=20.0 g variance=20.000 g² std_dev=4.47 g

SectionSection第 05 节

The 68 – 95 – 99.7 Rule68 – 95 – 99.7 नियम68 – 95 – 99.7 法则

When data follows a normal distribution (bell curve), the standard deviation tells you what share of values falls in any range. This is the Empirical Rule.

जब data normal distribution (bell curve) पर चलता है, तो standard deviation बताता है कि किसी भी range में कितनी values आती हैं। इसे Empirical Rule कहते हैं।

当数据服从正态分布(钟形曲线)时,标准差能告诉你任意区间内有多少比例的数值。这就是经验法则(Empirical Rule)。

68% Rule68% नियम68% 法则
±1σ
  • About 68% of valuesलगभग 68% values约 68% 的数值
  • The "normal" range"आम" range“正常”范围
  • Mean ± 1 SDMean ± 1 SD均值 ± 1 个标准差
95% Rule95% नियम95% 法则
±2σ
  • About 95% of valuesलगभग 95% values约 95% 的数值
  • Covers almost everyoneलगभग सबको शामिल करता है几乎涵盖所有人
  • Mean ± 2 SDMean ± 2 SD均值 ± 2 个标准差
99.7% Rule99.7% नियम99.7% 法则
±3σ
  • About 99.7% of valuesलगभग 99.7% values约 99.7% 的数值
  • Beyond = very unusualइससे बाहर = बहुत असामान्य超出 = 非常罕见
  • Mean ± 3 SDMean ± 3 SD均值 ± 3 个标准差

Real Example — Adult Heightsअसली उदाहरण — वयस्कों की ऊँचाई真实例子 — 成年人身高

Take adult heights that follow a normal distribution with mean = 175 cm and standard deviation = 7 cm (a common textbook approximation for adult men).

वयस्कों की ऊँचाई लें जो mean = 175 cm और standard deviation = 7 cm वाले normal distribution पर चलती है (वयस्क पुरुषों के लिए एक आम textbook अनुमान)।

假设成年人身高服从均值 = 175 cm、标准差 = 7 cm 的正态分布(这是教科书中对成年男性常用的近似)。

RangeRange范围Calculationगणना计算Heightsऊँचाई身高% of men% पुरुष男性占比
±1σ175 ± 7168 – 182 cm68.3%
±2σ175 ± 14161 – 189 cm95.4%
±3σ175 ± 21154 – 196 cm99.7%
Beyond ±3σ±3σ से बाहर超出 ±3σ—<154 cm or >196 cm<154 cm या >196 cm<154 cm 或 >196 cm0.3%
from scipy.stats import norm

mean, std_dev = 175, 7   # cm

for k in (1, 2, 3):
    lo, hi = mean - k * std_dev, mean + k * std_dev
    share = norm.cdf(hi, mean, std_dev) - norm.cdf(lo, mean, std_dev)
    print(f"±{k}σ: {lo} – {hi} cm  →  {share:.2%} of men")

# Any other range works too, e.g. taller than 190 cm:
print(f"Taller than 190 cm: {norm.sf(190, mean, std_dev):.2%}")
OUTPUT
±1σ: 168 – 182 cm → 68.27% of men ±2σ: 161 – 189 cm → 95.45% of men ±3σ: 154 – 196 cm → 99.73% of men Taller than 190 cm: 1.61%
LIVELIVE互动Test the Rule — Normal vs Skewed Dataनियम जाँचें — Normal vs Skewed Data检验法则 — 正态数据 vs 偏斜数据

Pick ±1σ, ±2σ or ±3σ and change the mean and SD. "Draw 1,000 people" checks the rule on random data. Then switch to skewed data with the same mean and SD — the rule breaks.

±1σ, ±2σ या ±3σ चुनें और mean व SD बदलें। "1,000 लोग निकालें" random data पर नियम जाँचता है। फिर उसी mean और SD वाले skewed data पर जाएँ — नियम टूट जाता है।

选择 ±1σ、±2σ 或 ±3σ,并调整均值和标准差。“随机抽 1,000 人”会在随机数据上检验这条法则。然后切换到均值和标准差相同的偏斜数据 —— 法则就失效了。


SectionSection第 06 节

Standard Deviation in Machine LearningMachine Learning में Standard Deviation机器学习中的标准差

Standard deviation is not just a statistics idea — it powers several machine-learning tools you will use every day.

Standard deviation सिर्फ़ statistics का विचार नहीं — यह कई machine-learning औज़ारों को चलाता है जो आप रोज़ इस्तेमाल करेंगे।

标准差不只是统计学概念 —— 它支撑着你每天都会用到的多种机器学习工具。

Z-scores and StandardScalerZ-scores और StandardScalerZ 分数与 StandardScaler

Many algorithms (SVM, KNN, neural networks, PCA) are sensitive to the scale of features. StandardScaler turns each feature into z-scores: mean 0 and standard deviation 1. A z-score says "how many SDs away from the mean" — so values in different units become comparable.

कई algorithms (SVM, KNN, neural networks, PCA) features के scale से प्रभावित होते हैं। StandardScaler हर feature को z-scores में बदलता है: mean 0 और standard deviation 1. Z-score बताता है "mean से कितने SD दूर" — इसलिए अलग units की values की तुलना हो सकती है।

很多算法(SVM、KNN、神经网络、PCA)对特征的尺度很敏感。StandardScaler 把每个特征转换成 z 分数:均值 0、标准差 1。z 分数表示“离均值有几个标准差” —— 于是不同单位的数值也能放在一起比较。

Z-score FormulaZ-score FormulaZ 分数公式
z = (x − μ) / σ
Turns any value into "number of standard deviations from the mean".
किसी भी value को "mean से कितने standard deviations" में बदल देता है।
把任意数值转换成“离均值几个标准差”。
LIVELIVE互动Who Did Better? Compare Two Exams with Z-Scoresकिसने बेहतर किया? दो Exams की Z-Scores से तुलना谁考得更好?用 Z 分数比较两场考试

Maths: class mean 70. English: class mean 80. Set the student's scores and each class's SD. Raw marks cannot be compared directly — z-scores can.

Maths: class mean 70. English: class mean 80. Student के scores और हर class का SD set करें। सीधे marks की तुलना नहीं हो सकती — z-scores की हो सकती है।

数学:班级平均 70 分。英语:班级平均 80 分。设置学生的分数和每个班的标准差。原始分数无法直接比较 —— z 分数可以。

import numpy as np
from sklearn.preprocessing import StandardScaler

# Two features on very different scales: height (cm) and weight (kg)
X = np.array([[160, 55], [175, 70], [180, 80], [155, 50], [190, 95]])

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

print("Means used :", scaler.mean_)
print("SDs used   :", scaler.scale_.round(2))      # population SD (ddof=0)
print("Scaled data:\n", X_scaled.round(2))
print("New means  :", X_scaled.mean(axis=0).round(2) + 0.0)
print("New SDs    :", X_scaled.std(axis=0).round(2))
OUTPUT
Means used : [172. 70.] SDs used : [12.88 16.43] Scaled data: [[-0.93 -0.91] [ 0.23 0. ] [ 0.62 0.61] [-1.32 -1.22] [ 1.4 1.52]] New means : [0. 0.] New SDs : [1. 1.]

Both columns now have mean 0 and SD 1, so height (cm) and weight (kg) count equally in distance-based models. Note that StandardScaler divides by the population SD (12.88 for height, not the sample SD 14.40).

अब दोनों columns का mean 0 और SD 1 है, इसलिए distance-based models में height (cm) और weight (kg) बराबर गिने जाते हैं। ध्यान दें कि StandardScaler population SD से भाग देता है (height के लिए 12.88, sample SD 14.40 नहीं)।

现在两列的均值都是 0、标准差都是 1,所以在基于距离的模型里,身高(cm)和体重(kg)的分量相同。注意 StandardScaler 除的是总体标准差(身高为 12.88,而不是样本标准差 14.40)。

Anomaly Detection — and a TrapAnomaly Detection — और एक जाल异常检测 —— 以及一个陷阱

A common rule: flag any value more than 3 standard deviations from the mean. Let us test it on server response times with one obvious problem (500 ms).

एक आम नियम: mean से 3 standard deviations से ज़्यादा दूर वाली हर value को flag करें। चलिए इसे server response times पर आज़माएँ जिनमें एक साफ़ समस्या है (500 ms)।

一条常见规则:离均值超过 3 个标准差的值都标记为异常。我们在服务器响应时间上试一试,其中有一个明显的问题(500 ms)。

import numpy as np

# Server response times (ms)
response_times = np.array([120, 118, 122, 119, 121, 120, 118, 500, 123, 119])

mean    = response_times.mean()
std_dev = response_times.std(ddof=1)
print(f"Mean:    {mean:.1f} ms")
print(f"Std Dev: {std_dev:.1f} ms")

# Naive rule: anything above mean + 3 std devs is an anomaly
threshold = mean + 3 * std_dev
print(f"Threshold: {threshold:.1f} ms")
print("Anomalies detected:", response_times[response_times > threshold].tolist())
OUTPUT
Mean: 158.0 ms Std Dev: 120.2 ms Threshold: 518.5 ms Anomalies detected: []
🚨
The 3-Sigma Rule Missed the Outage!3-Sigma नियम Outage चूक गया!3-sigma 规则漏掉了这次故障!

The 500 ms value pulls the mean up to 158 ms and inflates the SD to 120 ms, so the threshold becomes 518 ms — and the outlier hides behind its own effect. This is called masking. It is common with small samples: with only 10 points, one value can never be more than about 2.85 SDs from the mean, however extreme it is.

500 ms वाली value mean को 158 ms तक खींच लेती है और SD को 120 ms तक बढ़ा देती है, इसलिए threshold 518 ms हो जाता है — और outlier अपने ही असर के पीछे छिप जाता है। इसे masking कहते हैं। छोटे samples में यह आम है: सिर्फ़ 10 points में कोई भी value mean से लगभग 2.85 SDs से ज़्यादा दूर हो ही नहीं सकती, चाहे वह कितनी भी extreme हो।

500 ms 这个值把均值拉到 158 ms,把标准差撑到 120 ms,于是阈值变成 518 ms —— 离群点被它自己造成的影响掩盖了。这叫作掩蔽效应(masking)。 这在小样本中很常见:只有 10 个点时,无论某个值多极端,它离均值都不可能超过约 2.85 个标准差。

The fix: measure the centre and spread with statistics the outlier cannot move — the median and the MAD (median absolute deviation).

हल: केंद्र और फैलाव ऐसे statistics से मापें जिन्हें outlier हिला न सके — median और MAD (median absolute deviation)।

解决办法:用离群点撼动不了的统计量来衡量中心和离散程度 —— 中位数和 MAD(中位数绝对偏差)。

# Robust fix: use the median and the MAD (median absolute deviation),
# which the outlier cannot inflate.
median = np.median(response_times)
mad    = np.median(np.abs(response_times - median))
robust_sd = 1.4826 * mad            # MAD scaled to match the SD for normal data

robust_z = (response_times - median) / robust_sd
print(f"Median: {median:.1f} ms   robust SD: {robust_sd:.2f} ms")
print("Robust z of 500 ms:", round(robust_z[response_times == 500][0], 1))
print("Anomalies detected:", response_times[np.abs(robust_z) > 3].tolist())
OUTPUT
Median: 120.0 ms robust SD: 2.22 ms Robust z of 500 ms: 170.9 Anomalies detected: [500]
LIVELIVE互动Classic vs Robust Anomaly DetectionClassic vs Robust Anomaly Detection传统 vs 稳健的异常检测

Move the slow response time. Red dashed band = mean ± 3 SD (classic). Green band = median ± 3 robust SD. A red dot is flagged by the robust rule; a thick black ring means the classic rule flags it too.

धीमे response time को खिसकाएँ। लाल dashed पट्टी = mean ± 3 SD (classic)। हरी पट्टी = median ± 3 robust SD. लाल dot को robust नियम पकड़ता है; मोटा काला घेरा मतलब classic नियम भी पकड़ता है।

移动那个慢响应时间。红色虚线带 = 均值 ± 3 个标准差(传统规则);绿色带 = 中位数 ± 3 个稳健标准差。红点表示被稳健规则标记;粗黑圈表示传统规则也标记了它。

💡
Anomaly Detection in the Real Worldअसली दुनिया में Anomaly Detection现实世界中的异常检测

Banks flag unusual transactions, hospitals flag abnormal lab results, and DevOps teams flag slow servers using exactly this idea: "how many SDs away from normal is this?" In practice they compute normal behaviour from a clean history (or with robust statistics), so a single incident cannot hide itself.

Banks असामान्य transactions पकड़ते हैं, hospitals असामान्य lab results, और DevOps teams धीमे servers — बिल्कुल इसी विचार से: "यह आम से कितने SD दूर है?" असल में वे आम व्यवहार साफ़ इतिहास से (या robust statistics से) निकालते हैं, ताकि एक अकेली घटना ख़ुद को छिपा न सके।

银行标记可疑交易、医院标记异常化验结果、运维团队标记慢服务器,用的都是这个思路:“它离正常值有几个标准差?”实际操作中,他们用干净的历史数据(或稳健统计量)来计算“正常”,这样单次事故就无法把自己藏起来。


SectionSection第 07 节

Variance vs Standard Deviation — SummaryVariance vs Standard Deviation — सार方差 vs 标准差 — 总结

Propertyगुण特性VarianceStandard Deviation
SymbolSymbol符号σ² / s²σ / s
FormulaFormula公式Average of squared differencesSquared अंतरों का औसत平方差的平均Square root of varianceVariance का square root方差的平方根
UnitUnit单位Squared (g², min²)Squared (g², min²)平方(g²、分钟²)Same as the data (g, min)Data जैसी (g, min)与数据相同(g、分钟)
Easy to interpret?समझना आसान?容易解释?Harderमुश्किल较难Easyआसान容易
Used inकहाँ इस्तेमाल用于PCA, ANOVA, maths and proofsPCA, ANOVA, गणित और proofsPCA、ANOVA、数学推导Reporting, scaling, z-scores, quality controlReporting, scaling, z-scores, quality control报告、缩放、z 分数、质量控制
Sensitive to outliers?Outliers का असर?对离群点敏感?Yes, veryहाँ, बहुत是,非常Yesहाँ是

SectionSection第 08 节

Golden RulesGolden Rules黄金法则

🎯 Standard Deviation — Key RulesStandard Deviation — मुख्य नियम标准差 — 关键规则
1
Use ddof=1 in NumPy for sample data. The default ddof=0 gives the population SD, which slightly underestimates the spread of a sample.
Sample data के लिए NumPy में ddof=1 इस्तेमाल करें। Default ddof=0 population SD देता है, जो sample के फैलाव को थोड़ा कम आँकता है।
样本数据在 NumPy 中请用 ddof=1。默认的 ddof=0 给出总体标准差,会略微低估样本的离散程度。
2
SD is in the same unit as your data, so it is directly meaningful: an SD of 12 marks means a typical student is about 12 marks from the mean.
SD data की ही unit में होता है, इसलिए सीधे समझ आता है: 12 marks का SD मतलब आम student mean से लगभग 12 marks दूर है।
标准差和数据的单位相同,所以含义直观:标准差 12 分,意味着一个典型学生离均值约 12 分。
3
The 68–95–99.7 rule only works for roughly normal data. Plot a histogram first — skewed or multi-peaked data behave very differently.
68–95–99.7 नियम सिर्फ़ लगभग normal data पर चलता है। पहले histogram बनाएँ — skewed या कई peaks वाला data बहुत अलग व्यवहार करता है।
68–95–99.7 法则只适用于大致正态的数据。先画直方图 —— 偏斜或多峰的数据表现完全不同。
4
For outlier detection, do not let the outliers set the threshold. Use median and MAD (robust z-scores), or compute mean and SD from clean historical data.
Outlier detection में outliers को threshold तय न करने दें। Median और MAD (robust z-scores) इस्तेमाल करें, या mean और SD साफ़ पुराने data से निकालें।
做离群点检测时,别让离群点自己决定阈值。用中位数和 MAD(稳健 z 分数),或用干净的历史数据计算均值和标准差。
5
In ML preprocessing, StandardScaler uses the SD to put features on the same scale. Fit it on the training data only, then apply it to the test data.
ML preprocessing में StandardScaler features को एक scale पर लाने के लिए SD इस्तेमाल करता है। इसे सिर्फ़ training data पर fit करें, फिर test data पर लगाएँ।
在机器学习预处理中,StandardScaler 用标准差把特征放到同一尺度。只在训练数据上 fit,再应用到测试数据。
6
An SD of zero means every value is identical. Such a feature carries no information for a model and can be dropped (scikit-learn: VarianceThreshold).
शून्य SD मतलब हर value एक जैसी है। ऐसा feature model के लिए कोई जानकारी नहीं देता और हटाया जा सकता है (scikit-learn: VarianceThreshold)।
标准差为零表示所有值都相同。这样的特征对模型没有任何信息,可以删掉(scikit-learn:VarianceThreshold)。