The Story: Why "Impurity" Matters
Strategy A: Grab fruit randomly, put it anywhere.
Strategy B: First ask "Is it round?" Then "Is it yellow?" Then "Is it bumpy?"
After Strategy A your boxes are still a mess — apples mixed with oranges, bananas mixed with limes. After Strategy B each box has mostly one type. The boxes are purer.
A Decision Tree is Strategy B. But it needs a number to measure how messy a box is right now, and how much cleaner each possible question would make it. That number is Entropy (or Gini Impurity). The improvement from asking a question is Information Gain.
These three concepts are the entire mathematical engine inside a decision tree. Everything else is just bookkeeping.
Look for the interactive boxes: the Impurity Mixer (Section 02), the Entropy Curve (03), Multi-Class Entropy (04), the Split Lab (05), the Random Label Game (06), Entropy vs Gini Race (08), the Cardinality Trap (09) and the Threshold Finder (11). Every number is computed live with the formulas in this tutorial.
What Is Impurity? — The Core Concept
Before we touch any formula, let's build the intuition. A node in a decision tree contains a group of training samples. Impurity measures how mixed the class labels are in that group.
A Decision Tree split is good if the child nodes are purer than the parent. The algorithm's job is to find the split that reduces impurity the most. Two different formulas measure impurity: Entropy (information theory) and Gini Impurity (probability theory). Both measure the same thing — just with different maths.
Try It — The Impurity Mixer
Fill a node with Yes and No samples. Watch how mixed it is — and what the two impurity numbers say — before you learn a single formula.
कहानी: "Impurity" क्यों ज़रूरी है
तरीका A: कोई भी फल उठाओ, कहीं भी रख दो।
तरीका B: पहले पूछो "क्या यह गोल है?" फिर "क्या यह पीला है?" फिर "क्या यह खुरदुरा है?"
तरीका A के बाद आपके डिब्बे अब भी गड़बड़ हैं — सेब संतरों में मिले हैं, केले नींबू में मिले हैं। तरीका B के बाद हर डिब्बे में ज़्यादातर एक ही किस्म है। डिब्बे ज़्यादा pure हैं।
Decision Tree तरीका B है। पर उसे एक संख्या चाहिए, जो बताए कि अभी डिब्बा कितना गड़बड़ है, और हर मुमकिन सवाल उसे कितना साफ़ करेगा। वह संख्या है Entropy (या Gini Impurity)। सवाल पूछने से जो सुधार होता है, वह है Information Gain।
ये तीन concept ही decision tree के अंदर का पूरा गणित वाला इंजन हैं। बाकी सब बस हिसाब-किताब है।
Interactive boxes ढूँढिए: Impurity Mixer (सेक्शन 02), Entropy Curve (03), Multi-Class Entropy (04), Split Lab (05), Random Label Game (06), Entropy vs Gini Race (08), Cardinality Trap (09) और Threshold Finder (11)। हर संख्या इसी tutorial के formulas से live निकाली जाती है।
Impurity क्या है? — मूल concept
कोई भी formula छूने से पहले, चलिए समझ बनाते हैं। Decision tree का एक node training samples का एक समूह रखता है। Impurity नापती है कि उस समूह में class labels कितने मिले-जुले हैं।
Decision Tree का split तब अच्छा है जब child nodes parent से ज़्यादा pure हों। Algorithm का काम है वह split ढूँढना जो impurity को सबसे ज़्यादा घटाए। Impurity नापने के दो अलग formulas हैं: Entropy (information theory) और Gini Impurity (probability theory)। दोनों एक ही चीज़ नापते हैं — बस गणित अलग है।
ख़ुद आज़माइए — Impurity Mixer
एक node को Yes और No samples से भरिए। देखिए वह कितना मिला-जुला है — और दोनों impurity संख्याएँ क्या कहती हैं — एक भी formula सीखने से पहले।
故事:为什么 “impurity” 很重要
方法 A:随手拿起水果,随便放。
方法 B:先问 “它是圆的吗?” 再问 “它是黄色的吗?” 然后问 “它表面凹凸不平吗?”
用方法 A 之后,箱子还是一团乱:苹果和橙子混在一起, 香蕉和青柠混在一起。用方法 B 之后,每个箱子里基本只有一种水果。 箱子变得更 pure 了。
Decision Tree 就是方法 B。但它需要一个 数字, 来衡量一个箱子现在有多乱,以及每个可能的问题能让它变得多干净。 这个数字就是 Entropy(或 Gini Impurity)。提一个问题带来的改进, 就是 Information Gain。
这三个概念就是 decision tree 里面全部的数学引擎。 其他的都只是记账而已。
请留意这些互动框:Impurity Mixer(第 02 节)、Entropy Curve(03)、 Multi-Class Entropy(04)、Split Lab(05)、Random Label Game(06)、 Entropy vs Gini Race(08)、Cardinality Trap(09)和 Threshold Finder(11)。 每个数字都用本教程里的公式实时计算。
什么是 impurity?—— 核心概念
在接触任何公式之前,我们先建立直觉。decision tree 里的一个 node 包含一组 training samples。Impurity 衡量的是这组 samples 里 class labels 混得有多厉害。
如果 child nodes 比 parent 更 pure, 这个 Decision Tree split 就是 好 的。算法的任务是找到 最能降低 impurity 的 split。有两个不同的公式可以衡量 impurity: Entropy(信息论)和 Gini Impurity (概率论)。两者衡量的是同一件事,只是数学方法不同。
动手试试 —— Impurity Mixer
往一个 node 里放入 Yes 和 No samples。在学任何公式之前,先看看它混得有多厉害, 以及两个 impurity 数字分别说了什么。
Entropy — Disorder Measured in Bits
If the text is all A's — you need 0 questions. You already know the answer. Zero surprise. Zero entropy.
If every letter is equally likely — you need about 4.7 questions (log₂ 26 ≈ 4.7). Maximum surprise. Maximum entropy.
A node in a decision tree is like a bag of letters. If all samples belong to one class — zero surprise, entropy = 0. If classes are perfectly balanced — maximum surprise, entropy = 1.0 (for binary). The tree wants to ask questions that reduce surprise as fast as possible.
Sum over all classes. Convention: 0 · log₂(0) = 0.
Unit: bits. Range: 0 (pure) to log₂(n) for n classes.
Maximum = 1.0 bit when p = 0.5 (50/50 split).
Minimum = 0 bits when p = 0 or p = 1 (pure node).
The Entropy Curve — Visualised
The entropy curve is a symmetric hill. It peaks at 1.0 bit when classes are 50/50 (maximum uncertainty) and falls to 0 at both extremes (complete certainty). The tree wants to move nodes leftward or rightward — away from the peak.
Step-by-Step Entropy Calculation — Weather Dataset
We have 14 days of weather data. The target is Play Tennis? The full set S contains 9 Yes and 5 No.
p(Yes) = 9/14 = 0.643 | p(No) = 5/14 = 0.357
−(0.643 · log₂ 0.643) = −(0.643 · −0.637) = +0.410
−(0.357 · log₂ 0.357) = −(0.357 · −1.486) = +0.530
This is the baseline. Every split's quality is measured against this.
Try It — The Entropy Curve
Set the number of Yes and No samples. The dot moves along the entropy hill, and the box works out H step by step — exactly like the calculation above. Try [9, 5] first.
Entropy — Bits में नापी गई गड़बड़ी
अगर text में सब A ही हैं — तो आपको 0 सवाल चाहिए। आपको जवाब पहले से पता है। Zero सरप्राइज़। Zero entropy।
अगर हर अक्षर के आने का मौका बराबर है — तो आपको लगभग 4.7 सवाल चाहिए (log₂ 26 ≈ 4.7)। सबसे ज़्यादा सरप्राइज़। सबसे ज़्यादा entropy।
Decision tree का एक node अक्षरों के एक थैले जैसा है। अगर सारे samples एक ही class के हैं — zero सरप्राइज़, entropy = 0। अगर classes पूरी तरह बराबर बँटी हैं — सबसे ज़्यादा सरप्राइज़, entropy = 1.0 (binary के लिए)। Tree चाहता है ऐसे सवाल पूछना जो सरप्राइज़ को जितनी जल्दी हो सके घटाएँ।
सभी classes पर जोड़िए। नियम: 0 · log₂(0) = 0।
इकाई: bits। दायरा: 0 (pure) से n classes के लिए log₂(n) तक।
अधिकतम = 1.0 bit जब p = 0.5 (50/50 split)।
न्यूनतम = 0 bits जब p = 0 या p = 1 (pure node)।
Entropy Curve — चित्र में
Entropy curve एक बराबर (symmetric) पहाड़ी जैसा है। जब classes 50/50 हों (सबसे ज़्यादा अनिश्चितता), तो यह 1.0 bit पर सबसे ऊपर होता है, और दोनों छोरों पर 0 तक गिर जाता है (पूरी निश्चितता)। Tree चाहता है कि nodes बाएँ या दाएँ जाएँ — चोटी से दूर।
Entropy का Step-by-Step हिसाब — Weather Dataset
हमारे पास 14 दिनों का मौसम data है। Target है Play Tennis? पूरे set S में 9 Yes और 5 No हैं।
p(Yes) = 9/14 = 0.643 | p(No) = 5/14 = 0.357
−(0.643 · log₂ 0.643) = −(0.643 · −0.637) = +0.410
−(0.357 · log₂ 0.357) = −(0.357 · −1.486) = +0.530
यह baseline है। हर split की quality इसी से तुलना करके नापी जाती है।
ख़ुद आज़माइए — Entropy Curve
Yes और No samples की गिनती सेट कीजिए। बिंदु entropy की पहाड़ी पर चलता है, और box H को step-by-step निकालता है — बिल्कुल ऊपर वाले हिसाब की तरह। पहले [9, 5] आज़माइए।
Entropy —— 用 bits 来衡量混乱程度
如果文字全是 A,你需要 0 个问题。 你早就知道答案了。零意外。零 entropy。
如果每个字母出现的可能性都一样,你大约需要 4.7 个问题(log₂ 26 ≈ 4.7)。最大的意外。 最大的 entropy。
decision tree 里的一个 node 就像一袋字母。如果所有 samples 都属于同一个 class,就是零意外,entropy = 0。如果各个 class 完全 平衡,就是最大的意外,entropy = 1.0(二分类时)。tree 希望 提出能尽快降低意外的问题。
对所有 class 求和。约定:0 · log₂(0) = 0。
单位:bits。范围:0(pure)到 log₂(n),n 是 class 的个数。
当 p = 0.5(50/50 split)时,最大值 = 1.0 bit。
当 p = 0 或 p = 1(pure node)时,最小值 = 0 bits。
Entropy 曲线 —— 画出来看看
Entropy 曲线是一座对称的小山。当 class 是 50/50 时,它在 1.0 bit 达到顶点(最大的不确定);在两端都降到 0(完全确定)。tree 希望把 node 往左或往右推,远离山顶。
一步一步计算 Entropy —— 天气 dataset
我们有 14 天的天气数据。目标是 Play Tennis? 整个集合 S 包含 9 个 Yes 和 5 个 No。
p(Yes) = 9/14 = 0.643 | p(No) = 5/14 = 0.357
−(0.643 · log₂ 0.643) = −(0.643 · −0.637) = +0.410
−(0.357 · log₂ 0.357) = −(0.357 · −1.486) = +0.530
这就是基准线。每个 split 的好坏都拿它来比较。
动手试试 —— Entropy Curve
设置 Yes 和 No samples 的数量。圆点会沿着 entropy 小山移动,下面的框会一步一步算出 H, 和上面的计算完全一样。先试试 [9, 5]。
Entropy for Multiple Classes
Entropy works for any number of classes — not just binary. The formula is identical; you just sum more terms. The maximum possible entropy scales with the number of classes.
| Number of Classes (n) | Maximum Entropy = log₂(n) | Example Problem | Max Entropy Value |
|---|---|---|---|
| 2 (Binary) | log₂(2) | Play Tennis: Yes / No | 1.000 bits |
| 3 | log₂(3) | Weather: Sunny / Overcast / Rain | 1.585 bits |
| 4 | log₂(4) | Seasons: Spring / Summer / Autumn / Winter | 2.000 bits |
| 5 | log₂(5) | Risk: Very Low / Low / Med / High / Very High | 2.322 bits |
| 10 | log₂(10) | Digit recognition: 0 – 9 | 3.322 bits |
p(Apple) = 6/12 = 0.50 | p(Orange) = 3/12 = 0.25 | p(Banana) = 3/12 = 0.25
= −(0.50 · −1.0) − (0.25 · −2.0) − (0.25 · −2.0)
= 0.500 + 0.500 + 0.500 = 1.500 bits
Our box is close to maximum impurity (1.5/1.585 = 94.6% of maximum). A good split should bring this down significantly.
1. Always ≥ 0 — entropy is zero only when the node is perfectly pure (one class only).
2. Maximum when classes are equally likely — this is when you have the least information.
3. Additive for independent events — this is what makes it theoretically elegant and why Shannon chose it over alternatives.
Try It — Multi-Class Entropy
A fruit box with up to five kinds of fruit. Each bar shows one term of the sum. Start with the 6 / 3 / 3 example, then make all counts equal and see the entropy reach log₂(n).
कई Classes के लिए Entropy
Entropy कितनी भी classes के लिए काम करती है — सिर्फ़ binary के लिए नहीं। Formula वही है; बस आप ज़्यादा terms जोड़ते हैं। सबसे बड़ी मुमकिन entropy classes की गिनती के साथ बढ़ती है।
| Classes की गिनती (n) | अधिकतम Entropy = log₂(n) | उदाहरण समस्या | अधिकतम Entropy मान |
|---|---|---|---|
| 2 (Binary) | log₂(2) | Play Tennis: Yes / No | 1.000 bits |
| 3 | log₂(3) | मौसम: Sunny / Overcast / Rain | 1.585 bits |
| 4 | log₂(4) | ऋतुएँ: वसंत / गर्मी / पतझड़ / सर्दी | 2.000 bits |
| 5 | log₂(5) | जोखिम: बहुत कम / कम / मध्यम / ज़्यादा / बहुत ज़्यादा | 2.322 bits |
| 10 | log₂(10) | अंक पहचानना: 0 – 9 | 3.322 bits |
p(Apple) = 6/12 = 0.50 | p(Orange) = 3/12 = 0.25 | p(Banana) = 3/12 = 0.25
= −(0.50 · −1.0) − (0.25 · −2.0) − (0.25 · −2.0)
= 0.500 + 0.500 + 0.500 = 1.500 bits
हमारा डिब्बा अधिकतम impurity के क़रीब है (1.5/1.585 = अधिकतम का 94.6%)। एक अच्छा split इसे काफ़ी नीचे लाना चाहिए।
1. हमेशा ≥ 0 — entropy तभी zero होती है जब node पूरी तरह pure हो (सिर्फ़ एक class)।
2. सबसे ज़्यादा तब, जब सभी classes का मौका बराबर हो — यही वह हालत है जब आपके पास सबसे कम जानकारी होती है।
3. अलग-अलग (independent) घटनाओं के लिए जुड़ती है — इसी वजह से यह theory में सुंदर है, और इसीलिए शैनन ने दूसरे विकल्पों की जगह इसे चुना।
ख़ुद आज़माइए — Multi-Class Entropy
एक फलों का डिब्बा, जिसमें पाँच तरह तक के फल हैं। हर bar जोड़ का एक term दिखाता है। 6 / 3 / 3 वाले उदाहरण से शुरू कीजिए, फिर सारी गिनतियाँ बराबर कीजिए और देखिए entropy log₂(n) तक पहुँचती है।
多个 class 的 Entropy
Entropy 适用于任意数量的 class,不只是二分类。公式完全一样, 只是要多加几项。最大可能的 entropy 会随着 class 的数量变大。
| class 数量(n) | 最大 Entropy = log₂(n) | 示例问题 | 最大 Entropy 值 |
|---|---|---|---|
| 2(二分类) | log₂(2) | Play Tennis:Yes / No | 1.000 bits |
| 3 | log₂(3) | 天气:Sunny / Overcast / Rain | 1.585 bits |
| 4 | log₂(4) | 季节:春 / 夏 / 秋 / 冬 | 2.000 bits |
| 5 | log₂(5) | 风险:很低 / 低 / 中 / 高 / 很高 | 2.322 bits |
| 10 | log₂(10) | 数字识别:0 – 9 | 3.322 bits |
p(Apple) = 6/12 = 0.50 | p(Orange) = 3/12 = 0.25 | p(Banana) = 3/12 = 0.25
= −(0.50 · −1.0) − (0.25 · −2.0) − (0.25 · −2.0)
= 0.500 + 0.500 + 0.500 = 1.500 bits
我们的箱子很接近最大 impurity(1.5/1.585 = 最大值的 94.6%)。 一个好的 split 应该能把它大幅降下来。
1. 永远 ≥ 0 —— 只有当 node 完全 pure(只有一个 class)时,entropy 才是零。
2. 各 class 可能性相同时最大 —— 这时你掌握的信息最少。
3. 对独立事件可以相加 —— 这让它在理论上很优美,也是 Shannon 选它而不选其他方案的原因。
动手试试 —— Multi-Class Entropy
一个水果箱,最多可以有五种水果。每根柱子表示求和中的一项。先从 6 / 3 / 3 的例子开始,然后 把所有数量设成一样,看看 entropy 如何达到 log₂(n)。
Information Gain — Measuring Split Quality
Information Gain answers the question: "How much does asking this question reduce my uncertainty about the answer?" It is simply the difference in entropy before and after the split, weighted by the size of each child group.
Sᵥ = subset of samples where attribute A has value v.
The weighted entropy of all children is subtracted from parent entropy.
Worked Example: Which Attribute Splits the Weather Data Best?
| Day | Outlook | Temp | Humidity | Wind | Play Tennis? |
|---|---|---|---|---|---|
| D1 | Sunny | Hot | High | Weak | No |
| D2 | Sunny | Hot | High | Strong | No |
| D3 | Overcast | Hot | High | Weak | Yes |
| D4 | Rain | Mild | High | Weak | Yes |
| D5 | Rain | Cool | Normal | Weak | Yes |
| D6 | Rain | Cool | Normal | Strong | No |
| D7 | Overcast | Cool | Normal | Strong | Yes |
| D8 | Sunny | Mild | High | Weak | No |
| D9 | Sunny | Cool | Normal | Weak | Yes |
| D10 | Rain | Mild | Normal | Weak | Yes |
| D11 | Sunny | Mild | Normal | Strong | Yes |
| D12 | Overcast | Mild | High | Strong | Yes |
| D13 | Overcast | Hot | Normal | Weak | Yes |
| D14 | Rain | Mild | High | Strong | No |
Parent entropy H(S) = 0.940 bits. Now we calculate IG for each attribute.
H(Sunny) = −0.4·log₂0.4 − 0.6·log₂0.6 = 0.529 + 0.442 = 0.971 bits
H(Overcast) = 0 − 0 = 0.000 bits
H(Rain) = −0.6·log₂0.6 − 0.4·log₂0.4 = 0.442 + 0.529 = 0.971 bits
= 0.347 + 0.000 + 0.347 = 0.694 bits
H(High) = −(3/7)·log₂(3/7) − (4/7)·log₂(4/7) = 0.524 + 0.461 = 0.985 bits
H(Normal) = −(6/7)·log₂(6/7) − (1/7)·log₂(1/7) = 0.191 + 0.401 = 0.592 bits
IG(S, Humidity) = 0.940 − 0.789 = 0.151 bits
Weighted = (6/14)·1.0 + (8/14)·0.811 = 0.893
IG(S, Wind) = 0.940 − 0.893 = 0.048 bits
Weighted = (4/14)·1.0 + (6/14)·0.918 + (4/14)·0.811 = 0.911
IG(S, Temp) = 0.940 − 0.911 = 0.029 bits
Outlook is selected as the root node with IG = 0.246 bits. Temp earns only 0.029 bits — it is outcompeted at every node and ends up never being used in the final tree.
Try It — The Split Lab
Pick an attribute to split the 14 days. See the child nodes, their impurity, the weighted average and the gain. The bars on the right rank all four attributes. Switch the measure to Gini and compare.
Information Gain — Split की Quality नापना
Information Gain इस सवाल का जवाब देता है: "यह सवाल पूछने से जवाब के बारे में मेरी अनिश्चितता कितनी घटती है?" यह बस split से पहले और बाद की entropy का अंतर है, जिसमें हर child समूह के साइज़ के हिसाब से वज़न दिया जाता है।
Sᵥ = samples का वह हिस्सा जहाँ attribute A का मान v है।
सभी children की weighted entropy को parent entropy से घटाया जाता है।
हल किया हुआ उदाहरण: कौन-सा Attribute Weather Data को सबसे अच्छा Split करता है?
| दिन | Outlook | Temp | Humidity | Wind | Play Tennis? |
|---|---|---|---|---|---|
| D1 | Sunny | Hot | High | Weak | No |
| D2 | Sunny | Hot | High | Strong | No |
| D3 | Overcast | Hot | High | Weak | Yes |
| D4 | Rain | Mild | High | Weak | Yes |
| D5 | Rain | Cool | Normal | Weak | Yes |
| D6 | Rain | Cool | Normal | Strong | No |
| D7 | Overcast | Cool | Normal | Strong | Yes |
| D8 | Sunny | Mild | High | Weak | No |
| D9 | Sunny | Cool | Normal | Weak | Yes |
| D10 | Rain | Mild | Normal | Weak | Yes |
| D11 | Sunny | Mild | Normal | Strong | Yes |
| D12 | Overcast | Mild | High | Strong | Yes |
| D13 | Overcast | Hot | Normal | Weak | Yes |
| D14 | Rain | Mild | High | Strong | No |
Parent entropy H(S) = 0.940 bits। अब हम हर attribute के लिए IG निकालते हैं।
H(Sunny) = −0.4·log₂0.4 − 0.6·log₂0.6 = 0.529 + 0.442 = 0.971 bits
H(Overcast) = 0 − 0 = 0.000 bits
H(Rain) = −0.6·log₂0.6 − 0.4·log₂0.4 = 0.442 + 0.529 = 0.971 bits
= 0.347 + 0.000 + 0.347 = 0.694 bits
H(High) = −(3/7)·log₂(3/7) − (4/7)·log₂(4/7) = 0.524 + 0.461 = 0.985 bits
H(Normal) = −(6/7)·log₂(6/7) − (1/7)·log₂(1/7) = 0.191 + 0.401 = 0.592 bits
IG(S, Humidity) = 0.940 − 0.789 = 0.151 bits
Weighted = (6/14)·1.0 + (8/14)·0.811 = 0.893
IG(S, Wind) = 0.940 − 0.893 = 0.048 bits
Weighted = (4/14)·1.0 + (6/14)·0.918 + (4/14)·0.811 = 0.911
IG(S, Temp) = 0.940 − 0.911 = 0.029 bits
Outlook को root node चुना जाता है, IG = 0.246 bits के साथ। Temp को सिर्फ़ 0.029 bits मिलते हैं — यह हर node पर दूसरों से हार जाता है और final tree में कभी इस्तेमाल ही नहीं होता।
ख़ुद आज़माइए — Split Lab
14 दिनों को split करने के लिए एक attribute चुनिए। Child nodes, उनकी impurity, weighted average और gain देखिए। दाईं ओर के bars चारों attributes की रैंकिंग दिखाते हैं। Measure को Gini पर बदलिए और तुलना कीजिए।
Information Gain —— 衡量 split 的好坏
Information Gain 回答的问题是:“问这个问题, 能让我对答案的不确定减少多少?”它就是 split 前后 entropy 的差值,并按每个 child 组的大小加权。
Sᵥ = attribute A 取值为 v 的那部分 samples。
用 parent entropy 减去所有 child 的加权 entropy。
例题:哪个 attribute 最适合 split 天气数据?
| 日期 | Outlook | Temp | Humidity | Wind | Play Tennis? |
|---|---|---|---|---|---|
| D1 | Sunny | Hot | High | Weak | No |
| D2 | Sunny | Hot | High | Strong | No |
| D3 | Overcast | Hot | High | Weak | Yes |
| D4 | Rain | Mild | High | Weak | Yes |
| D5 | Rain | Cool | Normal | Weak | Yes |
| D6 | Rain | Cool | Normal | Strong | No |
| D7 | Overcast | Cool | Normal | Strong | Yes |
| D8 | Sunny | Mild | High | Weak | No |
| D9 | Sunny | Cool | Normal | Weak | Yes |
| D10 | Rain | Mild | Normal | Weak | Yes |
| D11 | Sunny | Mild | Normal | Strong | Yes |
| D12 | Overcast | Mild | High | Strong | Yes |
| D13 | Overcast | Hot | Normal | Weak | Yes |
| D14 | Rain | Mild | High | Strong | No |
Parent entropy H(S) = 0.940 bits。现在我们为每个 attribute 计算 IG。
H(Sunny) = −0.4·log₂0.4 − 0.6·log₂0.6 = 0.529 + 0.442 = 0.971 bits
H(Overcast) = 0 − 0 = 0.000 bits
H(Rain) = −0.6·log₂0.6 − 0.4·log₂0.4 = 0.442 + 0.529 = 0.971 bits
= 0.347 + 0.000 + 0.347 = 0.694 bits
H(High) = −(3/7)·log₂(3/7) − (4/7)·log₂(4/7) = 0.524 + 0.461 = 0.985 bits
H(Normal) = −(6/7)·log₂(6/7) − (1/7)·log₂(1/7) = 0.191 + 0.401 = 0.592 bits
IG(S, Humidity) = 0.940 − 0.789 = 0.151 bits
加权 = (6/14)·1.0 + (8/14)·0.811 = 0.893
IG(S, Wind) = 0.940 − 0.893 = 0.048 bits
加权 = (4/14)·1.0 + (6/14)·0.918 + (4/14)·0.811 = 0.911
IG(S, Temp) = 0.940 − 0.911 = 0.029 bits
Outlook 以 IG = 0.246 bits 被选为 root node。Temp 只得到 0.029 bits —— 它在每个 node 都比不过别人,最后在最终的 tree 里从来没被用到。
动手试试 —— Split Lab
选一个 attribute 来 split 这 14 天。看看 child nodes、它们的 impurity、weighted average 和 gain。 右边的柱子给全部四个 attribute 排名。把衡量方式切换成 Gini,再比较一下。
Gini Impurity — The CART Algorithm's Measure
If the node is pure (all one class), I'll never mislabel it — Gini = 0.
If the node is 50/50, I'll mislabel half the time — Gini = 0.5.
No logarithms. No bits. Just a probability between 0 and 0.5 (for binary problems). This is why scikit-learn defaults to Gini — it's computationally faster while capturing the same information.
Sum the squared proportions, subtract from 1.
Range: 0 (pure) to (n−1)/n for n classes. Binary max = 0.5.
Gini Gain = Gini(parent) − Gini_split.
Pick the split with the highest Gini Gain.
Gini Values Across Class Distributions
| Distribution [Yes, No] | p(Yes) | Gini = 1 − (p² + q²) | Interpretation |
|---|---|---|---|
| [10, 0] — all Yes | 1.00 | 1 − (1.00² + 0.00²) = 0.000 | Pure — no impurity |
| [8, 2] — mostly Yes | 0.80 | 1 − (0.80² + 0.20²) = 1 − 0.68 = 0.320 | Low impurity |
| [6, 4] — slightly skewed | 0.60 | 1 − (0.60² + 0.40²) = 1 − 0.52 = 0.480 | Moderate impurity |
| [5, 5] — perfectly mixed | 0.50 | 1 − (0.50² + 0.50²) = 1 − 0.50 = 0.500 | Maximum impurity (binary) |
Gini Calculation — Same Weather Dataset
Gain = 0.460 − 0.367 = 0.093
Try It — The Random Label Game
Play the Gini game for real. Each round picks a random sample from the node and gives it a random label, drawn from the node's own class mix. Count how often the label is wrong. After many rounds, that rate is the Gini impurity.
Gini Impurity — CART Algorithm का पैमाना
अगर node pure है (सब एक ही class), तो मैं कभी ग़लत label नहीं दूँगा — Gini = 0।
अगर node 50/50 है, तो मैं आधी बार ग़लत label दूँगा — Gini = 0.5।
कोई logarithm नहीं। कोई bits नहीं। बस 0 और 0.5 के बीच एक probability (binary समस्याओं के लिए)। इसीलिए scikit-learn default में Gini लेता है — यह हिसाब में तेज़ है और वही जानकारी पकड़ता है।
हिस्सों के square जोड़िए, फिर 1 में से घटाइए।
दायरा: 0 (pure) से n classes के लिए (n−1)/n तक। Binary में अधिकतम = 0.5।
Gini Gain = Gini(parent) − Gini_split।
वह split चुनिए जिसका Gini Gain सबसे ज़्यादा हो।
अलग-अलग Class बँटवारों पर Gini के मान
| बँटवारा [Yes, No] | p(Yes) | Gini = 1 − (p² + q²) | मतलब |
|---|---|---|---|
| [10, 0] — सब Yes | 1.00 | 1 − (1.00² + 0.00²) = 0.000 | Pure — कोई impurity नहीं |
| [8, 2] — ज़्यादातर Yes | 0.80 | 1 − (0.80² + 0.20²) = 1 − 0.68 = 0.320 | कम impurity |
| [6, 4] — थोड़ा एकतरफ़ा | 0.60 | 1 − (0.60² + 0.40²) = 1 − 0.52 = 0.480 | मध्यम impurity |
| [5, 5] — पूरी तरह मिला-जुला | 0.50 | 1 − (0.50² + 0.50²) = 1 − 0.50 = 0.500 | अधिकतम impurity (binary) |
Gini का हिसाब — वही Weather Dataset
Gain = 0.460 − 0.367 = 0.093
ख़ुद आज़माइए — Random Label Game
Gini वाला खेल सच में खेलिए। हर round में node से एक random sample चुना जाता है और उसे एक random label दिया जाता है, जो node के अपने class मिश्रण से निकाला जाता है। गिनिए कि label कितनी बार ग़लत होता है। बहुत सारे rounds के बाद, वही दर Gini impurity है।
Gini Impurity —— CART 算法的衡量方式
如果 node 是 pure 的(全是一个 class),我永远不会贴错 —— Gini = 0。
如果 node 是 50/50,我有一半的时候会贴错 —— Gini = 0.5。
没有 log。没有 bits。只是一个 0 到 0.5 之间的 probability(二分类 问题)。这就是 scikit-learn 默认用 Gini 的原因 —— 它计算 更快,同时能抓住同样的信息。
把比例的平方加起来,再用 1 减去。
范围:0(pure)到 (n−1)/n,n 是 class 的个数。二分类最大值 = 0.5。
Gini Gain = Gini(parent) − Gini_split。
选 Gini Gain 最高 的 split。
不同 class 分布下的 Gini 值
| 分布 [Yes, No] | p(Yes) | Gini = 1 − (p² + q²) | 解读 |
|---|---|---|---|
| [10, 0] —— 全是 Yes | 1.00 | 1 − (1.00² + 0.00²) = 0.000 | Pure —— 没有 impurity |
| [8, 2] —— 大多是 Yes | 0.80 | 1 − (0.80² + 0.20²) = 1 − 0.68 = 0.320 | impurity 低 |
| [6, 4] —— 稍微偏斜 | 0.60 | 1 − (0.60² + 0.40²) = 1 − 0.52 = 0.480 | impurity 中等 |
| [5, 5] —— 完全混合 | 0.50 | 1 − (0.50² + 0.50²) = 1 − 0.50 = 0.500 | 最大 impurity(二分类) |
Gini 计算 —— 同一个天气 dataset
Gain = 0.460 − 0.367 = 0.093
动手试试 —— Random Label Game
真的来玩一次 Gini 游戏。每一轮从 node 里随机挑一个 sample,再随机给它一个 label,这个 label 按照 node 自己的 class 比例来抽。数一数 label 错了多少次。玩了很多轮之后,这个错误率就是 Gini impurity。
Gini for Multiple Classes — Maximum Values
Just like entropy, the maximum Gini Impurity increases with the number of classes. The formula for maximum Gini is (n−1)/n — occurring when all classes are equally likely.
| Classes (n) | Max Gini = (n−1)/n | Worked Example (equal probs) |
|---|---|---|
| 2 | 0.500 | p=0.5 each: 1−(0.5²+0.5²) = 1−0.5 = 0.500 |
| 3 | 0.667 | p=1/3 each: 1−3·(1/3)² = 1−3·(1/9) = 1−1/3 = 0.667 |
| 4 | 0.750 | p=0.25 each: 1−4·(0.25²) = 1−4·0.0625 = 1−0.25 = 0.750 |
| 5 | 0.800 | p=0.2 each: 1−5·(0.2²) = 1−5·0.04 = 1−0.20 = 0.800 |
| 10 | 0.900 | p=0.1 each: 1−10·(0.1²) = 1−10·0.01 = 1−0.10 = 0.900 |
Entropy vs Gini — Head-to-Head
All three curves are symmetric and peak at p = 0.5. Entropy (purple) rises more steeply away from the extremes — it is more sensitive to small probability changes near p=0 and p=1. Gini (blue) is a smoother quadratic approximation. Misclassification rate (dashed) is linear and less smooth — rarely used because it lacks the mathematical properties the other two have.
| Property | Detail |
|---|---|
| Formula | 1 − Σ pᵢ² |
| Binary range | 0 to 0.5 |
| n-class max | (n−1)/n |
| Computation | Fast — squaring only, no log |
| Behaviour | Tends toward larger balanced partitions |
| Sensitivity | Less sensitive at extremes (near-pure nodes) |
| Used by | CART, sklearn DecisionTree (default) |
| Property | Detail |
|---|---|
| Formula | −Σ pᵢ · log₂(pᵢ) |
| Binary range | 0 to 1.0 |
| n-class max | log₂(n) |
| Computation | Slower — log per class per node |
| Behaviour | More sensitive to subtle class changes |
| Sensitivity | Higher sensitivity at near-pure nodes |
| Used by | ID3, C4.5, sklearn with criterion='entropy' |
Try It — Entropy vs Gini Race
Left: slide p to compare the three curves at any point (Gini and misclassification are doubled so all three top out at 1). Right: a race. Hundreds of random nodes each get 8 random candidate splits. Do entropy and Gini pick the same winner?
कई Classes के लिए Gini — अधिकतम मान
Entropy की तरह, अधिकतम Gini Impurity भी classes की गिनती के साथ बढ़ती है। अधिकतम Gini का formula है (n−1)/n — यह तब होता है जब सभी classes का मौका बराबर हो।
| Classes (n) | अधिकतम Gini = (n−1)/n | हल किया उदाहरण (बराबर probabilities) |
|---|---|---|
| 2 | 0.500 | हर एक p=0.5: 1−(0.5²+0.5²) = 1−0.5 = 0.500 |
| 3 | 0.667 | हर एक p=1/3: 1−3·(1/3)² = 1−3·(1/9) = 1−1/3 = 0.667 |
| 4 | 0.750 | हर एक p=0.25: 1−4·(0.25²) = 1−4·0.0625 = 1−0.25 = 0.750 |
| 5 | 0.800 | हर एक p=0.2: 1−5·(0.2²) = 1−5·0.04 = 1−0.20 = 0.800 |
| 10 | 0.900 | हर एक p=0.1: 1−10·(0.1²) = 1−10·0.01 = 1−0.10 = 0.900 |
Entropy vs Gini — आमने-सामने
तीनों curves बराबर (symmetric) हैं और p = 0.5 पर सबसे ऊपर होते हैं। Entropy (बैंगनी) छोरों से दूर ज़्यादा तेज़ी से चढ़ती है — p=0 और p=1 के पास probability के छोटे बदलावों पर यह ज़्यादा संवेदनशील है। Gini (नीला) एक ज़्यादा smooth quadratic अंदाज़ा है। Misclassification rate (dashed) सीधी रेखा वाला और कम smooth है — इसका इस्तेमाल कम होता है, क्योंकि इसमें वे गणितीय ख़ूबियाँ नहीं हैं जो बाकी दोनों में हैं।
| ख़ूबी | विवरण |
|---|---|
| Formula | 1 − Σ pᵢ² |
| Binary दायरा | 0 से 0.5 |
| n-class अधिकतम | (n−1)/n |
| हिसाब | तेज़ — सिर्फ़ square, कोई log नहीं |
| बर्ताव | बड़े, बराबर हिस्सों की ओर झुकती है |
| संवेदनशीलता | छोरों पर (लगभग pure nodes पर) कम संवेदनशील |
| कौन इस्तेमाल करता है | CART, sklearn DecisionTree (default) |
| ख़ूबी | विवरण |
|---|---|
| Formula | −Σ pᵢ · log₂(pᵢ) |
| Binary दायरा | 0 से 1.0 |
| n-class अधिकतम | log₂(n) |
| हिसाब | धीमा — हर node पर हर class के लिए log |
| बर्ताव | class के बारीक बदलावों पर ज़्यादा संवेदनशील |
| संवेदनशीलता | लगभग pure nodes पर ज़्यादा संवेदनशील |
| कौन इस्तेमाल करता है | ID3, C4.5, sklearn with criterion='entropy' |
ख़ुद आज़माइए — Entropy vs Gini Race
बाएँ: किसी भी बिंदु पर तीनों curves की तुलना के लिए p को खिसकाइए (Gini और misclassification को दोगुना किया गया है, ताकि तीनों का शिखर 1 पर हो)। दाएँ: एक दौड़। सैकड़ों random nodes में से हर एक को 8 random candidate splits मिलते हैं। क्या entropy और Gini एक ही विजेता चुनते हैं?
多个 class 的 Gini —— 最大值
和 entropy 一样,最大的 Gini Impurity 会随着 class 的数量增加。 最大 Gini 的公式是 (n−1)/n,出现在所有 class 可能性都相同的时候。
| class 数量(n) | 最大 Gini = (n−1)/n | 计算示例(probability 相同) |
|---|---|---|
| 2 | 0.500 | 每个 p=0.5:1−(0.5²+0.5²) = 1−0.5 = 0.500 |
| 3 | 0.667 | 每个 p=1/3:1−3·(1/3)² = 1−3·(1/9) = 1−1/3 = 0.667 |
| 4 | 0.750 | 每个 p=0.25:1−4·(0.25²) = 1−4·0.0625 = 1−0.25 = 0.750 |
| 5 | 0.800 | 每个 p=0.2:1−5·(0.2²) = 1−5·0.04 = 1−0.20 = 0.800 |
| 10 | 0.900 | 每个 p=0.1:1−10·(0.1²) = 1−10·0.01 = 1−0.10 = 0.900 |
Entropy 与 Gini —— 正面对比
三条曲线都是对称的,都在 p = 0.5 达到顶点。Entropy(紫色)离开两端时上升得更陡 —— 在 p=0 和 p=1 附近,它对 probability 的小变化更敏感。Gini(蓝色)是一个更平滑的二次近似。misclassification 率(虚线)是直线,不够平滑 —— 很少使用,因为它缺少另外两个所具备的数学性质。
| 性质 | 说明 |
|---|---|
| 公式 | 1 − Σ pᵢ² |
| 二分类范围 | 0 到 0.5 |
| n 个 class 的最大值 | (n−1)/n |
| 计算 | 快 —— 只做平方,不用 log |
| 行为 | 倾向于更大、更均衡的划分 |
| 敏感度 | 在两端(接近 pure 的 node)不太敏感 |
| 使用者 | CART、sklearn DecisionTree(默认) |
| 性质 | 说明 |
|---|---|
| 公式 | −Σ pᵢ · log₂(pᵢ) |
| 二分类范围 | 0 到 1.0 |
| n 个 class 的最大值 | log₂(n) |
| 计算 | 较慢 —— 每个 node 的每个 class 都要算一次 log |
| 行为 | 对细微的 class 变化更敏感 |
| 敏感度 | 在接近 pure 的 node 上更敏感 |
| 使用者 | ID3、C4.5、sklearn 设置 criterion='entropy' 时 |
动手试试 —— Entropy vs Gini Race
左边:拖动 p,在任意位置比较三条曲线(Gini 和 misclassification 都乘了 2,所以三条曲线的最高点都是 1)。 右边:一场比赛。几百个随机 node,每个都有 8 个随机的候选 split。entropy 和 Gini 会选出同一个赢家吗?
The Bias Problem — and Gain Ratio (C4.5)
Imagine you add a "Day ID" column (D1, D2, … D14) to the weather dataset. Splitting on Day ID gives 14 child nodes, each with exactly 1 sample — perfectly pure! IG = 0.940 bits — maximum possible. But it's useless — it memorises training data and can't generalise. ID3 would always pick it. This is why Ross Quinlan upgraded ID3 to C4.5, which normalises IG by the feature's own entropy (its "split information").
SplitInfo = −(5/14)·log₂(5/14) − (4/14)·log₂(4/14) − (5/14)·log₂(5/14)
= 0.531 + 0.464 + 0.531 = 1.577 bits
GainRatio = 0.246 / 1.577 = 0.156
SplitInfo = 14 × −(1/14)·log₂(1/14) = log₂(14) = 3.807 bits
GainRatio = 0.940 / 3.807 = 0.247
Still wins numerically here but much less dominant — and in real datasets Day ID would never appear as a feature for prediction.
Try It — The Cardinality Trap
Add a completely random feature to the weather data: each day gets one of k values by chance. The feature knows nothing about tennis. Move k and watch Information Gain get fooled badly, while Gain Ratio grows much more slowly.
Bias की समस्या — और Gain Ratio (C4.5)
सोचिए, आप weather dataset में एक "Day ID" column (D1, D2, … D14) जोड़ देते हैं। Day ID पर split करने से 14 child nodes बनते हैं, हर एक में ठीक 1 sample — पूरी तरह pure! IG = 0.940 bits — सबसे ज़्यादा मुमकिन। पर यह बेकार है — यह training data को रट लेता है और नए data पर काम नहीं कर पाता। ID3 इसे हमेशा चुन लेगा। इसीलिए रॉस क्विनलन ने ID3 को सुधारकर C4.5 बनाया, जो IG को feature की अपनी entropy (उसकी "split information") से normalise करता है।
SplitInfo = −(5/14)·log₂(5/14) − (4/14)·log₂(4/14) − (5/14)·log₂(5/14)
= 0.531 + 0.464 + 0.531 = 1.577 bits
GainRatio = 0.246 / 1.577 = 0.156
SplitInfo = 14 × −(1/14)·log₂(1/14) = log₂(14) = 3.807 bits
GainRatio = 0.940 / 3.807 = 0.247
यहाँ संख्या में अब भी जीतता है, पर उसका दबदबा बहुत कम है — और असली datasets में Day ID कभी prediction के लिए feature के रूप में नहीं आएगा।
ख़ुद आज़माइए — Cardinality Trap
Weather data में एक पूरी तरह random feature जोड़िए: हर दिन को संयोग से k में से एक मान मिलता है। इस feature को tennis के बारे में कुछ नहीं पता। k को बदलिए और देखिए Information Gain बुरी तरह धोखा खाता है, जबकि Gain Ratio बहुत धीरे बढ़ता है।
偏向问题 —— 以及 Gain Ratio(C4.5)
想象你给天气 dataset 加上一列 “Day ID”(D1, D2, … D14)。 按 Day ID split 会得到 14 个 child nodes,每个正好只有 1 个 sample —— 完全 pure!IG = 0.940 bits —— 达到最大可能值。但它毫无用处 —— 它只是背下了 training 数据,无法推广到新数据。ID3 每次都会选它。这就是为什么 Ross Quinlan 把 ID3 升级成了 C4.5,它用 feature 自己的 entropy(也就是它的 “split information”)来对 IG 做归一化。
SplitInfo = −(5/14)·log₂(5/14) − (4/14)·log₂(4/14) − (5/14)·log₂(5/14)
= 0.531 + 0.464 + 0.531 = 1.577 bits
GainRatio = 0.246 / 1.577 = 0.156
SplitInfo = 14 × −(1/14)·log₂(1/14) = log₂(14) = 3.807 bits
GainRatio = 0.940 / 3.807 = 0.247
在这里从数字上看它仍然赢了,但优势小多了 —— 而且在真实的 dataset 里, Day ID 永远不会被当作 prediction 用的 feature。
动手试试 —— Cardinality Trap
给天气数据加一个 完全随机 的 feature:每天随机得到 k 个值中的一个。这个 feature 对打网球 一无所知。调整 k,看看 Information Gain 被骗得多厉害,而 Gain Ratio 涨得慢得多。
Python Implementation — From Scratch and with sklearn
Manual Calculation — Entropy, Gini and Information Gain
Python Implementation — शुरू से, और sklearn के साथ
हाथ से हिसाब — Entropy, Gini और Information Gain
Python 实现 —— 从零写起,以及用 sklearn
手动计算 —— Entropy、Gini 和 Information Gain
import numpy as np
# ── Helper functions ──────────────────────────────────────────
def entropy(labels):
"""Shannon entropy of a list of class labels."""
n = len(labels)
if n == 0:
return 0.0
classes, counts = np.unique(labels, return_counts=True)
probs = counts / n
# Convention: 0 * log2(0) = 0
return -np.sum(probs * np.log2(probs + 1e-12))
def gini(labels):
"""Gini impurity of a list of class labels."""
n = len(labels)
if n == 0:
return 0.0
_, counts = np.unique(labels, return_counts=True)
probs = counts / n
return 1 - np.sum(probs ** 2)
def information_gain(parent_labels, child_groups):
"""IG = H(parent) - weighted sum of H(children)."""
n_parent = len(parent_labels)
h_parent = entropy(parent_labels)
weighted_child = sum(
(len(group) / n_parent) * entropy(group)
for group in child_groups
)
return h_parent - weighted_child
def gini_gain(parent_labels, child_groups):
"""Gini Gain = Gini(parent) - weighted Gini(children)."""
n_parent = len(parent_labels)
g_parent = gini(parent_labels)
weighted_child = sum(
(len(group) / n_parent) * gini(group)
for group in child_groups
)
return g_parent - weighted_child
# ── Play Tennis Dataset ───────────────────────────────────────
y = np.array([0,0,1,1,1,0,1,0,1,1,1,1,1,0]) # 0=No, 1=Yes
# Outlook split: Sunny=[0,1,2,7,10] → indices → labels
sunny = y[[0,1,7,8,10]] # D1,D2,D8,D9,D11
overcast = y[[2,6,11,12]] # D3,D7,D12,D13
rain = y[[3,4,5,9,13]] # D4,D5,D6,D10,D14
print("=== ENTROPY ===")
print(f"H(parent) = {entropy(y):.4f}")
print(f"H(Sunny) = {entropy(sunny):.4f}")
print(f"H(Overcast)= {entropy(overcast):.4f}")
print(f"H(Rain) = {entropy(rain):.4f}")
print(f"IG(Outlook)= {information_gain(y, [sunny, overcast, rain]):.4f}")
print("\n=== GINI ===")
print(f"Gini(parent) = {gini(y):.4f}")
print(f"Gini(Sunny) = {gini(sunny):.4f}")
print(f"Gini(Overcast)= {gini(overcast):.4f}")
print(f"Gini(Rain) = {gini(rain):.4f}")
print(f"Gini Gain(Outlook) = {gini_gain(y, [sunny, overcast, rain]):.4f}")
Compare All Attributes at Once
सभी Attributes की एक साथ तुलना
一次比较所有 attribute
import pandas as pd
# Full dataset as a DataFrame
df = pd.DataFrame({
'Outlook' : ['Sunny','Sunny','Overcast','Rain','Rain','Rain',
'Overcast','Sunny','Sunny','Rain','Sunny',
'Overcast','Overcast','Rain'],
'Temp' : ['Hot','Hot','Hot','Mild','Cool','Cool','Cool',
'Mild','Cool','Mild','Mild','Mild','Hot','Mild'],
'Humidity': ['High','High','High','High','Normal','Normal','Normal',
'High','Normal','Normal','Normal','High','Normal','High'],
'Wind' : ['Weak','Strong','Weak','Weak','Weak','Strong','Strong',
'Weak','Weak','Weak','Strong','Strong','Weak','Strong'],
'Play' : [0,0,1,1,1,0,1,0,1,1,1,1,1,0]
})
results = []
for col in ['Outlook', 'Temp', 'Humidity', 'Wind']:
groups = [df[df[col] == v]['Play'].values for v in df[col].unique()]
ig = information_gain(df['Play'].values, groups)
gg = gini_gain(df['Play'].values, groups)
results.append({'Attribute': col, 'Info Gain': ig, 'Gini Gain': gg})
summary = pd.DataFrame(results).sort_values('Info Gain', ascending=False)
print(summary.to_string(index=False, float_format='{:.4f}'.format))
sklearn: Switching Between Gini and Entropy
sklearn: Gini और Entropy के बीच बदलना
sklearn:在 Gini 和 Entropy 之间切换
from sklearn.tree import DecisionTreeClassifier, export_text
from sklearn.preprocessing import OrdinalEncoder
import numpy as np
X_raw = df[['Outlook','Temp','Humidity','Wind']].values
y = df['Play'].values
enc = OrdinalEncoder()
X = enc.fit_transform(X_raw)
for criterion in ['gini', 'entropy']:
dt = DecisionTreeClassifier(criterion=criterion, random_state=42)
dt.fit(X, y)
print(f"\n=== criterion='{criterion}' ===")
print(export_text(dt, feature_names=['Outlook','Temp','Humidity','Wind']))
print("Feature importances:")
for f, imp in zip(['Outlook','Temp','Humidity','Wind'], dt.feature_importances_):
print(f" {f:<12} {imp:.4f}")
Gini and Entropy agree on Outlook as the root, Humidity on the Sunny branch, and Wind on the Rain branch. Temp gets zero importance from both. On real-world noisy data, they may diverge slightly — but the difference in final accuracy is almost always under 1%. Use Gini for speed (default), Entropy when theoretical correctness matters.
Side-by-Side Split Quality Diagram
The diagram below shows the same parent node being split two ways. Split A is good — children are purer. Split B is poor — children are as mixed as the parent.
Try It — The Threshold Finder
Real features are often numbers. Here are the same 14 days with the actual humidity and temperature values (the numeric version of this dataset). Drag the purple line to try a cut, or press Jump to best. The chart below shows the gain of every possible cut.
Gini और Entropy दोनों मानते हैं कि root Outlook है, Sunny शाखा पर Humidity है, और Rain शाखा पर Wind है। दोनों से Temp को zero importance मिलती है। असली दुनिया के noisy data पर ये थोड़ा अलग हो सकते हैं — पर final accuracy का अंतर लगभग हमेशा 1% से कम होता है। तेज़ी के लिए Gini (default) लीजिए, और जब theory के हिसाब से सही होना ज़रूरी हो तब Entropy।
Split Quality का आमने-सामने Diagram
नीचे का diagram एक ही parent node को दो तरह से split होते दिखाता है। Split A अच्छा है — children ज़्यादा pure हैं। Split B कमज़ोर है — children उतने ही मिले-जुले हैं जितना parent।
ख़ुद आज़माइए — Threshold Finder
असली features अक्सर संख्याएँ होते हैं। यहाँ वही 14 दिन हैं, humidity और temperature के असली मानों के साथ (इस dataset का numeric रूप)। कोई cut आज़माने के लिए बैंगनी रेखा को खींचिए, या सबसे अच्छे पर जाओ दबाइए। नीचे का chart हर मुमकिन cut का gain दिखाता है।
Gini 和 Entropy 都同意:Outlook 做 root,Sunny 分支用 Humidity,Rain 分支用 Wind。两者给 Temp 的 importance 都是零。在真实世界有噪声的数据上,它们可能会稍有不同 —— 但最终准确率的差别几乎总是小于 1%。想要速度就用 Gini(默认);看重理论上的正确性时用 Entropy。
split 好坏的并排对比图
下图展示了同一个 parent node 用两种方式 split。Split A 是好的 —— child 更 pure 了。Split B 很差 —— child 和 parent 一样混乱。
动手试试 —— Threshold Finder
真实的 feature 常常是数字。这里还是那 14 天,但用的是 真实的湿度和温度数值 (这个 dataset 的数值版本)。拖动紫色的线来试一个切分点,或者按跳到最佳。下面的图 显示每一个可能切分点的 gain。