Zum Inhalt springen

English:Decision trees, pruning and model comparison - AI Year 8

Aus MOOCsWiki Staging
aiMOOC-Siegel aiMOOC

Decision trees, pruning and model comparison - AI Year 8

QR-Code



Introduction

A simple black classification boundary and a more winding green boundary among two sets of points
A generic illustration of overfitting. It is not a picture of our trees, cards or measured evaluation results.

Does adding another branch make a classifier better? Trace five inputs through two original tree candidates, perform a training-only leaf replacement and compare eight held-out cases. Then inspect a separate final fixture that challenges an overconfident conclusion. Every measurement, label and result belongs to a fictional classroom exercise.


Trace a threshold tree before judging it

A classification tree asks feature questions and follows branches to a leaf containing an output label. A root is the first question; a subtree is a smaller tree below a node. Here depth means the maximum number of feature tests along a root-to-leaf path. It is not the number of leaves or the total number of nodes. A short tree can make its decision path easier to inspect, but being readable does not establish correctness.

Our fictional museum display cards have width in centimetres, mass in grams and a colour tag red or blue. The target is a supplied display-tray label: S standard, X special, H higher-mass or B broad. These labels are invented administrative categories, not physical laws or real packaging and lifting instructions. The two candidate trees below are supplied teaching designs. No program searches for optimal split thresholds, and no CART training algorithm is claimed.

The deeper candidate first asks whether width is greater than ten. If yes, output B. Otherwise ask whether mass is greater than five. If yes, output H. Otherwise ask whether the colour is blue: blue gives X and red gives S. It has three question nodes, four leaves and maximum depth three. The pruned candidate keeps the width and mass questions but replaces the final colour subtree with one S leaf. It has two question nodes, three leaves and maximum depth two. Pruning removes that subtree, not the records used to evaluate it.

Trace input Width cm Mass g Colour Deep output Pruned output
A 10 5 red S S
B 10 5 blue X S
C 10 5.01 blue H H
D 10.01 1 red B B
E missing 2 red ASK ASK

Try the paths before checking the last two columns. First hint: equality is not greater than. Partial step: width exactly ten reaches the mass question. A has mass exactly five and is red, so the deeper model reaches S. B follows the same numerical path, but blue reaches X in the deeper candidate and S in the pruned one. C crosses the mass threshold; D crosses the width threshold. For E, the declared implementation defers when any required feature is missing instead of inventing a value. This ASK rule belongs to our program; some production tree libraries have separately learned missing-value routes. Never assume all trees must use our policy.

CodeAI introduces machine learning from examples. Use it to distinguish a model supplied by an author from split rules actually fitted by an algorithm. In our exercise only the replacement leaf is derived from training labels; the split structure and thresholds were supplied. The independent written trace is the evidence you can inspect without relying on the video.


Prune one subtree using training evidence

Replacing a subtree needs a declared rule. The four training cards reaching the colour question have labels S, S, S and X. Their unique most frequent label is S, so the candidate replacement is one S leaf. This is a training-only calculation. The other four training cards reach H or B and must not vote in the replacement subtree. If no training cards reach it, or the highest label counts tie, our replacement procedure stops rather than guessing.

Training card Width cm Mass g Colour Reference tray
T1 2 1 red S
T2 4 2 red S
T3 8 4 red S
T4 6 3 blue X
T5 5 7 red H
T6 10 9 blue H
T7 12 2 red B
T8 15 10 blue B

The deep tree matches all eight training labels. The pruned tree matches seven because T4 now receives S instead of X. Do not secretly change T4 to make the simpler model look perfect. One unusual example is not automatically a mistake: it might represent a genuine exception, a recording problem or a category requiring more evidence. The current fictional record alone cannot decide which explanation is true.

Consider a deliberately simple classroom score:

J=E+αL

E is the number of wrong training labels, L is the number of leaves and alpha is a nonnegative penalty per leaf. Lower score is preferred. At alpha zero, deep scores zero and pruned one, so deep wins. At alpha one half, the scores are two and two and one half, again preferring deep. At alpha one, both score four; our declared tie rule prefers fewer leaves. At alpha two, deep scores eight and pruned seven. Thus the preference depends on the explicit penalty and tie policy. This total-error teaching score is inspired by a complexity trade-off; it is not scikit-learn's sample-weighted impurity criterion, a full weakest-link pruning search or a library ccp_alpha result.

Try first: explain why the pruned tree's training fit is worse even when its score can be preferred. Hint: separate the two terms. Partial step: its extra error increases E by one while one removed leaf decreases the penalty by alpha. After attempting, check that your explanation preserves both quantities and does not rename a penalty score as accuracy.

