Skip to main content

πŸ“ Decision Tree

Description​

  • Decision tree is a popular machine learning algorithm used for classification and regression tasks. It works by recursively splitting the data into subsets based on the most significant feature at each node, creating a tree-like structure. The decision tree can be visualized to understand the decision-making process and the importance of different features in the dataset.

Key points​

< Gini impurity >​

  • Node impurity: Gini impurity measures how mixed a classification node's classes are:

    G(node)=1βˆ’βˆ‘k=1Kpk2G(\text{node}) = 1-\sum_{k=1}^{K}p_k^2

    Here, pkp_k is the fraction of examples in class kk within that node. Lower values mean a purer node; G=0G=0 means every example belongs to one class.

  • Split impurity: assess a candidate split using the sample-weighted average of its children's impurities:

    Gsplit=nLnGL+nRnGRG_{\text{split}}=\frac{n_L}{n}G_L+\frac{n_R}{n}G_R
    SymbolMeaning
    GLG_LGini impurity of the left child, calculated from its own class proportions
    GRG_RGini impurity of the right child, calculated from its own class proportions
    nLn_L, nRn_RNumbers of samples in the left and right children
    n=nL+nRn=n_L+n_RNumber of samples in the parent node
  • Gini gain: among eligible candidate splits at the same parent, the tree chooses the lowest weighted child impurity, equivalently the largest decrease from the parent's impurity:

    GiniΒ gain=Gparentβˆ’Gsplit\text{Gini gain}=G_{\text{parent}}-G_{\text{split}}

    β€œGini score” can mean either node impurity or weighted split impurity, so state which one is being used.

  • Example β€” recommend a lipstick to a user: train a decision tree to predict whether a user will click a recommendation for one particular lipstick. Inputs are gender, age, annual income, and nationality; the training label is clicked (1) or did not click (0) after the lipstick was shown. The predicted click probability can be used as a recommendation score.

    1. Start with the same training data for every candidate split. This synthetic dataset is deliberately constructed so gender has the largest Gini gain. It illustrates the calculation, not a general claim about demographic groups or lipstick preferences.

    UserGenderAgeAnnual income (USD)NationalityClicked
    U1Woman2480,000Japan1
    U2Woman2480,000France1
    U3Woman2440,000Japan1
    U4Woman4080,000France1
    U5Woman4040,000Japan1
    U6Man4080,000France1
    U7Man4040,000France1
    U8Man2480,000Japan0
    U9Man2440,000Japan0
    U10Man2440,000France0

    The parent has 7 clicked and 3 did-not-click samples:

    Gparent=1βˆ’(7/10)2βˆ’(3/10)2=0.42G_{\text{parent}}=1-(7/10)^2-(3/10)^2=0.42

    2. Calculate the gender split. The question is β€œIs gender Woman?” Its left child contains the five women; its right child contains the five men in this dataset.

    ChildLabel countsSample countGini impurity
    Left: Woman5 clicked, 0 did not clicknL=5n_L=5GL=1βˆ’(5/5)2βˆ’(0/5)2=0G_L=1-(5/5)^2-(0/5)^2=0
    Right: Man2 clicked, 3 did not clicknR=5n_R=5GR=1βˆ’(2/5)2βˆ’(3/5)2=0.48G_R=1-(2/5)^2-(3/5)^2=0.48

    Weight the child impurities by their sample counts, then subtract from the parent impurity:

    Gsplit,Β gender=510Γ—0+510Γ—0.48=0.24G_{\text{split, gender}}=\frac{5}{10}\times0+\frac{5}{10}\times0.48=0.24 GiniΒ gaingender=0.42βˆ’0.24=0.18\text{Gini gain}_{\text{gender}}=0.42-0.24=0.18

    3. Compare the four candidate splits. Counts below are written as clicked / did not click. Each right child is the complement of the left-child condition.

    Feature / left-child conditionLeft countsRight countsGLG_LGRG_RWeighted child GiniGini gain
    Gender: Woman5 / 02 / 3000.480.480.5Γ—0+0.5Γ—0.48=0.240.5\times0+0.5\times0.48=0.240.180.18
    Age: at most 303 / 34 / 00.500.50000.6Γ—0.50+0.4Γ—0=0.300.6\times0.50+0.4\times0=0.300.120.12
    Income: above USD 60,0004 / 13 / 20.320.320.480.480.5Γ—0.32+0.5Γ—0.48=0.400.5\times0.32+0.5\times0.48=0.400.020.02
    Nationality: Japan3 / 24 / 10.480.480.320.320.5Γ—0.48+0.5Γ—0.32=0.400.5\times0.48+0.5\times0.32=0.400.020.02
    Lipstick click prediction: gender has Gini gain 0.18, age 0.12, income 0.02, and nationality 0.02. Gender is selected in this synthetic dataset.

    4. Choose gender at the root. It has the lowest weighted impurity (0.240.24) and largest gain (0.180.18). The pure left child becomes a leaf; the right child can be split again using the remaining features if the stopping criteria allow it. β€œHelps the most” here means the largest impurity reduction at this node, not a causal effect or a universal feature ranking.

    5. Produce a recommendation score. If the tree stops after this one split, a new user with features (Woman, 24, 40000, Japan) reaches the left leaf and receives an empirical click probability of 5/5=1.05/5=1.0. A user with (Man, 40, 80000, France) reaches the right leaf and receives 2/5=0.42/5=0.4. These tiny-sample estimates demonstrate leaf scoring; they are not reliable real-world click probabilities. A deeper tree may produce different scores.

Comparison​

< Decision Tree vs. Naive Bayes >​

Implementation​

Favorites​

⛳️

Video Tutorial​

Reference​