ENع
→Decision trees
الدرس 07 · تعلّم الآلة التقليدي

Decision trees

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة9 خطوات5 أسئلةIntermediate
عن هذا الدرس

A decision tree predicts by asking a short chain of yes/no questions about the features. Learning one means choosing those questions: try every "feature ≤ threshold" split, keep the one that leaves the purest groups by Gini impurity, and repeat on each group until it is pure or the tree is deep enough. Watch eight students grow a two-question tree, predict a new student, and see why deep trees overfit.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 7 · تفاعلي
01 / 09

Eight labelled students

Our training data: eight students. For each we know two features, hours studied and hours slept, and a label: did they pass or fail the quiz? A decision tree learns to predict the label by asking yes/no questions.

hours  sleep  label
  1      8     fail
  2      6     fail
  2      9     fail
  3      7     pass
  4      4     fail
  4      8     pass
  5      5     fail
  6      6     pass
5 fail, 3 pass
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Eight labelled students

    Our training data: eight students. For each we know two features, hours studied and hours slept, and a label: did they pass or fail the quiz? A decision tree learns to predict the label by asking yes/no questions.

    hours  sleep  label
      1      8     fail
      2      6     fail
      2      9     fail
      3      7     pass
      4      4     fail
      4      8     pass
      5      5     fail
      6      6     pass
    5 fail, 3 pass
  2. How mixed is a group?

    All eight start at the root. A group is pure when every label matches. Gini impurity measures the mix: 1 minus each label's share squared. Here 5 fail, 3 pass: 1 − (5/8)² − (3/8)² = 0.47.

    def gini(labels):
        n = len(labels)
        return 1 - sum((labels.count(c) / n) ** 2
                       for c in set(labels))
    
    root: 5 fail, 3 pass
    gini = 1 - (5/8)**2 - (3/8)**2 = 30/64 = 0.46875
    pure group -> 0 · half and half -> 0.5
  3. Try every question

    The learner tries every question "feature ≤ threshold", with thresholds halfway between neighbouring values: 10 in all. Each splits the group in two. Its score: the two Gini values, weighted by group size. Lower means purer.

    question       yes   no    weighted Gini
    hours ≤ 2.5?   3     5     0.30
    hours ≤ 5.5?   7     1     0.36
    sleep ≤ 5.5?   2     6     0.38
    sleep ≤ 7.5?   5     3     0.47
    ... 10 questions tried in all, 4 shown
  4. Keep the best question

    "hours ≤ 2.5?" scores lowest. Yes: 3 students, all fail, Gini 0. No: 2 fail, 3 pass, Gini 0.48. Weighted: 3/8 × 0 + 5/8 × 0.48 = 0.30. The two groups become child nodes.

    best: hours ≤ 2.5?   weighted Gini 0.30
    yes  (1,8) (2,6) (2,9)   all fail
    no   (3,7) (4,4) (4,8) (5,5) (6,6)
         2 fail, 3 pass -> Gini 0.48
    impurity: 0.47 -> 0.30
  5. A pure group is a leaf

    The yes group is pure: all three failed. No question can make it purer, so growing stops there and it becomes a leaf. A leaf holds an answer instead of a question: here, fail.

    labels = ["fail", "fail", "fail"]
    gini(labels) -> 0.0
    gini == 0  -> stop: return "fail"
    
    leaf = an answer, not a question
  6. Repeat on the mixed group

    The no group is still mixed, so the same recipe runs again on just those five. Now "sleep ≤ 5.5?" wins: 2 fail on the yes side, 3 pass on the no side. Both are pure, so both become leaves.

    rows: (3,7) (4,4) (4,8) (5,5) (6,6)
    best_split(rows) -> sleep ≤ 5.5?   weighted Gini 0.00
    yes  (4,4) (5,5)   fail, fail
    no   (3,7) (4,8) (6,6)   pass x3
    the same function, called again on a smaller group:
    that is recursion
  7. The tree is a rule

    Every leaf is pure, so growing stops. The tree has depth 2 and 3 leaves, and it is just nested if statements: the kind of rule a person might write, but found from the data.

    def predict(hours, sleep):
        if hours <= 2.5:
            return "fail"
        if sleep <= 5.5:
            return "fail"
        return "pass"
    
    print(tree) -> (0, 2.5, 'fail', (1, 5.5, 'fail', 'pass'))
    (feature 0 = hours, feature 1 = sleep)
  8. Predict a new student

    A new student studied 4 hours and slept 7. Is hours ≤ 2.5? No, go right. Is sleep ≤ 5.5? No, go right. That leaf says pass. Each prediction costs only two comparisons.

    predict(4, 7)
      hours <= 2.5?  4 <= 2.5  -> no
      sleep <= 5.5?  7 <= 5.5  -> no
      return "pass"
    the tree never saw this student; it answers anyway
  9. Deep trees memorise noise

    Real data is noisy. Add one odd student: 5 hours studied, 9 slept, failed. Grown until every leaf is pure, the tree adds "sleep ≤ 8.5?" just for that student: overfitting. With max_depth = 2, the leaf keeps its majority: pass.

    odd student: (5, 9, fail)
    no depth cap: depth 3, 4 leaves
    max_depth=2:  depth 2, 3 leaves, same questions
    its leaf: 3 pass, 1 fail -> answers "pass"
    
    limits that fight overfitting:
      max_depth, min_samples_leaf, pruning

اختبار يتاح بعد الخطوة الأخيرة.