The following complete Fraction-based program was actually executed. Its supplied candidates, majority replacement, training penalties, five traces and separate evaluation rows are retained in the output. It rejects unsupported numerical input rather than rounding a string or Boolean into a measurement. Missing features defer; negative values and undeclared colours are invalid. This limited input contract is not a universal rule for decision-tree software.

"""Original supplied tree candidates, one training-only leaf replacement and honest splits."""
from collections import Counter
from fractions import Fraction as F
import json


def exact(value):
    if type(value) not in [int, F] or value < 0:
        raise ValueError("Use nonnegative exact integers or fractions")
    return F(value)


def predict(features, model, replacement="S"):
    if model not in ["deep", "pruned"]:
        raise ValueError("Choose a supplied candidate")
    if type(features) not in [list, tuple] or len(features) != 3:
        raise ValueError("Supply width mass and colour")
    width, mass, colour = features
    if width is None or mass is None or colour is None:
        return "ASK"
    width, mass = exact(width), exact(mass)
    if colour not in ["red", "blue"] or type(colour) is not str:
        raise ValueError("Use a declared colour")
    if width > 10:
        return "B"
    if mass > 5:
        return "H"
    if model == "pruned":
        return replacement
    return "X" if colour == "blue" else "S"


def replacement_from_training(rows):
    if type(rows) not in [list, tuple] or not rows:
        raise ValueError("Supply training rows")
    labels = []
    for row in rows:
        if type(row) not in [list, tuple] or len(row) != 5:
            raise ValueError("Training rows need id width mass colour and label")
        identity, width, mass, colour, label = row
        if type(label) is not str or label not in ["S", "X", "H", "B"]:
            raise ValueError("Use a declared reference label")
        result = predict((width, mass, colour), "deep")
        if result == "ASK":
            raise ValueError("Do not fit a replacement with missing features")
        if exact(width) <= 10 and exact(mass) <= 5:
            labels.append(label)
    if not labels:
        raise ValueError("No training examples reach the replaced subtree")
    counts = Counter(labels)
    winners = [label for label, count in counts.items() if count == max(counts.values())]
    if len(winners) != 1:
        raise ValueError("Replacement majority is tied")
    return winners[0]


def evaluate(rows, model, replacement):
    results = []
    for identity, width, mass, colour, reference in rows:
        predicted = predict((width, mass, colour), model, replacement)
        results.append(dict(id=identity, expected=reference, predicted=predicted,
                            matched=predicted == reference, deferred=predicted == "ASK"))
    return dict(rows=results, cases=len(results), matches=sum(r["matched"] for r in results),
                deferrals=sum(r["deferred"] for r in results),
                wrong_labels=sum(not r["matched"] and not r["deferred"] for r in results))


def demonstrate():
    training = [("T1", 2, 1, "red", "S"), ("T2", 4, 2, "red", "S"),
                ("T3", 8, 4, "red", "S"), ("T4", 6, 3, "blue", "X"),
                ("T5", 5, 7, "red", "H"), ("T6", 10, 9, "blue", "H"),
                ("T7", 12, 2, "red", "B"), ("T8", 15, 10, "blue", "B")]
    validation = [("V1", 5, 2, "blue", "S"), ("V2", 8, 4, "blue", "S"),
                  ("V3", 10, 5, "red", "S"), ("V4", 10, 6, "blue", "H"),
                  ("V5", 6, 8, "red", "H"), ("V6", 11, 2, "blue", "B"),
                  ("V7", 15, 8, "red", "B"), ("V8", 9, 1, "red", "S")]
    final = [("F1", 7, 3, "blue", "X"), ("F2", 3, 2, "blue", "S"),
             ("F3", 10, 8, "red", "H"), ("F4", 14, 2, "blue", "B")]
    replacement = replacement_from_training(training)
    models = dict()
    for name, leaves, depth in [("deep", 4, 3), ("pruned", 3, 2)]:
        models[name] = dict(leaves=leaves, maximum_tests=depth,
                            training=evaluate(training, name, replacement),
                            validation=evaluate(validation, name, replacement),
                            final=evaluate(final, name, replacement))
    scores = []
    for alpha in [F(0), F(1, 2), F(1), F(2)]:
        costs = dict((name, row["training"]["wrong_labels"] + alpha * row["leaves"])
                     for name, row in models.items())
        winner = min(models, key=lambda name: (costs[name], models[name]["leaves"]))
        scores.append(dict(alpha=alpha, costs=costs, preferred=winner))
    trace_inputs = [(10, 5, "red"), (10, 5, "blue"), (10, F(501, 100), "blue"),
                    (F(1001, 100), 1, "red"), (None, 2, "red")]
    traces = [dict(features=x, deep=predict(x, "deep"), pruned=predict(x, "pruned", replacement))
              for x in trace_inputs]
    return dict(training=training, validation=validation, final=final, replacement=replacement,
                models=models, training_penalty_scores=scores, five_traces=traces,
                split_thresholds_supplied=True, CART_split_search_performed=False,
                real_parcel_routing=False, local_generative_model_called=False)


