%22%22%22A%20marimo%20notebook%20that%20shows%20the%20graphs%20and%20the%20descriptions%20of%20a%20multiclass%20EBM.%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%20marimo%20as%20mo%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%20%20%20%20from%20sklearn.datasets%20import%20load_iris%0A%0A%20%20%20%20from%20ebaml%20import%20(%0A%20%20%20%20%20%20%20%20DescribeEBMRequest%2C%0A%20%20%20%20%20%20%20%20DescribeGraphRequest%2C%0A%20%20%20%20%20%20%20%20EBMGraph%2C%0A%20%20%20%20%20%20%20%20GraphOptions%2C%0A%20%20%20%20%20%20%20%20InvalidOptionError%2C%0A%20%20%20%20%20%20%20%20Normalization%2C%0A%20%20%20%20%20%20%20%20UnsupportedModelError%2C%0A%20%20%20%20%20%20%20%20describe_ebm%2C%0A%20%20%20%20%20%20%20%20describe_graph%2C%0A%20%20%20%20%20%20%20%20model_kind%2C%0A%20%20%20%20)%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20DescribeEBMRequest%2C%0A%20%20%20%20%20%20%20%20DescribeGraphRequest%2C%0A%20%20%20%20%20%20%20%20EBMGraph%2C%0A%20%20%20%20%20%20%20%20ExplainableBoostingClassifier%2C%0A%20%20%20%20%20%20%20%20GraphOptions%2C%0A%20%20%20%20%20%20%20%20InvalidOptionError%2C%0A%20%20%20%20%20%20%20%20Normalization%2C%0A%20%20%20%20%20%20%20%20UnsupportedModelError%2C%0A%20%20%20%20%20%20%20%20describe_ebm%2C%0A%20%20%20%20%20%20%20%20describe_graph%2C%0A%20%20%20%20%20%20%20%20load_iris%2C%0A%20%20%20%20%20%20%20%20mo%2C%0A%20%20%20%20%20%20%20%20model_kind%2C%0A%20%20%20%20%20%20%20%20np%2C%0A%20%20%20%20%20%20%20%20pl%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%20Multiclass%20classifiers%0A%0A%20%20%20%20This%20notebook%20shows%20how%20EBaMl%20reads%20an%20EBM%20with%20three%20or%20more%20classes%3A%20the%20kind%20of%20the%20model%2C%20the%20graph%20with%20one%0A%20%20%20%20series%20per%20class%2C%20the%20options%20that%20select%20and%20shift%20the%20classes%2C%20and%20the%20description%20by%20the%20LLM.%0A%0A%20%20%20%20-%20You%20need%20no%20data%20download%3A%20the%20iris%20data%20comes%20with%20scikit-learn.%0A%20%20%20%20-%20Everything%20outside%20the%20buttons%20runs%20without%20a%20key%20and%20sends%20no%20request.%0A%20%20%20%20-%20The%20buttons%20at%20the%20end%20send%20real%20requests.%20The%20default%20model%20needs%20%60OPENROUTER_API_KEY%60.%20Pass%20%60model%3D%60%20to%20use%0A%20%20%20%20%20%20another%20model%2C%20as%20the%20documentation%20page%20%22Choosing%20the%20model%22%20shows.%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%20A%20multiclass%20EBM%0A%0A%20%20%20%20The%20iris%20data%20has%20three%20classes%20of%20flower%20and%20four%20features.%20%60model_kind%60%20reads%20the%20kind%20of%20the%20fitted%20EBM.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ExplainableBoostingClassifier%2C%20load_iris%2C%20mo%2C%20model_kind%2C%20pl)%3A%0A%20%20%20%20iris%20%3D%20load_iris(as_frame%3DTrue)%0A%20%20%20%20features%20%3D%20pl.from_pandas(iris.data).rename(lambda%20name%3A%20name.removesuffix(%22%20(cm)%22).replace(%22%20%22%2C%20%22_%22))%0A%20%20%20%20species%20%3D%20pl.Series(%22species%22%2C%20iris.target_names%5Biris.target%5D)%0A%20%20%20%20%23%20no%20pairwise%20interactions%3A%20the%20LLM%20reads%20one%20graph%20per%20feature%0A%20%20%20%20ebm%20%3D%20ExplainableBoostingClassifier(feature_names%3Dfeatures.columns%2C%20interactions%3D0%2C%20random_state%3D42).fit(%0A%20%20%20%20%20%20%20%20features%2C%0A%20%20%20%20%20%20%20%20species%2C%0A%20%20%20%20)%0A%20%20%20%20kind%20%3D%20model_kind(ebm)%0A%20%20%20%20petal_index%20%3D%20list(features.columns).index(%22petal_length%22)%0A%20%20%20%20mo.md(f%22%60model_kind(ebm)%60%20is%20%60%7Bkind!r%7D%60%2C%20with%20the%20outputs%20%60%7Bkind.outputs%7D%60.%5Cn%5Cn%7Bkind.score_meaning%7D%22)%0A%20%20%20%20return%20ebm%2C%20features%2C%20kind%2C%20petal_index%2C%20species%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%20One%20series%20per%20class%0A%0A%20%20%20%20The%20graph%20of%20a%20feature%20has%20one%20score%20per%20bin%20and%20per%20class.%20Softmax%20gives%20the%20same%20probabilities%20when%20the%20same%0A%20%20%20%20number%20is%20added%20to%20the%20scores%20of%20all%20the%20classes%20of%20a%20bin.%20So%20%60EBMGraph.from_ebm%60%20centres%20each%20bin%3A%20the%20mean%20of%0A%20%20%20%20its%20scores%20over%20the%20classes%20is%20zero.%20The%20text%20has%20one%20series%20of%20means%20and%20bounds%20for%20each%20class%2C%20after%20a%0A%20%20%20%20%60Class%3A%60%20line.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(EBMGraph%2C%20ebm%2C%20mo%2C%20np%2C%20petal_index)%3A%0A%20%20%20%20graph%20%3D%20EBMGraph.from_ebm(ebm%2C%20petal_index)%0A%20%20%20%20largest_mean%20%3D%20float(np.abs(np.asarray(graph.scores).mean(axis%3D1)).max())%0A%20%20%20%20text%20%3D%20graph.to_text(y_axis_precision%3D2%2C%20confidence_bounds%3DFalse)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22The%20largest%20mean%20of%20a%20bin%20over%20the%20classes%20is%20%60%7Blargest_mean%3A.1e%7D%60.%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22%60%60%60%5Cn%7Btext%7D%5Cn%60%60%60%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20(graph%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%20The%20centring%20keeps%20the%20predictions%0A%0A%20%20%20%20The%20intercept%20plus%20the%20centred%20score%20of%20each%20feature%20gives%20the%20same%20probabilities%20as%20the%20EBM%2C%20because%20the%20centring%0A%20%20%20%20only%20adds%20one%20number%20per%20bin%20to%20all%20the%20classes.%20The%20cell%20compares%20them%20for%20the%20first%20five%20flowers.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(EBMGraph%2C%20ebm%2C%20features%2C%20kind%2C%20mo%2C%20np)%3A%0A%20%20%20%20scores%20%3D%20np.tile(np.asarray(ebm.intercept_%2C%20dtype%3Dfloat)%2C%20(5%2C%201))%0A%20%20%20%20for%20index%2C%20_%20in%20enumerate(features.columns)%3A%0A%20%20%20%20%20%20%20%20feature_graph%20%3D%20EBMGraph.from_ebm(ebm%2C%20index%2C%20use_feature_bounds%3DFalse)%0A%20%20%20%20%20%20%20%20%23%20the%20bin%20of%20each%20value%2C%20from%20the%20cut%20points%20of%20the%20EBM%20(one%20bin%20per%20interval)%0A%20%20%20%20%20%20%20%20cuts%20%3D%20np.asarray(ebm.bins_%5Bindex%5D%5B0%5D%2C%20dtype%3Dfloat)%0A%20%20%20%20%20%20%20%20bins%20%3D%20np.searchsorted(cuts%2C%20features%5B%3A5%2C%20index%5D.to_numpy()%2C%20side%3D%22right%22)%0A%20%20%20%20%20%20%20%20scores%20%2B%3D%20np.asarray(feature_graph.scores)%5Bbins%5D%0A%20%20%20%20difference%20%3D%20float(np.abs(kind.predict(scores)%20-%20ebm.predict_proba(features.head(5))).max())%0A%20%20%20%20mo.md(f%22The%20largest%20difference%20from%20%60ebm.predict_proba%60%20is%20%60%7Bdifference%3A.1e%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%20Select%20the%20classes%0A%0A%20%20%20%20The%20text%20of%20a%20model%20with%20many%20classes%20is%20long.%20%60classes%60%20keeps%20some%20classes%2C%20in%20the%20order%20that%20you%20give.%20The%0A%20%20%20%20scores%20stay%20centred%20over%20all%20the%20classes%20of%20the%20model%2C%20so%20a%20bin%20of%20the%20selected%20classes%20need%20not%20have%20a%20mean%20of%0A%20%20%20%20zero.%20%60describe_graph%60%20and%20%60describe_ebm%60%20take%20the%20same%20option%20as%20%60GraphOptions(classes%3D%5B...%5D)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(EBMGraph%2C%20ebm%2C%20mo%2C%20petal_index)%3A%0A%20%20%20%20two%20%3D%20EBMGraph.from_ebm(ebm%2C%20petal_index%2C%20classes%3D%5B%22virginica%22%2C%20%22setosa%22%5D)%0A%20%20%20%20mo.md(f%22%60%60%60%5Cn%7Btwo.to_text(y_axis_precision%3D2%2C%20confidence_bounds%3DFalse%2C%20include_description%3DFalse)%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%20Shift%20the%20scores%0A%0A%20%20%20%20%60Normalization.MIN_MAX%60%20and%20%60Normalization.Z_SCORE%60%20shift%20all%20the%20scores%20of%20the%20graph%20by%20one%20number.%20The%0A%20%20%20%20differences%20between%20the%20classes%20of%20a%20bin%2C%20which%20carry%20the%20meaning%2C%20do%20not%20change.%20Here%20the%20smallest%20score%20becomes%0A%20%20%20%20zero.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(EBMGraph%2C%20Normalization%2C%20ebm%2C%20graph%2C%20mo%2C%20np%2C%20petal_index)%3A%0A%20%20%20%20shifted%20%3D%20EBMGraph.from_ebm(ebm%2C%20petal_index%2C%20Normalization.MIN_MAX)%0A%20%20%20%20gaps%20%3D%20np.diff(np.asarray(graph.scores)%2C%20axis%3D1)%0A%20%20%20%20shifted_gaps%20%3D%20np.diff(np.asarray(shifted.scores)%2C%20axis%3D1)%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20f%22The%20smallest%20score%20is%20%60%7Bnp.min(shifted.scores)%3A.2f%7D%60.%20The%20largest%20change%20of%20a%20gap%20between%20two%20classes%20is%22%0A%20%20%20%20%20%20%20%20f%22%20%60%7Bfloat(np.abs(gaps%20-%20shifted_gaps).max())%3A.1e%7D%60.%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%20When%20the%20options%20are%20wrong%0A%0A%20%20%20%20The%20cells%20below%20show%20the%20errors%20for%20an%20unknown%20class%2C%20for%20one%20string%20in%20place%20of%20a%20list%2C%20and%20for%20%60classes%60%20on%20a%0A%20%20%20%20model%20with%20one%20output.%20They%20send%20no%20request.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(EBMGraph%2C%20InvalidOptionError%2C%20ebm%2C%20mo%2C%20petal_index)%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20EBMGraph.from_ebm(ebm%2C%20petal_index%2C%20classes%3D%5B%22setosa%22%2C%20%22rose%22%5D)%0A%20%20%20%20%20%20%20%20unknown_class%20%3D%20mo.md(%22No%20error.%22)%0A%20%20%20%20except%20InvalidOptionError%20as%20unknown%3A%0A%20%20%20%20%20%20%20%20unknown_class%20%3D%20mo.callout(mo.md(f%22%60InvalidOptionError%60%3A%20%7Bunknown%7D%22)%2C%20kind%3D%22danger%22)%0A%20%20%20%20unknown_class%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(EBMGraph%2C%20InvalidOptionError%2C%20ebm%2C%20mo%2C%20petal_index)%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20EBMGraph.from_ebm(ebm%2C%20petal_index%2C%20classes%3D%22setosa%22)%0A%20%20%20%20%20%20%20%20string_classes%20%3D%20mo.md(%22No%20error.%22)%0A%20%20%20%20except%20InvalidOptionError%20as%20one_string%3A%0A%20%20%20%20%20%20%20%20string_classes%20%3D%20mo.callout(mo.md(f%22%60InvalidOptionError%60%3A%20%7Bone_string%7D%22)%2C%20kind%3D%22danger%22)%0A%20%20%20%20string_classes%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20EBMGraph%2C%0A%20%20%20%20ExplainableBoostingClassifier%2C%0A%20%20%20%20UnsupportedModelError%2C%0A%20%20%20%20features%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20petal_index%2C%0A%20%20%20%20species%2C%0A)%3A%0A%20%20%20%20binary%20%3D%20ExplainableBoostingClassifier(feature_names%3Dfeatures.columns%2C%20interactions%3D0%2C%20random_state%3D42).fit(%0A%20%20%20%20%20%20%20%20features%2C%0A%20%20%20%20%20%20%20%20species%20%3D%3D%20%22setosa%22%2C%0A%20%20%20%20)%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20EBMGraph.from_ebm(binary%2C%20petal_index%2C%20classes%3D%5B%22True%22%5D)%0A%20%20%20%20%20%20%20%20binary_classes%20%3D%20mo.md(%22No%20error.%22)%0A%20%20%20%20except%20UnsupportedModelError%20as%20one_output%3A%0A%20%20%20%20%20%20%20%20binary_classes%20%3D%20mo.callout(mo.md(f%22%60UnsupportedModelError%60%3A%20%7Bone_output%7D%22)%2C%20kind%3D%22danger%22)%0A%20%20%20%20binary_classes%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%20Put%20it%20in%20words%0A%0A%20%20%20%20Each%20button%20sends%20real%20requests.%20%60describe_graph%60%20makes%20three%20requests%20for%20one%20graph.%20%60describe_ebm%60%20makes%20three%0A%20%20%20%20for%20each%20feature%20and%20two%20for%20the%20summary.%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%20graph_button%20%3D%20mo.ui.run_button(label%3D%22Describe%20the%20petal%20length%20graph%22)%0A%20%20%20%20model_button%20%3D%20mo.ui.run_button(label%3D%22Describe%20the%20whole%20model%22)%0A%20%20%20%20mo.hstack(%5Bgraph_button%2C%20model_button%5D%2C%20justify%3D%22start%22)%0A%20%20%20%20return%20graph_button%2C%20model_button%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20DescribeGraphRequest%2C%0A%20%20%20%20GraphOptions%2C%0A%20%20%20%20describe_graph%2C%0A%20%20%20%20ebm%2C%0A%20%20%20%20graph_button%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20petal_index%2C%0A)%3A%0A%20%20%20%20mo.stop(not%20graph_button.value%2C%20mo.md(%22Press%20the%20first%20button%20to%20describe%20the%20graph.%22))%0A%20%20%20%20with%20mo.status.spinner(%22Asking%20the%20LLM%20(three%20turns)...%22)%3A%0A%20%20%20%20%20%20%20%20graph_description%20%3D%20describe_graph(%0A%20%20%20%20%20%20%20%20%20%20%20%20DescribeGraphRequest(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ebm%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20petal_index%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20num_sentences%3D4%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20graph%3DGraphOptions(classes%3D%5B%22setosa%22%2C%20%22virginica%22%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20mo.md(graph_description)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(DescribeEBMRequest%2C%20describe_ebm%2C%20ebm%2C%20mo%2C%20model_button)%3A%0A%20%20%20%20mo.stop(not%20model_button.value%2C%20mo.md(%22Press%20the%20second%20button%20to%20describe%20the%20model.%22))%0A%20%20%20%20with%20mo.status.spinner(%22Asking%20the%20LLM%20(three%20turns%20per%20feature%2C%20then%20the%20summary)...%22)%3A%0A%20%20%20%20%20%20%20%20model_description%20%3D%20describe_ebm(DescribeEBMRequest(ebm%2C%20num_sentences%3D8)).text%0A%20%20%20%20mo.md(model_description)%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
47e46a9f9b911f2f3799f3c6833e27c9