%22%22%22A%20marimo%20notebook%20that%20compares%20the%20three%20kinds%20of%20EBM%3A%20binary%20classifier%2C%20multiclass%20classifier%20and%20regressor.%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%20time%0A%0A%20%20%20%20import%20attrs%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%2C%20ExplainableBoostingRegressor%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%20DescribeGraphRequest%2C%0A%20%20%20%20%20%20%20%20EBMGraph%2C%0A%20%20%20%20%20%20%20%20GreedyCover%2C%0A%20%20%20%20%20%20%20%20LocalOptions%2C%0A%20%20%20%20%20%20%20%20LocalSearchCover%2C%0A%20%20%20%20%20%20%20%20MipCover%2C%0A%20%20%20%20%20%20%20%20OptionRangeError%2C%0A%20%20%20%20%20%20%20%20OrToolsBackend%2C%0A%20%20%20%20%20%20%20%20OrToolsSolver%2C%0A%20%20%20%20%20%20%20%20Stop%2C%0A%20%20%20%20%20%20%20%20compute_local_facts%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%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%20ExplainableBoostingRegressor%2C%0A%20%20%20%20%20%20%20%20GreedyCover%2C%0A%20%20%20%20%20%20%20%20LocalOptions%2C%0A%20%20%20%20%20%20%20%20LocalSearchCover%2C%0A%20%20%20%20%20%20%20%20MipCover%2C%0A%20%20%20%20%20%20%20%20OptionRangeError%2C%0A%20%20%20%20%20%20%20%20OrToolsBackend%2C%0A%20%20%20%20%20%20%20%20OrToolsSolver%2C%0A%20%20%20%20%20%20%20%20Stop%2C%0A%20%20%20%20%20%20%20%20attrs%2C%0A%20%20%20%20%20%20%20%20compute_local_facts%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%20%20%20%20time%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%20Model%20kinds%0A%0A%20%20%20%20This%20notebook%20puts%20the%20three%20kinds%20of%20EBM%20side%20by%20side%3A%20a%20binary%20classifier%2C%20a%20multiclass%20classifier%20and%20a%0A%20%20%20%20regressor.%20You%20learn%20how%20EBaMl%20reads%20the%20kind%20of%20a%20model%2C%20what%20a%20score%20means%20for%20each%20kind%2C%20how%20the%20graph%20text%0A%20%20%20%20differs%2C%20and%20which%20fields%20the%20local%20explanations%20give.%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%20Three%20EBMs%20on%20the%20same%20features%0A%0A%20%20%20%20The%20three%20models%20read%20the%20same%20three%20features%20of%20the%20iris%20data.%20Only%20the%20target%20changes%3A%0A%0A%20%20%20%20%7C%20Model%20%7C%20Target%20%7C%20Kind%20%7C%0A%20%20%20%20%7C%20---%20%7C%20---%20%7C%20---%20%7C%0A%20%20%20%20%7C%20%60binary%60%20%7C%20Is%20the%20flower%20a%20virginica%3F%20%7C%20Binary%20classifier%20%7C%0A%20%20%20%20%7C%20%60multiclass%60%20%7C%20The%20species%20of%20the%20flower%20%7C%20Multiclass%20classifier%20%7C%0A%20%20%20%20%7C%20%60regressor%60%20%7C%20The%20petal%20width%2C%20in%20centimetres%20%7C%20Regressor%20%7C%0A%0A%20%20%20%20%60model_kind%60%20reads%20the%20kind%20of%20each%20fitted%20EBM%20from%20its%20link%20function.%20Each%20kind%20gives%20the%20names%20of%20the%20outputs%0A%20%20%20%20and%20the%20meaning%20of%20a%20score.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20ExplainableBoostingClassifier%2C%0A%20%20%20%20ExplainableBoostingRegressor%2C%0A%20%20%20%20load_iris%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20model_kind%2C%0A%20%20%20%20pl%2C%0A)%3A%0A%20%20%20%20iris%20%3D%20load_iris(as_frame%3DTrue)%0A%20%20%20%20measures%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%20features%20%3D%20measures.drop(%22petal_width%22)%0A%20%20%20%20%23%20no%20pairwise%20interactions%3A%20the%20graphs%20and%20the%20local%20explanations%20read%20one%20effect%20per%20feature%0A%20%20%20%20models%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22binary%22%3A%20ExplainableBoostingClassifier(feature_names%3Dfeatures.columns%2C%20interactions%3D0%2C%20random_state%3D42).fit(%0A%20%20%20%20%20%20%20%20%20%20%20%20features%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20species%20%3D%3D%20%22virginica%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%22multiclass%22%3A%20ExplainableBoostingClassifier(%0A%20%20%20%20%20%20%20%20%20%20%20%20feature_names%3Dfeatures.columns%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20interactions%3D0%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20random_state%3D42%2C%0A%20%20%20%20%20%20%20%20).fit(features%2C%20species)%2C%0A%20%20%20%20%20%20%20%20%22regressor%22%3A%20ExplainableBoostingRegressor(feature_names%3Dfeatures.columns%2C%20interactions%3D0%2C%20random_state%3D42).fit(%0A%20%20%20%20%20%20%20%20%20%20%20%20features%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20measures%5B%22petal_width%22%5D%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%7D%0A%20%20%20%20kinds%20%3D%20%7Bname%3A%20model_kind(ebm)%20for%20name%2C%20ebm%20in%20models.items()%7D%0A%20%20%20%20petal_index%20%3D%20list(features.columns).index(%22petal_length%22)%0A%20%20%20%20mo.ui.table(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22model%22%3A%20name%2C%20%22kind%22%3A%20repr(kind)%2C%20%22outputs%22%3A%20%22%2C%20%22.join(kind.outputs)%2C%20%22score%22%3A%20kind.score_meaning%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20name%2C%20kind%20in%20kinds.items()%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20selection%3DNone%2C%0A%20%20%20%20)%0A%20%20%20%20return%20features%2C%20kinds%2C%20models%2C%20petal_index%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%20From%20scores%20to%20the%20output%0A%0A%20%20%20%20An%20EBM%20adds%20the%20intercept%20and%20one%20score%20per%20feature.%20%60ModelKind.predict%60%20converts%20that%20sum%20to%20the%20output%20of%20the%0A%20%20%20%20model%3A%20the%20sigmoid%20for%20a%20binary%20classifier%2C%20softmax%20for%20a%20multiclass%20classifier%2C%20and%20no%20change%20for%20a%20regressor.%20The%0A%20%20%20%20cell%20sums%20the%20scores%20of%20the%20graphs%20for%20the%20first%20five%20flowers%20and%20compares%20the%20result%20with%20the%20EBM.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(EBMGraph%2C%20features%2C%20kinds%2C%20mo%2C%20models%2C%20np)%3A%0A%20%20%20%20first%20%3D%20features.head(5)%0A%20%20%20%20%23%20the%20output%20of%20each%20EBM%2C%20as%20its%20kind%20defines%20it%3A%20one%20column%20per%20output%0A%20%20%20%20model_outputs%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22binary%22%3A%20models%5B%22binary%22%5D.predict_proba(first)%5B%3A%2C%201%3A%5D%2C%0A%20%20%20%20%20%20%20%20%22multiclass%22%3A%20models%5B%22multiclass%22%5D.predict_proba(first)%2C%0A%20%20%20%20%20%20%20%20%22regressor%22%3A%20models%5B%22regressor%22%5D.predict(first).reshape(-1%2C%201)%2C%0A%20%20%20%20%7D%0A%20%20%20%20differences%20%3D%20%5B%5D%0A%20%20%20%20for%20model_name%2C%20model%20in%20models.items()%3A%0A%20%20%20%20%20%20%20%20total%20%3D%20np.asarray(model.intercept_%2C%20dtype%3Dfloat)%20%2B%20np.zeros((len(first)%2C%20len(kinds%5Bmodel_name%5D.outputs)))%0A%20%20%20%20%20%20%20%20for%20feature_index%2C%20_%20in%20enumerate(features.columns)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20graph_scores%20%3D%20np.asarray(EBMGraph.from_ebm(model%2C%20feature_index%2C%20use_feature_bounds%3DFalse).scores)%0A%20%20%20%20%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%20%20%20%20%20cuts%20%3D%20np.asarray(model.bins_%5Bfeature_index%5D%5B0%5D%2C%20dtype%3Dfloat)%0A%20%20%20%20%20%20%20%20%20%20%20%20bins%20%3D%20np.searchsorted(cuts%2C%20first%5B%3A%2C%20feature_index%5D.to_numpy()%2C%20side%3D%22right%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20total%20%2B%3D%20graph_scores.reshape(len(graph_scores)%2C%20-1)%5Bbins%5D%0A%20%20%20%20%20%20%20%20difference%20%3D%20float(np.abs(kinds%5Bmodel_name%5D.predict(total)%20-%20model_outputs%5Bmodel_name%5D).max())%0A%20%20%20%20%20%20%20%20differences.append(%7B%22model%22%3A%20model_name%2C%20%22largest%20difference%22%3A%20f%22%7Bdifference%3A.1e%7D%22%7D)%0A%20%20%20%20mo.ui.table(differences%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%20The%20graph%20of%20each%20kind%0A%0A%20%20%20%20The%20tabs%20show%20the%20text%20of%20the%20petal%20length%20graph%20for%20each%20model.%20A%20binary%20classifier%20and%20a%20regressor%20have%20one%0A%20%20%20%20series%20of%20means.%20A%20multiclass%20classifier%20has%20one%20series%20for%20each%20class%2C%20after%20a%20%60Class%3A%60%20line%2C%20and%20the%20text%20says%0A%20%20%20%20how%20to%20read%20it.%20The%20scores%20of%20a%20binary%20classifier%20are%20log-odds%2C%20and%20the%20scores%20of%20the%20regressor%20are%20centimetres.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(EBMGraph%2C%20mo%2C%20models%2C%20petal_index)%3A%0A%20%20%20%20mo.ui.tabs(%7B%0A%20%20%20%20%20%20%20%20name%3A%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%60%60%60%5Cn%7BEBMGraph.from_ebm(ebm%2C%20petal_index).to_text(y_axis_precision%3D2%2C%20confidence_bounds%3DFalse)%7D%5Cn%60%60%60%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20for%20name%2C%20ebm%20in%20models.items()%0A%20%20%20%20%7D)%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%20Local%20explanations%20of%20each%20kind%0A%0A%20%20%20%20The%20result%20types%20of%20the%20local%20explanations%20have%20field%20names%20that%20fit%20every%20kind.%20Pick%20a%20kind%20below%20to%20see%20the%0A%20%20%20%20fields%20for%20that%20kind.%20The%20facts%20come%20from%20Python%20and%20send%20no%20request.%0A%0A%20%20%20%20-%20%60Prediction.decision%60%20is%20the%20predicted%20class.%20For%20a%20regressor%2C%20it%20is%20the%20answer%20to%20a%20question%2C%20such%20as%20%22above%201%22.%0A%20%20%20%20-%20%60Prediction.output%60%20is%20the%20probability%20of%20that%20class.%20For%20a%20regressor%2C%20it%20is%20the%20predicted%20value.%0A%20%20%20%20-%20%60Prediction.margin%60%20is%20positive%20while%20the%20decision%20holds.%20It%20is%20the%20log-odds%20of%20the%20predicted%20class%20for%20a%20binary%0A%20%20%20%20%20%20classifier%2C%20the%20smallest%20score%20gap%20to%20another%20class%20for%20a%20multiclass%20classifier%2C%20and%20the%20distance%20to%20the%20threshold%0A%20%20%20%20%20%20for%20a%20regressor.%0A%20%20%20%20-%20%60Counterfactual.new_decision%60%20is%20the%20class%20that%20is%20predicted%20after%20the%20changes.%20For%20a%20regressor%2C%20it%20is%20the%20other%0A%20%20%20%20%20%20answer.%0A%20%20%20%20-%20%60SufficientReason.minimality%60%20tells%20why%20a%20set%20of%20features%20is%2C%20or%20is%20not%2C%20known%20to%20be%20the%20smallest%20one%20that%20fixes%0A%20%20%20%20%20%20the%20decision.%20It%20is%20always%20%60SORTING_PROOF%60%20for%20a%20binary%20classifier.%20For%20a%20decision%20with%20several%20margins%2C%20it%0A%20%20%20%20%20%20depends%20on%20the%20solver%20that%20%60LocalOptions.search%60%20names.%20%60SufficientReason.lower_bound%60%20gives%20the%20fewest%20features%0A%20%20%20%20%20%20that%20any%20set%20needs.%0A%0A%20%20%20%20A%20multiclass%20classifier%20has%20one%20margin%20for%20each%20other%20class%3A%20the%20score%20of%20the%20predicted%20class%20minus%20the%20score%20of%0A%20%20%20%20that%20class.%20The%20option%20%60target_class%60%20asks%20for%20the%20fewest%20changes%20that%20lead%20to%20a%20chosen%20class%2C%20over%20every%20choice%20of%0A%20%20%20%20bins.%20The%20next%20section%20compares%20the%20solvers.%0A%0A%20%20%20%20The%20iris%20data%20has%20only%20150%20flowers%2C%20so%20the%20confidence%20intervals%20of%20the%20scores%20are%20wide.%20At%20a%20high%20confidence%20level%2C%0A%20%20%20%20most%20flowers%20have%20no%20counterfactual%20and%20no%20sufficient%20reason.%20Flower%2070%20is%20a%20versicolor%20near%20the%20border%20with%20the%0A%20%20%20%20virginica.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(features%2C%20mo%2C%20models)%3A%0A%20%20%20%20kind_choice%20%3D%20mo.ui.dropdown(list(models)%2C%20value%3D%22binary%22%2C%20label%3D%22Model%22)%0A%20%20%20%20flower%20%3D%20mo.ui.slider(0%2C%20len(features)%20-%201%2C%20value%3D70%2C%20label%3D%22Flower%22%2C%20show_value%3DTrue)%0A%20%20%20%20confidence%20%3D%20mo.ui.slider(0.0%2C%200.99%2C%20step%3D0.01%2C%20value%3D0.0%2C%20label%3D%22Confidence%20level%22%2C%20show_value%3DTrue)%0A%20%20%20%20target%20%3D%20mo.ui.dropdown(%0A%20%20%20%20%20%20%20%20%5B%22no%20target%22%2C%20*models%5B%22multiclass%22%5D.classes_%5D%2C%0A%20%20%20%20%20%20%20%20value%3D%22no%20target%22%2C%0A%20%20%20%20%20%20%20%20label%3D%22Target%20class%20(multiclass%20only)%22%2C%0A%20%20%20%20)%0A%20%20%20%20mo.vstack(%5Bkind_choice%2C%20flower%2C%20confidence%2C%20target%5D)%0A%20%20%20%20return%20confidence%2C%20flower%2C%20kind_choice%2C%20target%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20LocalOptions%2C%0A%20%20%20%20OptionRangeError%2C%0A%20%20%20%20compute_local_facts%2C%0A%20%20%20%20confidence%2C%0A%20%20%20%20features%2C%0A%20%20%20%20flower%2C%0A%20%20%20%20kind_choice%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20models%2C%0A%20%20%20%20target%2C%0A)%3A%0A%20%20%20%20instance%20%3D%20features.row(flower.value%2C%20named%3DTrue)%0A%20%20%20%20chosen_target%20%3D%20target.value%20if%20kind_choice.value%20%3D%3D%20%22multiclass%22%20and%20target.value%20!%3D%20%22no%20target%22%20else%20None%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20facts%20%3D%20compute_local_facts(%0A%20%20%20%20%20%20%20%20%20%20%20%20models%5Bkind_choice.value%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20instance%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20LocalOptions(confidence_level%3Dconfidence.value%2C%20target_class%3Dchosen_target)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20except%20OptionRangeError%20as%20option_error%3A%0A%20%20%20%20%20%20%20%20mo.stop(predicate%3DTrue%2C%20output%3Dmo.callout(mo.md(f%22%60OptionRangeError%60%3A%20%7Boption_error%7D%22)%2C%20kind%3D%22danger%22))%0A%20%20%20%20return%20facts%2C%20instance%0A%0A%0A%40app.cell%0Adef%20_(attrs%2C%20facts%2C%20instance%2C%20mo)%3A%0A%20%20%20%20prediction%20%3D%20facts.prediction%0A%20%20%20%20counterfactual%20%3D%20facts.counterfactual%0A%20%20%20%20sufficient%20%3D%20facts.sufficient_reason%0A%20%20%20%20sections%20%3D%20%5B%0A%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22The%20flower%20has%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20%22%2C%20%22.join(f%22**%7Bname%7D**%20%3D%20%7Bvalue%7D%22%20for%20name%2C%20value%20in%20instance.items())%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20f%22.%20The%20decision%20is%20**%7Bprediction.decision%7D**%20with%20output%20**%7Bprediction.output%3A.2f%7D**.%20The%20score%20is%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%20**%7Bprediction.score%3A.2f%7D**%20and%20the%20margin%20is%20**%7Bprediction.margin%3A.2f%7D**.%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20mo.ui.table(%5Battrs.asdict(prediction)%5D%2C%20selection%3DNone)%2C%0A%20%20%20%20%5D%0A%20%20%20%20if%20counterfactual%20is%20None%3A%0A%20%20%20%20%20%20%20%20sections.append(mo.md(%22**No%20counterfactual**%20at%20this%20confidence%20level.%22))%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20sections.extend(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22The%20counterfactual%20changes%20the%20decision%20to%20**%7Bcounterfactual.new_decision%7D**%2C%20with%20output%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%20**%7Bcounterfactual.new_output%3A.2f%7D**.%20%60minimality%60%20is%20**%7Bcounterfactual.minimality%7D**%3A%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.ui.table(%5Battrs.asdict(change)%20for%20change%20in%20counterfactual.changes%5D%2C%20selection%3DNone)%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%20%20%20%20if%20sufficient%20is%20None%3A%0A%20%20%20%20%20%20%20%20sections.append(mo.md(%22**No%20sufficient%20reason**%20at%20this%20confidence%20level.%22))%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20sections.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22The%20sufficient%20reason%20is%20**%7B'%2C%20'.join(sufficient.features)%7D**%2C%20with%20a%20guaranteed%20margin%20of%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%20**%7Bsufficient.guaranteed_margin%3A.2f%7D**.%20%60minimality%60%20is%20**%7Bsufficient.minimality%7D**.%22%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.vstack(sections)%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%20Choose%20the%20solver%0A%0A%20%20%20%20The%20sufficient%20reason%20of%20a%20multiclass%20classifier%20and%20the%20counterfactual%20of%20a%20target%20class%20are%20covering%20problems.%0A%20%20%20%20No%20fast%20exact%20algorithm%20solves%20them%20unless%20P%20%3D%20NP%2C%20so%20%60LocalOptions.search%60%20chooses%20the%20algorithm%3A%0A%0A%20%20%20%20-%20%60MipCover()%60%20searches%20all%20sets%20by%20branch%20and%20bound%20in%20a%20MIP%20solver%20of%20OR-Tools%3A%20HiGHS%20(the%20default)%2C%20or%20SCIP%20or%0A%20%20%20%20%20%20CP-SAT%20through%20%60OrToolsBackend(OrToolsSolver.SCIP)%60.%20%60stop%3DStop.FIRST%60%20ends%20at%20the%20first%20set%20that%20it%20finds.%0A%20%20%20%20-%20%60LocalSearchCover()%60%20removes%20a%20random%20part%20of%20the%20greedy%20set%20and%20builds%20it%20again%2C%20with%20a%20fixed%20seed.%0A%20%20%20%20-%20%60GreedyCover()%60%20adds%20the%20input%20with%20the%20largest%20gain%20until%20the%20set%20is%20enough.%0A%0A%20%20%20%20The%20last%20two%20need%20no%20MIP%20solver.%20The%20table%20runs%20each%20solver%20on%20the%20flower%20and%20the%20confidence%20level%20above%2C%20with%20the%0A%20%20%20%20target%20class%20%60setosa%60%20for%20the%20counterfactual.%20It%20shows%20the%20size%20of%20each%20set%2C%20its%20%60minimality%60%2C%20the%20lower%20bound%0A%20%20%20%20and%20the%20time.%20%60MipCover%60%20has%20a%20%60time_limit%60%20in%20seconds.%20A%20set%20is%20marked%20as%20the%20smallest%20only%20if%20the%20MIP%20solver%0A%20%20%20%20proves%20it%20or%20the%20set%20reaches%20the%20lower%20bound.%20The%20iris%20model%20has%20three%20features%2C%20so%20every%20solver%20finds%20the%20same%20sets%0A%20%20%20%20here.%20The%20solvers%20differ%20on%20models%20with%20many%20features.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20GreedyCover%2C%0A%20%20%20%20LocalOptions%2C%0A%20%20%20%20LocalSearchCover%2C%0A%20%20%20%20MipCover%2C%0A%20%20%20%20OrToolsBackend%2C%0A%20%20%20%20OrToolsSolver%2C%0A%20%20%20%20Stop%2C%0A%20%20%20%20compute_local_facts%2C%0A%20%20%20%20confidence%2C%0A%20%20%20%20instance%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20models%2C%0A%20%20%20%20time%2C%0A)%3A%0A%20%20%20%20solvers%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22MipCover()%22%3A%20MipCover()%2C%0A%20%20%20%20%20%20%20%20%22MipCover(OrToolsBackend(OrToolsSolver.SCIP))%22%3A%20MipCover(OrToolsBackend(OrToolsSolver.SCIP))%2C%0A%20%20%20%20%20%20%20%20%22MipCover(OrToolsBackend(OrToolsSolver.CP_SAT))%22%3A%20MipCover(OrToolsBackend(OrToolsSolver.CP_SAT))%2C%0A%20%20%20%20%20%20%20%20%22MipCover(stop%3DStop.FIRST)%22%3A%20MipCover(stop%3DStop.FIRST)%2C%0A%20%20%20%20%20%20%20%20%22LocalSearchCover()%22%3A%20LocalSearchCover()%2C%0A%20%20%20%20%20%20%20%20%22GreedyCover()%22%3A%20GreedyCover()%2C%0A%20%20%20%20%7D%0A%20%20%20%20search_rows%20%3D%20%5B%5D%0A%20%20%20%20for%20solver_name%2C%20solver%20in%20solvers.items()%3A%0A%20%20%20%20%20%20%20%20start%20%3D%20time.perf_counter()%0A%20%20%20%20%20%20%20%20search_facts%20%3D%20compute_local_facts(%0A%20%20%20%20%20%20%20%20%20%20%20%20models%5B%22multiclass%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20instance%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20LocalOptions(confidence_level%3Dconfidence.value%2C%20target_class%3D%22setosa%22%2C%20search%3Dsolver)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20seconds%20%3D%20time.perf_counter()%20-%20start%0A%20%20%20%20%20%20%20%20target_change%20%3D%20search_facts.counterfactual%0A%20%20%20%20%20%20%20%20search_reason%20%3D%20search_facts.sufficient_reason%0A%20%20%20%20%20%20%20%20search_rows.append(%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22solver%22%3A%20solver_name%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22changes%20to%20setosa%22%3A%20%22none%22%20if%20target_change%20is%20None%20else%20len(target_change.changes)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22changes%3A%20minimality%22%3A%20None%20if%20target_change%20is%20None%20else%20target_change.minimality.value%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22changes%3A%20lower%20bound%22%3A%20None%20if%20target_change%20is%20None%20else%20target_change.lower_bound%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22sufficient%20reason%22%3A%20%22none%22%20if%20search_reason%20is%20None%20else%20%22%2C%20%22.join(search_reason.features)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22reason%3A%20minimality%22%3A%20None%20if%20search_reason%20is%20None%20else%20search_reason.minimality.value%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22reason%3A%20lower%20bound%22%3A%20None%20if%20search_reason%20is%20None%20else%20search_reason.lower_bound%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22seconds%22%3A%20round(seconds%2C%203)%2C%0A%20%20%20%20%20%20%20%20%7D)%0A%20%20%20%20mo.ui.table(search_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%20A%20setup%20error%20and%20its%20fix%0A%0A%20%20%20%20A%20%60target_class%60%20needs%20a%20multiclass%20classifier%2C%20and%20it%20must%20be%20another%20class%20of%20the%20model.%20Each%20other%20call%20raises%0A%20%20%20%20an%20%60OptionRangeError%60%20before%20any%20LLM%20call.%20The%20message%20names%20the%20cause.%20To%20fix%20it%2C%20pass%20a%20class%20of%20the%20model%20that%0A%20%20%20%20is%20not%20the%20predicted%20class%2C%20or%20leave%20the%20option%20out.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20LocalOptions%2C%0A%20%20%20%20OptionRangeError%2C%0A%20%20%20%20compute_local_facts%2C%0A%20%20%20%20instance%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20models%2C%0A)%3A%0A%20%20%20%20attempts%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22binary%20classifier%2C%20target%20%60virginica%60%22%3A%20(models%5B%22binary%22%5D%2C%20%22virginica%22)%2C%0A%20%20%20%20%20%20%20%20%22multiclass%20classifier%2C%20target%20%60tulip%60%22%3A%20(models%5B%22multiclass%22%5D%2C%20%22tulip%22)%2C%0A%20%20%20%20%7D%0A%20%20%20%20errors%20%3D%20%5B%5D%0A%20%20%20%20for%20label%2C%20(attempted_model%2C%20target_class)%20in%20attempts.items()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20compute_local_facts(attempted_model%2C%20instance%2C%20LocalOptions(target_class%3Dtarget_class))%0A%20%20%20%20%20%20%20%20%20%20%20%20errors.append(mo.md(f%22%7Blabel%7D%3A%20no%20error.%22))%0A%20%20%20%20%20%20%20%20except%20OptionRangeError%20as%20option_error%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20errors.append(mo.callout(mo.md(f%22%7Blabel%7D%3A%20%60OptionRangeError%60%3A%20%7Boption_error%7D%22)%2C%20kind%3D%22danger%22))%0A%20%20%20%20mo.vstack(errors)%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%20the%20same%20call%2C%20%60describe_graph%60%20of%20the%20petal%20length%20graph%2C%20for%20one%20model.%20The%20prompt%20tells%20the%20LLM%0A%20%20%20%20what%20a%20score%20of%20that%20kind%20means.%20Each%20call%20makes%20three%20requests.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20models)%3A%0A%20%20%20%20buttons%20%3D%20%7Bname%3A%20mo.ui.run_button(label%3Df%22Describe%20the%20%7Bname%7D%20graph%22)%20for%20name%20in%20models%7D%0A%20%20%20%20mo.hstack(list(buttons.values())%2C%20justify%3D%22start%22)%0A%20%20%20%20return%20(buttons%2C)%0A%0A%0A%40app.cell%0Adef%20_(DescribeGraphRequest%2C%20buttons%2C%20describe_graph%2C%20mo%2C%20models%2C%20petal_index)%3A%0A%20%20%20%20pressed%20%3D%20%5Bname%20for%20name%2C%20button%20in%20buttons.items()%20if%20button.value%5D%0A%20%20%20%20mo.stop(not%20pressed%2C%20mo.md(%22Press%20a%20button%20to%20describe%20the%20graph%20of%20that%20model.%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%20descriptions%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3A%20describe_graph(DescribeGraphRequest(models%5Bname%5D%2C%20petal_index%2C%20num_sentences%3D4))%20for%20name%20in%20pressed%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20mo.vstack(%5Bmo.md(f%22%23%23%23%20%7Bname%7D%5Cn%5Cn%7Bdescription%7D%22)%20for%20name%2C%20description%20in%20descriptions.items()%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
feadf10ad8497c148f19e935372c371d