if __name__ == "__main__":
    print(json.dumps(demonstrate(), indent=2, default=str))

Use a school-approved local Python environment if you choose to run it yourself. First write expected paths for the five trace inputs, then preserve actual output separately. A code listing or someone else's execution does not prove your own practical competence. You can complete the reasoning with cards and tables if code execution is unavailable.


Compare held-out evidence and preserve rare cases

Training fit measures agreement on examples already used by the procedure. It cannot establish reliable predictions on new cases. The following eight fictional validation records are held out from the leaf-replacement calculation. Use the same rows for both candidates and retain their supplied labels. If you use these results to choose a model, they are selection evidence rather than an untouched final test.

Validation card Width cm Mass g Colour Reference Deep Pruned
V1 5 2 blue S X S
V2 8 4 blue S X S
V3 10 5 red S S S
V4 10 6 blue H H H
V5 6 8 red H H H
V6 11 2 blue B B B
V7 15 8 red B B B
V8 9 1 red S S S

Deep has six matches and two errors; pruned has eight matches and zero errors. Both answer all eight, so there are no deferrals in this table. A constant-S baseline matches V1, V2, V3 and V8: four of eight. The evidence supports preferring the pruned candidate on these validation cases and for the stated simple task. It refutes the claim that more complexity must be better. It does not prove that pruning always improves generalisation, that colour can never matter or that the model is ready for real use.

Now keep that choice fixed and inspect a separate original final fixture. F1 is width seven, mass three, blue, reference X: deep predicts X but pruned S. F2 is width three, mass two, blue, reference S: deep predicts X but pruned S. F3 is width ten, mass eight, red, reference H: both predict H. F4 is width fourteen, mass two, blue, reference B: both predict B. Each model matches three of four, but the mistakes differ. The chosen pruned model misses the genuine X reference at F1. Equal aggregate accuracy can hide a lost rare category and different consequences. A constant-S baseline matches only F2, one of four.

Attempt before checking: explain whether the final fixture justifies changing the colour rule again. Hint: distinguish evaluation from further design. Partial step: F1 and F2 have the same blue tag but different reference labels. A justified response may call for additional allowed features or better label documentation. If you redesign using these final results, those rows become development evidence; obtain new independent evaluation rather than repeatedly calling the same four rows unseen. Four authored cases cannot support a statistical reliability guarantee.

CodeAI discusses training data and bias. Connect it to which categories occur in the training and evaluation rows. Do not assume a majority leaf is fair simply because it has the most examples. Ask whose legitimate exception might be lost, whether its consequences differ and who can review a disputed result. The generic overfitting illustration at the start is an analogy; it does not demonstrate our model's scores or prove a causal explanation of its mistakes.

For a new fictional craft-library task, choose the target and permitted features before specifying tree thresholds. Compare a readable baseline and candidate on the same separated rows, trace equality and missing inputs, and document rare-category recall as well as total matches. A human review route must have an owner and a response plan; ASK is not a complete safety mechanism. If you change units, threshold numbers must change consistently. A displayed path makes a decision inspectable, but a clear explanation of a wrong decision remains wrong.


Interactive Tasks


Quiz: Test Your Knowledge

At width ten mass five and colour blue what does the deep candidate predict? (X) (!S) (!H) (!B)



What is the maximum depth of the pruned candidate under our convention? (Two feature tests) (!Three feature tests) (!Four leaves) (!Eight training rows)



Which labels determine the replacement leaf? (The training labels reaching the replaced subtree) (!Every final reference label) (!Only the widest card) (!The colours of validation cases)



How many training matches does the pruned candidate have? (Seven of eight) (!Eight of eight) (!Six of eight) (!Four of eight)



At penalty alpha one how does our score choose between the candidates? (Scores tie and the fewer-leaf rule prefers pruned) (!Deep wins because its identifier is first) (!Both are reported as perfect accuracy) (!The validation labels are changed)



Which statement describes the eight validation cases? (Deep has two errors and pruned none) (!Deep has no errors and pruned two) (!Both defer all eight) (!The constant-S baseline matches all eight)



