%22%22%22A%20marimo%20notebook%20that%20edits%20the%20graphs%20of%20an%20EBM%2C%20with%20the%20report%20after%20each%20edit.%22%22%22%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20tempfile%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20narwhals%20as%20nw%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20polars%20as%20pl%0A%20%20%20%20from%20interpret.glassbox%20import%20ExplainableBoostingClassifier%0A%0A%20%20%20%20from%20ebaml%20import%20(%0A%20%20%20%20%20%20%20%20Align%2C%0A%20%20%20%20%20%20%20%20Bins%2C%0A%20%20%20%20%20%20%20%20Bounded%2C%0A%20%20%20%20%20%20%20%20Categories%2C%0A%20%20%20%20%20%20%20%20EBMGraph%2C%0A%20%20%20%20%20%20%20%20EditSession%2C%0A%20%20%20%20%20%20%20%20ExtendRange%2C%0A%20%20%20%20%20%20%20%20FixedScore%2C%0A%20%20%20%20%20%20%20%20Interpolate%2C%0A%20%20%20%20%20%20%20%20MaxStep%2C%0A%20%20%20%20%20%20%20%20Monotone%2C%0A%20%20%20%20%20%20%20%20Monotonize%2C%0A%20%20%20%20%20%20%20%20SelectionError%2C%0A%20%20%20%20%20%20%20%20SetScores%2C%0A%20%20%20%20%20%20%20%20Shift%2C%0A%20%20%20%20%20%20%20%20Subgroup%2C%0A%20%20%20%20%20%20%20%20UnsupportedModelError%2C%0A%20%20%20%20%20%20%20%20ValueInterval%2C%0A%20%20%20%20%20%20%20%20WholeGraph%2C%0A%20%20%20%20%20%20%20%20affected_profile%2C%0A%20%20%20%20%20%20%20%20check_constraints%2C%0A%20%20%20%20%20%20%20%20edit_effect%2C%0A%20%20%20%20%20%20%20%20load_gamchanger%2C%0A%20%20%20%20%20%20%20%20save_gamchanger%2C%0A%20%20%20%20)%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20Align%2C%0A%20%20%20%20%20%20%20%20Bins%2C%0A%20%20%20%20%20%20%20%20Bounded%2C%0A%20%20%20%20%20%20%20%20Categories%2C%0A%20%20%20%20%20%20%20%20EBMGraph%2C%0A%20%20%20%20%20%20%20%20EditSession%2C%0A%20%20%20%20%20%20%20%20ExplainableBoostingClassifier%2C%0A%20%20%20%20%20%20%20%20ExtendRange%2C%0A%20%20%20%20%20%20%20%20FixedScore%2C%0A%20%20%20%20%20%20%20%20Interpolate%2C%0A%20%20%20%20%20%20%20%20MaxStep%2C%0A%20%20%20%20%20%20%20%20Monotone%2C%0A%20%20%20%20%20%20%20%20Monotonize%2C%0A%20%20%20%20%20%20%20%20Path%2C%0A%20%20%20%20%20%20%20%20SelectionError%2C%0A%20%20%20%20%20%20%20%20SetScores%2C%0A%20%20%20%20%20%20%20%20Shift%2C%0A%20%20%20%20%20%20%20%20Subgroup%2C%0A%20%20%20%20%20%20%20%20UnsupportedModelError%2C%0A%20%20%20%20%20%20%20%20ValueInterval%2C%0A%20%20%20%20%20%20%20%20WholeGraph%2C%0A%20%20%20%20%20%20%20%20affected_profile%2C%0A%20%20%20%20%20%20%20%20check_constraints%2C%0A%20%20%20%20%20%20%20%20edit_effect%2C%0A%20%20%20%20%20%20%20%20load_gamchanger%2C%0A%20%20%20%20%20%20%20%20mo%2C%0A%20%20%20%20%20%20%20%20np%2C%0A%20%20%20%20%20%20%20%20nw%2C%0A%20%20%20%20%20%20%20%20pl%2C%0A%20%20%20%20%20%20%20%20save_gamchanger%2C%0A%20%20%20%20%20%20%20%20tempfile%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Editing%20a%20model%0A%0A%20%20%20%20This%20notebook%20edits%20the%20graphs%20of%20an%20EBM%20with%20%60EditSession%60%2C%20as%20the%20study%20of%20GAM%20Changer%20(Wang%20et%20al.%2C%20KDD%202022)%0A%20%20%20%20edits%20a%20model%20of%20the%20risk%20of%20pneumonia%3A%20smooth%20a%20jump%2C%20flatten%20a%20dip%2C%20make%20a%20range%20monotone%2C%20and%20neutralize%20a%0A%20%20%20%20category.%20It%20then%20checks%20business%20constraints%2C%20adds%20a%20bin%20where%20the%20model%20saw%20no%20data%2C%20measures%20the%20effect%20of%20the%0A%20%20%20%20edits%20on%20the%20data%20and%20on%20a%20subgroup%2C%20and%20saves%20the%20edits%20as%20a%20file%20that%20GAM%20Changer%20opens.%20At%20the%20end%2C%20it%20edits%20one%0A%20%20%20%20class%20of%20a%20model%20with%20three%20classes.%0A%0A%20%20%20%20-%20You%20need%20no%20key%20and%20no%20download.%20The%20data%20are%20synthetic.%0A%20%20%20%20-%20No%20cell%20sends%20a%20request%20to%20an%20LLM.%20The%20edits%20are%20plain%20computations.%0A%20%20%20%20-%20Each%20edit%20shows%20its%20report%20first%20(%60preview%60)%2C%20and%20then%20applies%20it%20with%20the%20reason%20that%20a%20person%20gives.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20The%20data%20and%20the%20model%0A%0A%20%20%20%20The%20risk%20of%20death%20grows%20with%20the%20age.%20Two%20defects%20of%20the%20data%20make%20the%20graph%20of%20the%20age%20wrong%3A%20a%20jump%20near%2060%2C%20and%0A%20%20%20%20a%20dip%20for%20the%20oldest%20patients%2C%20who%20are%20few%20and%20were%20treated%20with%20special%20care.%20The%20model%20also%20has%20a%20category%0A%20%20%20%20%60unknown%60%20of%20the%20feature%20%60ward%60%20with%20no%20real%20meaning.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ExplainableBoostingClassifier%2C%20np%2C%20pl)%3A%0A%20%20%20%20random_generator%20%3D%20np.random.default_rng(7)%0A%20%20%20%20patient_count%20%3D%203000%0A%20%20%20%20age%20%3D%20random_generator.uniform(20%2C%20100%2C%20patient_count).round()%0A%20%20%20%20ward%20%3D%20random_generator.choice(%5B%22general%22%2C%20%22intensive%22%2C%20%22unknown%22%5D%2C%20patient_count%2C%20p%3D%5B0.55%2C%200.3%2C%200.15%5D)%0A%20%20%20%20jump_age%20%3D%20np.isin(age%2C%20np.arange(58%2C%2066))%20%20%23%20a%20jump%20of%20the%20risk%20near%20the%20age%20of%2060%0A%20%20%20%20dip_age%20%3D%20age%20%3E%20np.quantile(age%2C%200.88)%20%20%23%20the%20oldest%20patients%2C%20who%20are%20few%20and%20had%20special%20care%0A%20%20%20%20logit%20%3D%200.05%20*%20(age%20-%2060)%20%2B%200.8%20*%20(ward%20%3D%3D%20%22intensive%22)%20-%200.5%20%2B%201.0%20*%20jump_age%0A%20%20%20%20logit%20%3D%20logit%20-%202.0%20*%20dip_age%20%2B%200.6%20*%20(ward%20%3D%3D%20%22unknown%22)%0A%20%20%20%20died%20%3D%20(random_generator.uniform(size%3Dpatient_count)%20%3C%201%20%2F%20(1%20%2B%20np.exp(-logit))).astype(int)%0A%20%20%20%20patients%20%3D%20pl.DataFrame(%7B%22age%22%3A%20age%2C%20%22ward%22%3A%20ward%7D)%0A%20%20%20%20ebm%20%3D%20ExplainableBoostingClassifier(%0A%20%20%20%20%20%20%20%20feature_names%3Dpatients.columns%2C%0A%20%20%20%20%20%20%20%20feature_types%3D%5B%22continuous%22%2C%20%22nominal%22%5D%2C%0A%20%20%20%20%20%20%20%20interactions%3D0%2C%0A%20%20%20%20%20%20%20%20random_state%3D0%2C%0A%20%20%20%20).fit(patients%2C%20died)%0A%20%20%20%20return%20died%2C%20ebm%2C%20patients%0A%0A%0A%40app.cell%0Adef%20_(EBMGraph%2C%20ebm%2C%20mo)%3A%0A%20%20%20%20age_graph%20%3D%20EBMGraph.from_ebm(ebm%2C%200).to_text(include_description%3DFalse%2C%20confidence_bounds%3DFalse)%0A%20%20%20%20mo.md(f%22The%20graph%20of%20the%20age%20before%20any%20edit%3A%5Cn%5Cn%60%60%60%5Cn%7Bage_graph%7D%5Cn%60%60%60%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(EditSession%2C%20ebm%2C%20mo)%3A%0A%20%20%20%20session%20%3D%20EditSession(ebm)%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22A%20session%20works%20on%20a%20copy%20of%20the%20model%2C%20so%20%60ebm%60%20does%20not%20change.%20The%20head%20of%20the%20session%20is%20%22%0A%20%20%20%20%20%20%20%20f%22%60%7Bsession.revision%7D%60.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%20(session%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Smooth%20a%20jump%0A%0A%20%20%20%20The%20graph%20jumps%20up%20and%20down%20near%20the%20age%20of%2060.%20%60Interpolate%60%20draws%20a%20straight%20line%20from%20the%20first%20to%20the%20last%0A%20%20%20%20selected%20score%2C%20and%20it%20keeps%20the%20bins.%20%60ValueInterval%60%20selects%20every%20bin%20that%20overlaps%20the%20ages%2055%20to%2070%2C%20and%20the%0A%20%20%20%20report%20names%20the%20bins%20that%20it%20chose.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Interpolate%2C%20ValueInterval%2C%20mo%2C%20session)%3A%0A%20%20%20%20jump_edit%20%3D%20Interpolate(%22age%22%2C%20ValueInterval(55%2C%2070))%0A%20%20%20%20jump_report%20%3D%20session.preview(jump_edit)%0A%20%20%20%20mo.md(f%22%60%60%60%5Cn%7Bjump_report.to_text()%7D%5Cn%60%60%60%22)%0A%20%20%20%20return%20(jump_edit%2C)%0A%0A%0A%40app.cell%0Adef%20_(jump_edit%2C%20mo%2C%20session)%3A%0A%20%20%20%20session.apply(jump_edit%2C%20%22The%20jump%20near%2060%20has%20no%20cause%20in%20the%20data%22)%0A%20%20%20%20mo.md(f%22Applied.%20The%20head%20is%20now%20%60%7Bsession.revision%7D%60.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Flatten%20a%20dip%0A%0A%20%20%20%20The%20oldest%20patients%20are%20few%2C%20and%20the%20graph%20falls%20for%20them.%20%60Align%60%20with%20%60%22left%22%60%20copies%20the%20score%20of%20the%20bin%0A%20%20%20%20before%20the%20selection%20to%20the%20selected%20bins%2C%20so%20the%20graph%20stays%20flat%20there.%20The%20report%20shows%20how%20little%20of%20the%0A%20%20%20%20training%20weight%20these%20bins%20hold.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Align%2C%20Bins%2C%20ebm%2C%20mo%2C%20session)%3A%0A%20%20%20%20last_bin%20%3D%20len(ebm.term_scores_%5B0%5D)%20-%203%0A%20%20%20%20dip_edit%20%3D%20Align(%22age%22%2C%20Bins(last_bin%20-%205%2C%20last_bin)%2C%20%22left%22)%0A%20%20%20%20mo.md(f%22%60%60%60%5Cn%7Bsession.preview(dip_edit).to_text()%7D%5Cn%60%60%60%22)%0A%20%20%20%20return%20(dip_edit%2C)%0A%0A%0A%40app.cell%0Adef%20_(dip_edit%2C%20mo%2C%20session)%3A%0A%20%20%20%20session.apply(dip_edit%2C%20%22Too%20few%20patients%20over%2090%20to%20trust%20the%20fall%20of%20the%20graph%22)%0A%20%20%20%20mo.md(f%22Applied.%20The%20head%20is%20now%20%60%7Bsession.revision%7D%60.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Make%20a%20range%20monotone%0A%0A%20%20%20%20The%20risk%20must%20not%20fall%20when%20the%20age%20goes%20up.%20%60Monotonize%60%20fits%20a%20weighted%20isotonic%20regression%20on%20the%20selected%0A%20%20%20%20bins.%20The%20selection%20can%20be%20a%20part%20of%20the%20graph%2C%20and%20the%20rest%20stays%20as%20it%20is.%20The%20edit%20keeps%20the%20weighted%20mean%20of%0A%20%20%20%20the%20selected%20scores.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Monotonize%2C%20mo%2C%20session)%3A%0A%20%20%20%20monotone_edit%20%3D%20Monotonize(%22age%22%2C%20%22increasing%22)%0A%20%20%20%20monotone_report%20%3D%20session.preview(monotone_edit)%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20f%22The%20edit%20changes%20%7Bmonotone_report.bins_changed%7D%20bins%20and%20reaches%20%7Bmonotone_report.weight_share%3A.1%25%7D%20of%20the%20%22%0A%20%20%20%20%20%20%20%20%22training%20weight.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%20(monotone_edit%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20monotone_edit%2C%20session)%3A%0A%20%20%20%20session.apply(monotone_edit%2C%20%22The%20risk%20of%20death%20must%20not%20fall%20with%20the%20age%22)%0A%20%20%20%20mo.md(f%22Applied.%20The%20head%20is%20now%20%60%7Bsession.revision%7D%60.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Neutralize%20a%20category%0A%0A%20%20%20%20The%20category%20%60unknown%60%20of%20the%20ward%20has%20no%20real%20meaning%2C%20so%20the%20model%20must%20not%20score%20it.%20%60SetScores%60%20sets%20its%20bin%20to%0A%20%20%20%200%20and%20names%20the%20category.%20A%20category%20that%20does%20not%20exist%20raises%20a%20%60SelectionError%60%20with%20the%20categories%20that%20do.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Categories%2C%20SelectionError%2C%20SetScores%2C%20mo%2C%20session)%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20session.preview(SetScores(%22ward%22%2C%20Categories(%5B%22emergency%22%5D)%2C%200.0))%0A%20%20%20%20except%20SelectionError%20as%20error%3A%0A%20%20%20%20%20%20%20%20failure%20%3D%20str(error)%0A%20%20%20%20mo.md(f%22A%20wrong%20category%20fails%20with%20the%20fix%20steps%3A%5Cn%5Cn%3E%20%7Bfailure%7D%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Categories%2C%20SetScores%2C%20mo%2C%20session)%3A%0A%20%20%20%20ward_edit%20%3D%20SetScores(%22ward%22%2C%20Categories(%5B%22unknown%22%5D)%2C%200.0)%0A%20%20%20%20mo.md(f%22%60%60%60%5Cn%7Bsession.preview(ward_edit).to_text()%7D%5Cn%60%60%60%22)%0A%20%20%20%20return%20(ward_edit%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20session%2C%20ward_edit)%3A%0A%20%20%20%20session.apply(ward_edit%2C%20%22The%20ward%20unknown%20is%20a%20gap%20in%20the%20records%2C%20and%20it%20must%20not%20move%20the%20risk%22)%0A%20%20%20%20mo.md(f%22Applied.%20The%20head%20is%20now%20%60%7Bsession.revision%7D%60.%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Business%20constraints%0A%0A%20%20%20%20A%20constraint%20states%20a%20business%20requirement.%20%60check_constraints%60%20reads%20the%20model%20and%20changes%20nothing.%20It%20gives%20one%0A%20%20%20%20violation%20for%20each%20constraint%20that%20the%20model%20breaks%2C%20with%20the%20bins%20that%20break%20it%20and%20the%20edit%20that%20repairs%20it.%20Here%0A%20%20%20%20the%20risk%20must%20rise%20with%20the%20age%2C%20the%20ward%20%60unknown%60%20must%20score%200%2C%20the%20risk%20must%20not%20jump%20by%20more%20than%200.3%20between%0A%20%20%20%20two%20neighbour%20ages%2C%20and%20no%20score%20of%20the%20age%20may%20leave%20the%20range%20from%20-3%20to%203.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20Bounded%2C%0A%20%20%20%20Categories%2C%0A%20%20%20%20FixedScore%2C%0A%20%20%20%20MaxStep%2C%0A%20%20%20%20Monotone%2C%0A%20%20%20%20check_constraints%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20pl%2C%0A%20%20%20%20session%2C%0A)%3A%0A%20%20%20%20constraints%20%3D%20%5B%0A%20%20%20%20%20%20%20%20Monotone(%22age%22%2C%20%22increasing%22)%2C%0A%20%20%20%20%20%20%20%20FixedScore(%22ward%22%2C%20Categories(%5B%22unknown%22%5D)%2C%200.0)%2C%0A%20%20%20%20%20%20%20%20MaxStep(%22age%22%2C%200.3)%2C%0A%20%20%20%20%20%20%20%20Bounded(%22age%22%2C%20-3.0%2C%203.0)%2C%0A%20%20%20%20%5D%0A%20%20%20%20violations%20%3D%20check_constraints(session.model%2C%20constraints)%0A%20%20%20%20violation_rows%20%3D%20pl.DataFrame(%0A%20%20%20%20%20%20%20%20%5B%7B%22violation%22%3A%20item.description%2C%20%22repair%22%3A%20type(item.repair).__name__%7D%20for%20item%20in%20violations%5D%2C%0A%20%20%20%20%20%20%20%20schema%3D%7B%22violation%22%3A%20pl.String%2C%20%22repair%22%3A%20pl.String%7D%2C%0A%20%20%20%20)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22The%20model%20breaks%20%7Blen(violations)%7D%20of%20%7Blen(constraints)%7D%20constraints.%22)%2C%0A%20%20%20%20%20%20%20%20mo.ui.table(violation_rows%2C%20selection%3DNone)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20constraints%2C%20violations%0A%0A%0A%40app.cell%0Adef%20_(check_constraints%2C%20constraints%2C%20mo%2C%20session%2C%20violations)%3A%0A%20%20%20%20for%20violation%20in%20violations%3A%0A%20%20%20%20%20%20%20%20session.apply(violation.repair%2C%20f%22Meet%20the%20business%20constraint%3A%20%7Bviolation.description%7D%22)%0A%20%20%20%20remaining%20%3D%20check_constraints(session.model%2C%20constraints)%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20f%22Applied%20the%20repair%20of%20each%20of%20the%20%7Blen(violations)%7D%20violations.%20A%20repair%20of%20one%20constraint%20can%20break%20another%22%0A%20%20%20%20%20%20%20%20f%22%20one%2C%20so%20check%20again%3A%20the%20model%20breaks%20%7Blen(remaining)%7D%20of%20the%20constraints%20now.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Change%20the%20bins%0A%0A%20%20%20%20The%20model%20saw%20no%20patient%20above%20the%20age%20of%20100%2C%20so%20the%20last%20bin%20covers%20every%20higher%20age.%20%60ExtendRange%60%20adds%20an%20empty%0A%20%20%20%20bin%20above%20the%20oldest%20patient%2C%20with%20the%20score%20of%20the%20last%20bin%2C%20so%20no%20prediction%20changes.%20A%20later%20edit%20gives%20the%20new%0A%20%20%20%20bin%20its%20own%20score%3A%20here%20the%20expert%20knows%20that%20the%20risk%20keeps%20growing.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Bins%2C%20ExtendRange%2C%20SetScores%2C%20mo%2C%20session)%3A%0A%20%20%20%20oldest%20%3D%20float(session.model.feature_bounds_%5B0%5D%5B1%5D)%0A%20%20%20%20session.apply(ExtendRange(%22age%22%2C%20high%3D120.0)%2C%20%22Ages%20above%20the%20data%20must%20have%20their%20own%20score%22)%0A%20%20%20%20new_bin%20%3D%20len(session.model.bins_%5B0%5D%5B0%5D)%0A%20%20%20%20last_score%20%3D%20float(session.model.term_scores_%5B0%5D%5Bnew_bin%5D)%0A%20%20%20%20extension_edit%20%3D%20SetScores(%22age%22%2C%20Bins(new_bin%2C%20new_bin)%2C%20last_score%20%2B%200.5)%0A%20%20%20%20extension_report%20%3D%20session.preview(extension_edit)%0A%20%20%20%20session.apply(extension_edit%2C%20%22The%20risk%20keeps%20growing%20above%20the%20oldest%20patient%22)%0A%20%20%20%20mo.md(f%22The%20oldest%20patient%20was%20%7Boldest%3A.0f%7D.%20The%20new%20bin%3A%5Cn%5Cn%60%60%60%5Cn%7Bextension_report.to_text()%7D%5Cn%60%60%60%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20The%20effect%20on%20the%20predictions%0A%0A%20%20%20%20An%20edit%20changes%20the%20predictions%20of%20the%20model%2C%20so%20look%20at%20the%20effect%20before%20you%20use%20the%20model.%20The%20table%20shows%20the%0A%20%20%20%20change%20of%20the%20predicted%20risk%20for%20a%20few%20patients.%20The%20next%20two%20tables%20check%20the%20data%20and%20the%20subgroups%2C%20as%20the%0A%20%20%20%20Metric%20Panel%20and%20the%20Feature%20Panel%20of%20GAM%20Changer%20do.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ebm%2C%20mo%2C%20np%2C%20patients%2C%20pl%2C%20session)%3A%0A%20%20%20%20edited%20%3D%20session.model%0A%20%20%20%20comparison%20%3D%20pl.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22age%22%3A%20patients%5B%22age%22%5D%2C%0A%20%20%20%20%20%20%20%20%22ward%22%3A%20patients%5B%22ward%22%5D%2C%0A%20%20%20%20%20%20%20%20%22risk%20before%22%3A%20ebm.predict_proba(patients)%5B%3A%2C%201%5D.round(3)%2C%0A%20%20%20%20%20%20%20%20%22risk%20after%22%3A%20edited.predict_proba(patients)%5B%3A%2C%201%5D.round(3)%2C%0A%20%20%20%20%7D).with_columns((pl.col(%22risk%20after%22)%20-%20pl.col(%22risk%20before%22)).alias(%22change%22))%0A%20%20%20%20largest%20%3D%20comparison.sort(pl.col(%22change%22).abs()%2C%20descending%3DTrue).head(8)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22The%20mean%20absolute%20change%20of%20the%20risk%20is%20%60%7Bnp.abs(comparison%5B'change'%5D).mean()%3A.4f%7D%60.%22)%2C%0A%20%20%20%20%20%20%20%20mo.ui.table(largest%2C%20selection%3DNone)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Subgroup%2C%20died%2C%20edit_effect%2C%20mo%2C%20nw%2C%20patients%2C%20pl%2C%20session)%3A%0A%20%20%20%20effect%20%3D%20edit_effect(session%2C%20nw.from_native(patients)%2C%20died%2C%20Subgroup(%22ward%22%2C%20%22intensive%22))%0A%20%20%20%20scope_rows%20%3D%20pl.DataFrame(%5B%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22scope%22%3A%20scope.scope%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22rows%22%3A%20scope.row_count%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22accuracy%2C%20original%22%3A%20None%20if%20scope.original%20is%20None%20else%20round(scope.original.accuracy%2C%203)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22accuracy%2C%20before%20the%20last%20edit%22%3A%20None%20if%20scope.last%20is%20None%20else%20round(scope.last.accuracy%2C%203)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22accuracy%2C%20current%22%3A%20None%20if%20scope.current%20is%20None%20else%20round(scope.current.accuracy%2C%203)%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20for%20scope%20in%20effect.scopes%0A%20%20%20%20%5D)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22A%20global%20metric%20hardly%20moves%20when%20a%20few%20bins%20change%2C%20so%20read%20the%20rows%20that%20the%20last%20edit%20changes%20and%20a%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%20subgroup%20too.%20The%20last%20edit%20here%20is%20the%20score%20of%20the%20new%20bin%2C%20which%20reaches%20no%20patient%20of%20the%20data.%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20mo.ui.table(scope_rows%2C%20selection%3DNone)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(affected_profile%2C%20mo%2C%20nw%2C%20patients%2C%20pl%2C%20session)%3A%0A%20%20%20%20profile%20%3D%20affected_profile(session%2C%20nw.from_native(patients)%2C%20revision%3Dsession.history_table()%5B0%5D.id)%0A%20%20%20%20profile_rows%20%3D%20pl.DataFrame(%5B%0A%20%20%20%20%20%20%20%20%7B%22feature%22%3A%20feature.feature%2C%20%22bins%22%3A%20%22%2C%20%22.join(feature.bins)%2C%20%22l1%20distance%22%3A%20round(feature.distance%2C%203)%7D%0A%20%20%20%20%20%20%20%20for%20feature%20in%20profile.features%0A%20%20%20%20%5D)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22The%20first%20edit%20(the%20jump%20near%2060)%20changes%20%7Bprofile.affected_rows%7D%20of%20%7Bprofile.total_rows%7D%20patients.%20For%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%20each%20other%20feature%2C%20the%20l1%20distance%20compares%20its%20mix%20among%20these%20patients%20with%20all%20patients.%20A%20large%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%20distance%20means%20that%20the%20edit%20reaches%20one%20subgroup%20more%20than%20the%20others.%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20mo.ui.table(profile_rows%2C%20selection%3DNone)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20The%20history%0A%0A%20%20%20%20Each%20edit%20is%20a%20revision%20with%20its%20reason%2C%20a%20time%20and%20a%20flag%20for%20the%20review.%20%60model_at%60%20reads%20any%20revision%20without%0A%20%20%20%20a%20checkout.%20A%20save%20is%20a%20commit%20too%3A%20it%20needs%20a%20message%20and%20refuses%20edits%20that%20nobody%20reviewed.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20pl%2C%20session)%3A%0A%20%20%20%20rows%20%3D%20pl.DataFrame(%5B%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22id%22%3A%20row.short_id%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22edit%22%3A%20row.edit_type%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22feature%22%3A%20row.feature%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22reason%22%3A%20row.reason%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22reviewed%22%3A%20row.reviewed%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20for%20row%20in%20session.history_table()%0A%20%20%20%20%5D)%0A%20%20%20%20mo.ui.table(rows%2C%20selection%3DNone)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Open%20the%20edits%20in%20GAM%20Changer%0A%0A%20%20%20%20%60save_gamchanger%60%20writes%20the%20original%20model%20and%20the%20edits%20as%20a%20file%20that%20the%20GAM%20Changer%20widget%20opens%2C%20so%20a%20person%0A%20%20%20%20can%20check%20the%20edits%20there.%20Without%20data%2C%20the%20file%20holds%20synthetic%20rows%20made%20from%20the%20bins%2C%20because%20the%20widget%20needs%0A%20%20%20%20rows%20to%20start.%20The%20result%20lists%20what%20the%20file%20cannot%20hold%2C%20and%20%60load_gamchanger%60%20reads%20the%20file%20back.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Path%2C%20ebm%2C%20load_gamchanger%2C%20mo%2C%20pl%2C%20save_gamchanger%2C%20session%2C%20tempfile)%3A%0A%20%20%20%20export%20%3D%20save_gamchanger(session%2C%20Path(tempfile.mkdtemp())%20%2F%20%22pneumonia.gamchanger%22)%0A%20%20%20%20loaded%20%3D%20load_gamchanger(export.path%2C%20ebm)%0A%20%20%20%20commit_rows%20%3D%20pl.DataFrame(%5B%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22feature%22%3A%20commit.feature%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22type%22%3A%20commit.commit_type%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22edits%22%3A%20len(commit.edits)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22description%22%3A%20commit.description%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20for%20commit%20in%20loaded%0A%20%20%20%20%5D)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22The%20file%20holds%20%7Bexport.commit_count%7D%20commits%20and%20%7Bexport.sample_count%7D%20synthetic%20rows.%20It%20leaves%20out%3A%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%20%7B'%3B%20'.join(export.left_out)%20or%20'nothing'%7D.%20Read%20back%2C%20the%20newest%20commit%20of%20each%20feature%3A%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20mo.ui.table(commit_rows%2C%20selection%3DNone)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20A%20model%20with%20three%20classes%0A%0A%20%20%20%20A%20multiclass%20model%20has%20one%20score%20for%20each%20class%20in%20each%20bin.%20Here%20the%20outcome%20of%20a%20patient%20is%20%60recovered%60%2C%20%60icu%60%0A%20%20%20%20or%20%60died%60.%20An%20edit%20takes%20%60class_label%60%2C%20the%20class%20whose%20scores%20it%20changes%2C%20or%20%60None%60%20for%20every%20class.%20Softmax%20reads%0A%20%20%20%20only%20the%20differences%20between%20the%20classes%2C%20so%20an%20edit%20of%20one%20class%20changes%20the%20probabilities%20of%20every%20class.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ExplainableBoostingClassifier%2C%20died%2C%20np%2C%20patients)%3A%0A%20%20%20%20outcome_generator%20%3D%20np.random.default_rng(8)%0A%20%20%20%20intensive%20%3D%20(patients%5B%22ward%22%5D%20%3D%3D%20%22intensive%22).to_numpy()%0A%20%20%20%20icu_share%20%3D%200.6%20%20%23%20the%20share%20of%20the%20patients%20of%20the%20intensive%20ward%20who%20survive%20after%20a%20stay%20in%20the%20ICU%0A%20%20%20%20went_to_icu%20%3D%20intensive%20%26%20(outcome_generator.uniform(size%3Dlen(died))%20%3C%20icu_share)%0A%20%20%20%20outcome%20%3D%20np.where(died%20%3D%3D%201%2C%20%22died%22%2C%20np.where(went_to_icu%2C%20%22icu%22%2C%20%22recovered%22))%0A%20%20%20%20outcome_ebm%20%3D%20ExplainableBoostingClassifier(%0A%20%20%20%20%20%20%20%20feature_names%3Dpatients.columns%2C%0A%20%20%20%20%20%20%20%20feature_types%3D%5B%22continuous%22%2C%20%22nominal%22%5D%2C%0A%20%20%20%20%20%20%20%20interactions%3D0%2C%0A%20%20%20%20%20%20%20%20random_state%3D0%2C%0A%20%20%20%20).fit(patients%2C%20outcome)%0A%20%20%20%20return%20outcome%2C%20outcome_ebm%0A%0A%0A%40app.cell%0Adef%20_(EditSession%2C%20Monotonize%2C%20mo%2C%20outcome_ebm)%3A%0A%20%20%20%20outcome_session%20%3D%20EditSession(outcome_ebm)%0A%20%20%20%20outcome_report%20%3D%20outcome_session.apply(%0A%20%20%20%20%20%20%20%20Monotonize(%22age%22%2C%20%22increasing%22%2C%20class_label%3D%22died%22)%2C%0A%20%20%20%20%20%20%20%20%22The%20risk%20of%20death%20must%20not%20fall%20when%20the%20age%20goes%20up%22%2C%0A%20%20%20%20)%0A%20%20%20%20mo.md(f%22The%20report%20names%20the%20class%20of%20each%20change%3A%5Cn%5Cn%60%60%60%5Cn%7Boutcome_report.to_text()%7D%5Cn%60%60%60%22)%0A%20%20%20%20return%20(outcome_session%2C)%0A%0A%0A%40app.cell%0Adef%20_(Shift%2C%20WholeGraph%2C%20mo%2C%20np%2C%20outcome_ebm%2C%20outcome_session%2C%20patients)%3A%0A%20%20%20%20every_class%20%3D%20outcome_session.preview(Shift(%22ward%22%2C%20WholeGraph()%2C%201.0))%0A%20%20%20%20shifted%20%3D%20outcome_session.model.copy()%0A%20%20%20%20Shift(%22ward%22%2C%20WholeGraph()%2C%201.0).run(shifted)%0A%20%20%20%20unchanged%20%3D%20np.allclose(shifted.predict_proba(patients)%2C%20outcome_session.model.predict_proba(patients))%0A%20%20%20%20moved%20%3D%20np.abs(outcome_session.model.predict_proba(patients)%20-%20outcome_ebm.predict_proba(patients)).max(axis%3D0)%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20f%22The%20monotone%20edit%20of%20%60died%60%20moves%20the%20largest%20probability%20of%20each%20class%20(%7B'%2C%20'.join(outcome_ebm.classes_)%7D)%22%0A%20%20%20%20%20%20%20%20f%22%20by%20%7B'%2C%20'.join(f'%7Bchange%3A.3f%7D'%20for%20change%20in%20moved)%7D.%20The%20same%20shift%20of%20every%20class%2C%22%0A%20%20%20%20%20%20%20%20f%22%20%60%7Bevery_class.description%7D%60%2C%20changes%20%7B'no'%20if%20unchanged%20else%20'some'%7D%20probability.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20Path%2C%0A%20%20%20%20UnsupportedModelError%2C%0A%20%20%20%20edit_effect%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20nw%2C%0A%20%20%20%20outcome%2C%0A%20%20%20%20outcome_session%2C%0A%20%20%20%20patients%2C%0A%20%20%20%20pl%2C%0A%20%20%20%20save_gamchanger%2C%0A%20%20%20%20tempfile%2C%0A)%3A%0A%20%20%20%20outcome_effect%20%3D%20edit_effect(outcome_session%2C%20nw.from_native(patients)%2C%20outcome)%0A%20%20%20%20current_metrics%20%3D%20outcome_effect.scopes%5B0%5D.current%0A%20%20%20%20matrix_rows%20%3D%20pl.DataFrame(%0A%20%20%20%20%20%20%20%20%5Blist(row)%20for%20row%20in%20current_metrics.confusion_matrix%5D%2C%0A%20%20%20%20%20%20%20%20schema%3D%5Bf%22predicted%20%7Blabel%7D%22%20for%20label%20in%20current_metrics.classes%5D%2C%0A%20%20%20%20%20%20%20%20orient%3D%22row%22%2C%0A%20%20%20%20).insert_column(0%2C%20pl.Series(%22true%20class%22%2C%20list(current_metrics.classes)))%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20save_gamchanger(outcome_session%2C%20Path(tempfile.mkdtemp())%20%2F%20%22outcome.gamchanger%22)%0A%20%20%20%20%20%20%20%20refusal%20%3D%20%22none%22%0A%20%20%20%20except%20UnsupportedModelError%20as%20error%3A%0A%20%20%20%20%20%20%20%20refusal%20%3D%20str(error)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22The%20edited%20model%20has%20accuracy%20%60%7Bcurrent_metrics.accuracy%3A.3f%7D%60%20and%20a%20mean%20one-versus-rest%20ROC%20AUC%20of%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%20%60%7Bcurrent_metrics.roc_auc%3A.3f%7D%60.%20The%20confusion%20matrix%20of%20all%20classes%3A%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20mo.ui.table(matrix_rows%2C%20selection%3DNone)%2C%0A%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22A%20%60.gamchanger%60%20file%20holds%20one%20score%20in%20each%20bin%2C%20so%20the%20export%20fails%20with%20the%20fix%20steps%3A%5Cn%5Cn%3E%20%7Brefusal%7D%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%5D)%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
9a3da213db01c4f6e0f7488969cf363e