import%20marimo%0A%0A__generated_with%20%3D%20%220.25.0%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20METADATA%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22id%22%3A%20%22decision_tree%22%2C%0A%20%20%20%20%20%20%20%20%22name%22%3A%20%22Decision%20Tree%22%2C%0A%20%20%20%20%20%20%20%20%22types%22%3A%20%5B%22algorithm%22%5D%2C%0A%20%20%20%20%20%20%20%20%22families%22%3A%20%5B%22trees%22%5D%2C%0A%20%20%20%20%20%20%20%20%22tasks%22%3A%20%5B%22classification%22%2C%20%22regression%22%5D%2C%0A%20%20%20%20%20%20%20%20%22data%22%3A%20%5B%22tabular%22%5D%2C%0A%20%20%20%20%20%20%20%20%22learning%22%3A%20%5B%22supervised%22%5D%2C%0A%20%20%20%20%20%20%20%20%22capacity%22%3A%20%22non_parametric%22%2C%0A%20%20%20%20%20%20%20%20%22mechanisms%22%3A%20%5B%22recursive_partitioning%22%2C%20%22threshold_rules%22%5D%2C%0A%20%20%20%20%20%20%20%20%22properties%22%3A%20%5B%22interpretable%22%2C%20%22nonlinear%22%5D%2C%0A%20%20%20%20%20%20%20%20%22constraints%22%3A%20%5B%22poor_extrapolation%22%2C%20%22sensitive_to_tuning%22%5D%2C%0A%20%20%20%20%20%20%20%20%22difficulty%22%3A%20%22beginner%22%2C%0A%20%20%20%20%20%20%20%20%22status%22%3A%20%22complete%22%2C%0A%20%20%20%20%20%20%20%20%22explainability%22%3A%20%22high%22%2C%0A%20%20%20%20%20%20%20%20%22training_cost%22%3A%20%22low%22%2C%0A%20%20%20%20%20%20%20%20%22inference_cost%22%3A%20%22low%22%2C%0A%20%20%20%20%20%20%20%20%22data_appetite%22%3A%20%22low%22%2C%0A%20%20%20%20%7D%0A%0A%20%20%20%20mo.md(f%22%23%20%7BMETADATA%5B'name'%5D%7D%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20The%20prediction%20rule%0A%0A%20%20%20%20A%20Decision%20Tree%20predicts%20by%20following%20a%20sequence%20of%20feature-threshold%20questions%20to%20a%20leaf.%0A%0A%20%20%20%20%23%23%20Ask%20one%20useful%20question%20at%20a%20time%0A%0A%20%20%20%20It%20is%20a%20learned%20flowchart.%20A%20loan%20example%20might%20travel%20through%20%22income%20%3C%2040k%3F%22%2C%20then%20%22missed%20payments%20%3E%201%3F%22%2C%20then%20arrive%20at%20a%20leaf%20containing%20a%20default%20probability.%0A%0A%20%20%20%20This%20readability%20is%20strongest%20for%20a%20shallow%20tree.%20A%20tree%20with%20hundreds%20of%20nodes%20is%20technically%20rule-based%20but%20no%20longer%20easy%20to%20reason%20about.%0A%0A%20%20%20%20%23%23%20Input%20and%20output%0A%0A%20%20%20%20-%20**Input%3A**%20mainly%20tabular%20numerical%20or%20encoded%20categorical%20features.%0A%20%20%20%20-%20**Output%3A**%20class%2C%20class%20probability%2C%20or%20numeric%20leaf%20average.%0A%20%20%20%20-%20**Learns%3A**%20which%20feature%20and%20threshold%20to%20split%20at%20each%20node.%0A%0A%20%20%20%20%23%23%20How%20it%20works%0A%0A%20%20%20%20At%20a%20node%2C%20training%20evaluates%20candidate%20splits%20and%20chooses%20one%20that%20most%20reduces%20impurity%20or%20prediction%20error.%20Classification%20commonly%20uses%20Gini%20impurity%20or%20entropy%3B%20regression%20commonly%20uses%20squared%20or%20absolute%20error.%0A%0A%20%20%20%20The%20process%20repeats%20recursively.%20Unrestricted%20growth%20can%20create%20leaves%20containing%20very%20few%20training%20examples%2C%20producing%20nearly%20perfect%20training%20performance%20and%20poor%20generalization.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Step%20by%20step%3A%20choose%20one%20split%0A%0A%20%20%20%20For%20binary%20classification%2C%20Gini%20impurity%20at%20a%20node%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cboxed%7BG%3D1-p_0%5E2-p_1%5E2%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20where%20%24p_0%24%20and%20%24p_1%24%20are%20the%20class%20proportions.%0A%0A%20%20%20%20Suppose%20a%20node%20contains%2010%20examples%3A%206%20from%20class%200%20and%204%20from%20class%201.%0A%0A%20%20%20%20%24%24%0A%20%20%20%20G_%7Bparent%7D%3D1-0.6%5E2-0.4%5E2%3D0.48.%0A%20%20%20%20%24%24%0A%0A%20%20%20%20A%20candidate%20threshold%20creates%3A%0A%0A%20%20%20%20-%20left%20child%3A%205%20class-0%20and%201%20class-1%20example%3B%0A%20%20%20%20-%20right%20child%3A%201%20class-0%20and%203%20class-1%20examples.%0A%0A%20%20%20%20Their%20impurities%20are%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20G_%7Bleft%7D%3D1-(5%2F6)%5E2-(1%2F6)%5E2%5Capprox0.278%2C%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20G_%7Bright%7D%3D1-(1%2F4)%5E2-(3%2F4)%5E2%3D0.375.%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Weight%20each%20child%20by%20its%20size%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20G_%7Bchildren%7D%3D%5Cfrac6%7B10%7D(0.278)%2B%5Cfrac4%7B10%7D(0.375)%5Capprox0.317.%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20impurity%20decrease%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5CDelta%20G%3D0.48-0.317%3D0.163.%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Training%20evaluates%20many%20feature-threshold%20pairs%20and%20chooses%20the%20one%20with%20the%20largest%20valid%20decrease.%20It%20then%20repeats%20the%20same%20calculation%20inside%20each%20child.%0A%0A%20%20%20%20At%20a%20leaf%2C%20a%20class%20probability%20is%20the%20observed%20class%20fraction%20in%20that%20leaf.%20A%20regression%20leaf%20instead%20predicts%20the%20mean%20target%20of%20its%20training%20examples.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20A%20practical%20example%0A%0A%20%20%20%20For%20support-ticket%20escalation%2C%20a%20shallow%20tree%20can%20expose%20a%20policy-like%20structure%3A%20severe%20category%2C%20then%20customer%20tier%2C%20then%20unresolved%20duration.%20That%20can%20be%20useful%20for%20discussion%20with%20domain%20experts%20even%20if%20an%20ensemble%20ultimately%20serves%20predictions.%0A%0A%20%20%20%20%23%23%20When%20to%20use%20it%0A%0A%20%20%20%20-%20You%20need%20a%20quick%20nonlinear%20baseline.%0A%20%20%20%20-%20Thresholds%20and%20interactions%20are%20likely.%0A%20%20%20%20-%20Feature%20scaling%20should%20be%20minimal.%0A%20%20%20%20-%20A%20compact%20set%20of%20human-readable%20rules%20has%20value.%0A%20%20%20%20-%20You%20want%20to%20inspect%20structure%20before%20moving%20to%20an%20ensemble.%0A%0A%20%20%20%20%23%23%20When%20to%20avoid%20it%0A%0A%20%20%20%20-%20Small%20data%20changes%20must%20not%20change%20model%20behavior.%0A%20%20%20%20-%20Smooth%20responses%20or%20extrapolation%20are%20required.%0A%20%20%20%20-%20A%20deep%20tree%20is%20being%20treated%20as%20automatically%20interpretable.%0A%20%20%20%20-%20The%20target%20is%20dominated%20by%20additive%20linear%20trends%20a%20simpler%20model%20handles%20better.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Important%20controls%0A%0A%20%20%20%20%7C%20Control%20%7C%20Role%20%7C%0A%20%20%20%20%7C---------%7C------%7C%0A%20%20%20%20%7C%20%60max_depth%60%20%7C%20Caps%20rule-chain%20length%20%7C%0A%20%20%20%20%7C%20%60min_samples_leaf%60%20%7C%20Prevents%20tiny%2C%20fragile%20leaves%20%7C%0A%20%20%20%20%7C%20%60max_features%60%20%7C%20Limits%20features%20considered%20at%20a%20split%20%7C%0A%20%20%20%20%7C%20Cost-complexity%20pruning%20%7C%20Trades%20fit%20for%20a%20smaller%20tree%20%7C%0A%20%20%20%20%7C%20%60class_weight%60%20%7C%20Changes%20emphasis%20for%20imbalanced%20labels%20%7C%0A%0A%20%20%20%20Prefer%20constraints%20such%20as%20minimum%20leaf%20size%20over%20growing%20an%20enormous%20tree%20and%20hoping%20it%20generalizes.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20Notebook%20%E2%80%94%20depth%20as%20a%20complexity%20budget%0A%0A%20%20%20%20**Question%3A**%20when%20does%20adding%20rules%20stop%20helping%20unseen%20data%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20from%20sklearn.datasets%20import%20make_moons%0A%20%20%20%20from%20sklearn.inspection%20import%20DecisionBoundaryDisplay%0A%20%20%20%20from%20sklearn.model_selection%20import%20train_test_split%0A%20%20%20%20from%20sklearn.tree%20import%20DecisionTreeClassifier%0A%0A%20%20%20%20X%2C%20y%20%3D%20make_moons(n_samples%3D500%2C%20noise%3D0.28%2C%20random_state%3D12)%0A%20%20%20%20X_train%2C%20X_test%2C%20y_train%2C%20y_test%20%3D%20train_test_split(%0A%20%20%20%20%20%20%20%20X%2C%20y%2C%20test_size%3D0.4%2C%20stratify%3Dy%2C%20random_state%3D12%0A%20%20%20%20)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20DecisionBoundaryDisplay%2C%0A%20%20%20%20%20%20%20%20DecisionTreeClassifier%2C%0A%20%20%20%20%20%20%20%20X%2C%0A%20%20%20%20%20%20%20%20X_test%2C%0A%20%20%20%20%20%20%20%20X_train%2C%0A%20%20%20%20%20%20%20%20plt%2C%0A%20%20%20%20%20%20%20%20y_test%2C%0A%20%20%20%20%20%20%20%20y_train%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20We%20first%20create%20one%20nonlinear%20classification%20problem%20and%20hold%20out%20test%20examples.%0A%20%20%20%20Now%20depth%20is%20the%20only%20quantity%20allowed%20to%20change.%20Plotting%20both%20scores%20will%20show%0A%20%20%20%20when%20extra%20splits%20keep%20memorizing%20training%20data%20without%20helping%20new%20data.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(DecisionTreeClassifier%2C%20X_test%2C%20X_train%2C%20plt%2C%20y_test%2C%20y_train)%3A%0A%20%20%20%20depths%20%3D%20range(1%2C%2016)%0A%20%20%20%20train_scores%2C%20test_scores%20%3D%20%5B%5D%2C%20%5B%5D%0A%20%20%20%20for%20_depth%20in%20depths%3A%0A%20%20%20%20%20%20%20%20_tree%20%3D%20DecisionTreeClassifier(max_depth%3D_depth%2C%20random_state%3D0).fit(X_train%2C%20y_train)%0A%20%20%20%20%20%20%20%20train_scores.append(_tree.score(X_train%2C%20y_train))%0A%20%20%20%20%20%20%20%20test_scores.append(_tree.score(X_test%2C%20y_test))%0A%0A%20%20%20%20plt.plot(depths%2C%20train_scores%2C%20marker%3D%22o%22%2C%20label%3D%22training%22)%0A%20%20%20%20plt.plot(depths%2C%20test_scores%2C%20marker%3D%22o%22%2C%20label%3D%22test%22)%0A%20%20%20%20plt.xlabel(%22max%20depth%22)%0A%20%20%20%20plt.ylabel(%22accuracy%22)%0A%20%20%20%20plt.legend()%0A%20%20%20%20plt.title(%22Training%20keeps%20improving%3B%20generalization%20need%20not%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20score%20curves%20tell%20us%20**whether**%20depth%20overfits.%20The%20next%20code%20shows%20**how**%3A%0A%20%20%20%20three%20decision%20surfaces%20reveal%20the%20rectangular%20regions%20created%20as%20the%20tree%20asks%0A%20%20%20%20more%20and%20more%20axis-aligned%20questions.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20DecisionBoundaryDisplay%2C%0A%20%20%20%20DecisionTreeClassifier%2C%0A%20%20%20%20X%2C%0A%20%20%20%20X_test%2C%0A%20%20%20%20X_train%2C%0A%20%20%20%20plt%2C%0A%20%20%20%20y_test%2C%0A%20%20%20%20y_train%2C%0A)%3A%0A%20%20%20%20fig%2C%20axes%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(10%2C%204))%0A%20%20%20%20for%20ax%2C%20_depth%20in%20zip(axes%2C%20%5B2%2C%2012%5D)%3A%0A%20%20%20%20%20%20%20%20_tree%20%3D%20DecisionTreeClassifier(max_depth%3D_depth%2C%20random_state%3D0).fit(X_train%2C%20y_train)%0A%20%20%20%20%20%20%20%20DecisionBoundaryDisplay.from_estimator(_tree%2C%20X%2C%20ax%3Dax%2C%20alpha%3D0.3)%0A%20%20%20%20%20%20%20%20ax.scatter(X_test%5B%3A%2C%200%5D%2C%20X_test%5B%3A%2C%201%5D%2C%20c%3Dy_test%2C%20s%3D12%2C%20edgecolor%3D%22k%22%2C%20linewidth%3D0.2)%0A%20%20%20%20%20%20%20%20ax.set_title(f%22max_depth%3D%7B_depth%7D%22)%0A%20%20%20%20plt.tight_layout()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20Evaluation%20and%20diagnosis%0A%0A%20%20%20%20Compare%20training%20and%20validation%20curves%20as%20depth%20increases.%20Inspect%20leaf%20sample%20counts%20and%20probability%20calibration.%20Refit%20with%20different%20random%20splits%3A%20large%20structural%20changes%20reveal%20instability.%0A%0A%20%20%20%20Feature%20importance%20based%20on%20impurity%20can%20favor%20continuous%20or%20high-cardinality%20features.%20Use%20permutation-based%20checks%20and%20domain%20review%20before%20drawing%20conclusions.%0A%0A%20%20%20%20%23%23%20Cost%20profile%0A%0A%20%20%20%20Both%20training%20and%20prediction%20are%20generally%20fast%20for%20modest%20trees.%20Inference%20follows%20only%20one%20path%2C%20so%20a%20shallow%20tree%20is%20easy%20to%20serve.%0A%0A%20%20%20%20%23%23%20Related%20models%0A%0A%20%20%20%20-%20**Random%20Forest**%20averages%20many%20randomized%20trees%20to%20reduce%20variance.%0A%20%20%20%20-%20**Gradient%20Boosting**%20builds%20trees%20sequentially%20to%20correct%20errors.%0A%20%20%20%20-%20**Logistic%20Regression**%20provides%20a%20smoother%2C%20more%20stable%20baseline.%0A%0A%20%20%20%20%23%23%20What%20a%20tree%20is%20really%20buying%20you%0A%0A%20%20%20%20Use%20a%20shallow%20tree%20to%20discover%20useful%20thresholds%20and%20interactions.%20Treat%20depth%20as%20a%20budget%2C%20not%20a%20target.%0A%0A%20%20%20%20%23%23%20Concept%20references%0A%0A%20%20%20%20-%20%5BBias%20and%20Variance%5D(%2Fconcepts%2Fbias_variance)%20%E2%80%94%20why%20deep%20trees%20are%20sensitive%20to%20the%20sample.%0A%20%20%20%20-%20%5BRegularization%5D(%2Fconcepts%2Fregularization)%20%E2%80%94%20how%20depth%20and%20leaf%20constraints%20express%20a%20preference.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
694cdc394b62e6aaff335bd35b2c7a66