What follows if validation results are used to choose a model? (Fresh independent evaluation is needed for a final claim) (!The same rows remain untouched final tests forever) (!All training rows must be deleted) (!Accuracy becomes guaranteed)



What does the separate final fixture show? (Both match three of four but their mistakes differ) (!Pruned matches all four) (!Deep matches none) (!Rare categories cannot matter)



What does our program do when a required feature is missing? (Returns ASK under its declared policy) (!Invents a zero measurement) (!Always follows the blue branch) (!Proves all tree libraries behave identically)



What was actually fitted in the retained demonstration? (One majority replacement leaf from training labels) (!A complete CART split search) (!A universal museum classification standard) (!An automatically deployed decision service)





Memory Game

Root The first feature question in a tree
Leaf An endpoint containing an output label
Depth The maximum number of feature tests along a path here
Pruning Replacing or removing a subtree to simplify a candidate
Validation Evidence used to compare or select candidates
Generalisation How a method performs beyond its fitting examples





Drag and Drop

Use only labels that reach the replaced branch Replacement fitting
Write the exact equality behaviour Threshold boundary
Trade training mistakes against leaf count Declared penalty score
Compare fixed candidates on the same unused rows Model selection
Retain the cost of losing a legitimate exception Rare-category review





Crossword Puzzle

ROOT What node starts a tree path?
LEAF What endpoint supplies an output label?
DEPTH What counts the longest test path under our convention?
PRUNING What simplification replaces a subtree?
BASELINE What simple comparison tests the benefit of a candidate?
VALIDATION What evidence helps choose between candidate models?





LearningApps

Optional extra practice. Choose an appropriate activity with your teacher. If the external collection is unavailable, use the native activities and independent tasks in this course.


Cloze Text

Complete the explanation.
A tree begins at a

. A path ends at a

. Replacing a subtree is called

. Our replacement label is

. Only the relevant

rows determine that leaf. The deep candidate has

leaves. The pruned candidate has

leaves. The penalty score combines error count with

. Model selection consumes

evidence. Missing features return

. Equal total accuracy can hide different

.



Open Tasks


Easy

  1. Draw two supplied trees: Draw both candidates with question nodes and labelled leaves. Hint: start with the width question. Partial step: the broad branch ends immediately. Compare your leaves and maximum test counts with the text after attempting.
  2. Trace five exact inputs: Hide the output columns of the five-trace table and complete them. Hint: equality is not greater than. Partial step: width ten still reaches mass. Explain why missing width differs from a numerical threshold crossing.
  3. Find the replacement voters: Identify which training cards reach the colour subtree and count their reference labels. Hint: follow width then mass. Partial step: T1 reaches it but T5 does not. Retain all four labels before choosing the majority.
  4. Separate fit from quality: Explain why eight training matches do not prove future reliability. Hint: ask what information has already been used. Use the validation table as a concrete counterexample without making a universal claim.


Standard

  1. Calculate a complexity trade-off: Calculate both penalty scores for alpha zero one half one and two. Hint: keep error count separate from leaf count. Partial step: the deep candidate has zero training errors and four leaves. Apply the stated tie rule only after calculating.
  2. Audit eight held-out rows: Trace all validation records for both candidates and constant S. Hint: use exactly the same cases and references. Record matches wrong labels and deferrals separately and explain whether these rows are used for selection.
  3. Test numerical and missing boundaries: Design equality just-above and missing cases before optionally running the supplied Python. Hint: change one field at a time. Preserve expected results and actual outputs without claiming that missing data is universally handled this way.
  4. Compare final mistakes: Calculate each model and baseline on F1 to F4 and identify who loses the X reference. Hint: total matches alone do not identify consequences. Produce a short report with denominators and limits.


Advanced

  1. Review a rare-category loss: Argue for or against accepting the pruned candidate in a fictional setting where X errors have a high consequence. Hint: request more evidence rather than changing labels. Compare total errors minority-category coverage and a human review plan.
  2. Design a fresh final evaluation: Plan new cases after a team redesigns using F1 to F4. Hint: reused final rows are development evidence. Specify provenance references fixed before testing boundaries rare categories and limits of the sample.
  3. Transfer to craft-library cards: Define a new target feature dictionary and readable tree with a simpler baseline. Hint: do not copy the museum thresholds without justification. Explain changed units missing data consequences and the evidence needed before using it.
  4. Critique an apparent explanation: Evaluate a report claiming a perfectly readable tree is therefore correct and fair. Hint: distinguish path explanation from evidence of validity. Cite the retained counterexamples and propose a proportionate review route.



Learning Check

  1. Trace the boundary set: Give both candidates outputs for the five trace inputs and justify equality.
  2. Justify a replacement leaf: Which training rows vote and why is S chosen?
  3. Calculate and interpret a score: Compare both candidates at alpha one and explain the tie rule.
  4. Evaluate the held-out comparison: Give training validation and final matches and explain the limits.
  5. Explain a rare-case consequence: Why does the final aggregate fail to settle every decision?
  6. Transfer with honest evidence: What must change before a new fictional craft-library tree is evaluated?


Worked checks and assessment guidance

  1. The outputs are S and S then X and S then H and H then B and B then ASK and ASK. Width ten is not greater than ten and mass five is not greater than five. The missing feature invokes our explicit deferral policy before tree traversal rather than a fabricated value.
  2. T1 T2 T3 and T4 reach width at most ten and mass at most five. Their references are S S S X so S has three votes versus one. H and B branches do not contribute. The change loses T4 agreement and reduces the candidates four leaves to three.
  3. Deep has zero errors plus one times four leaves giving four. Pruned has one error plus one times three giving four. Our declared fewer-leaf tie rule chooses pruned. This is a training penalty comparison not an accuracy of four or a full library pruning algorithm.
  4. Deep has eight of eight training six of eight validation and three of four final. Pruned has seven of eight training eight of eight validation and three of four final. The pruned candidate is better on the eight selection cases but the final aggregate is tied. These small fictional fixtures do not establish general reliability and selecting repeatedly with final rows consumes their independence.
  5. Deep matches F1 reference X and fails F2 reference S while pruned fails F1 and matches F2. Both match F3 and F4. Equal three-of-four totals hide different mistakes and potentially different costs. One may need better permitted features more representative evidence or review rather than assuming the minority X label is wrong.
  6. Define target feature units allowed inputs thresholds and a missing-data route. Preserve training-only replacement fitting separate selection evidence and fresh final cases compare a simple baseline on the same rows and review rare-category consequences. Traceable paths and classroom execution do not certify a deployed process or practical safety.


Learning evidence

Retain both labelled trees, five complete traces, the four-row majority calculation, penalty arithmetic at four values, and training/validation/final comparisons with unchanged reference labels. Include a constant-label baseline, missing and just-above-boundary tests, analysis of the lost X case and a new-context evaluation plan. Retain actual Python output only when executed and distinguish it from paper predictions. State that the candidates and splits were supplied; no complete CART search, real routing approval or practical competence is demonstrated.


OER and Wikipedia

scikit-learn: Decision Trees supplies primary technical context for feature tests, overfitting, missing-value differences and minimal cost-complexity pruning. Its pruning criterion uses sample-weighted impurity; our explicitly declared total-training-error-plus-leaves score is a separate teaching simplification, not its ccp_alpha result. Relevant sections inspected 15 November 2026. DfE Computing, Key Stage 3 supplies abstraction and evaluation context. This AI route is editorial enrichment rather than a separate statutory subject.

Overfitting.svg, Chabacano, CC BY-SA 4.0. Original illustration unchanged. It shows a generic simpler black and winding green boundary, not the original display-card data or measured model scores.

https://www.youtube.com/watch?v=OeU5m6vRyCk — CodeAI: AI: What is Machine Learning? https://www.youtube.com/watch?v=x2mRoFNm22g — CodeAI: AI: Training Data & Bias. Actual creator, playability, embedding and preview bytes verified; previews visually inspected. Original recordings embedded unchanged without transcript reproduction, endorsement, redistribution or relicensing implied. The full examples and activities work independently of playback.

The museum cards, tray categories, supplied candidates, training-only majority replacement, validation rows and separate final fixture are original fictional teaching materials. The complete standard-library Python program was actually executed. No real parcel or museum process was controlled, no local generative LLM or paid API was used and no independent expert certification is claimed.


Linked learning areas


aiMOOC Projects



Schulfach+




aiMOOCs



aiMOOC Projekte









MOOCwiki · Deutsch

Nach dem Lernen ist vor dem Lernen

Entdecke direkt den nächsten Lernkurs. Weitere Inhalte erscheinen, wenn Du weiter nach unten scrollst.

Zur MOOCwiki-Hauptseite

Mediathek

Suche verfeinern

Dein Lernplan bestimmt, was oben steht. Alle anderen Inhalte bleiben erreichbar. Seitenaufrufe beeinflussen die Auswahl nicht.

Inhalte werden geladen ...

Mediathek wird aus dem Wiki geladen ...