%22%22%22A%20marimo%20notebook%20that%20talks%20to%20an%20EBM%20about%20the%20Titanic%2C%20like%20TalkToEBM's%20example%20notebook.%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%20os%0A%0A%20%20%20%20import%20marimo%20as%20mo%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%20fetch_openml%0A%20%20%20%20from%20sklearn.model_selection%20import%20train_test_split%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%20PromptOptions%2C%0A%20%20%20%20%20%20%20%20check_token_budget%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)%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%20PromptOptions%2C%0A%20%20%20%20%20%20%20%20check_token_budget%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%20fetch_openml%2C%0A%20%20%20%20%20%20%20%20mo%2C%0A%20%20%20%20%20%20%20%20os%2C%0A%20%20%20%20%20%20%20%20pl%2C%0A%20%20%20%20%20%20%20%20train_test_split%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%20Talk%20to%20an%20EBM%20about%20the%20Titanic%0A%0A%20%20%20%20It%20follows%20TalkToEBM's%20*Spaceship%20Titanic*%20notebook.%20It%20uses%20the%20classic%0A%20%20%20%20%5BTitanic%20dataset%5D(https%3A%2F%2Fwww.openml.org%2Fd%2F40945)%20from%20OpenML%2C%20which%20downloads%20without%20an%20account%2C%20instead%20of%20the%0A%20%20%20%20Kaggle%20one.%0A%0A%20%20%20%20The%20LLM%20cells%20call%20%5BOpenRouter%5D(https%3A%2F%2Fopenrouter.ai)%20through%20BAML%20and%20only%20run%20when%20you%20press%20their%20button.%20The%0A%20%20%20%20default%20model%20needs%20an%20OpenRouter%20API%20key%3A%20%60OPENROUTER_API_KEY%60%20from%20the%20environment%20that%20started%20the%20notebook%20(see%0A%20%20%20%20the%20README)%2C%20or%20the%20password%20field%20below%20when%20it%20is%20not%20set.%20To%20use%20another%20model%2C%20pass%20%60model%3D%60%20to%20the%20functions.%0A%20%20%20%20The%20documentation%20page%20%22Choosing%20the%20model%22%20lists%20the%20providers.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20os)%3A%0A%20%20%20%20key_from_env%20%3D%20bool(os.environ.get(%22OPENROUTER_API_KEY%22))%0A%20%20%20%20api_key_input%20%3D%20None%20if%20key_from_env%20else%20mo.ui.text(kind%3D%22password%22%2C%20label%3D%22OpenRouter%20API%20key%22%2C%20full_width%3DTrue)%0A%20%20%20%20key_status%20%3D%20(%0A%20%20%20%20%20%20%20%20mo.callout(%22Loaded%20%60OPENROUTER_API_KEY%60%20from%20the%20environment.%22%2C%20kind%3D%22success%22)%0A%20%20%20%20%20%20%20%20if%20key_from_env%0A%20%20%20%20%20%20%20%20else%20mo.vstack(%5Bmo.callout(%22%60OPENROUTER_API_KEY%60%20is%20not%20set%20in%20the%20environment.%22%2C%20kind%3D%22warn%22)%2C%20api_key_input%5D)%0A%20%20%20%20)%0A%20%20%20%20key_status%0A%20%20%20%20return%20(api_key_input%2C)%0A%0A%0A%40app.cell%0Adef%20_(api_key_input%2C%20os)%3A%0A%20%20%20%20%23%20a%20typed%20key%20only%20lives%20in%20this%20process%3B%20the%20BAML%20clients%20read%20the%20variable%20when%20they%20send%20a%20request%0A%20%20%20%20if%20api_key_input%20is%20not%20None%20and%20api_key_input.value%3A%0A%20%20%20%20%20%20%20%20os.environ%5B%22OPENROUTER_API_KEY%22%5D%20%3D%20api_key_input.value%0A%20%20%20%20has_api_key%20%3D%20bool(os.environ.get(%22OPENROUTER_API_KEY%22))%0A%20%20%20%20return%20(has_api_key%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%20Load%20the%20data%20and%20train%20an%20EBM%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(fetch_openml%2C%20pl)%3A%0A%20%20%20%20%23%20The%20liac-arff%20parser%20reads%20the%20categories%20as%20text.%20The%20pandas%20parser%20of%20scikit-learn%201.9%20fails%20on%20pandas%203.1.%0A%20%20%20%20titanic%20%3D%20pl.from_pandas(fetch_openml(%22titanic%22%2C%20version%3D1%2C%20as_frame%3DTrue%2C%20parser%3D%22liac-arff%22).frame)%0A%20%20%20%20%23%20%60boat%60%20and%20%60body%60%20only%20exist%20for%20the%20survivors%20and%20the%20dead%2C%20so%20they%20would%20leak%20the%20label%3B%20the%20rest%20of%20the%0A%20%20%20%20%23%20dropped%20columns%20are%20free%20text%20or%20have%20too%20many%20distinct%20values%20to%20fit%20in%20the%20context%20window%20of%20an%20LLM%0A%20%20%20%20features%20%3D%20titanic.select(%22pclass%22%2C%20%22sex%22%2C%20%22age%22%2C%20%22sibsp%22%2C%20%22parch%22%2C%20%22fare%22%2C%20%22embarked%22).with_columns(%0A%20%20%20%20%20%20%20%20pl.col(%22sex%22%2C%20%22embarked%22).cast(pl.String)%2C%0A%20%20%20%20)%0A%20%20%20%20survived%20%3D%20titanic%5B%22survived%22%5D.cast(pl.String).cast(pl.Int8)%0A%20%20%20%20return%20features%2C%20survived%0A%0A%0A%40app.cell%0Adef%20_(ExplainableBoostingClassifier%2C%20features%2C%20survived%2C%20train_test_split)%3A%0A%20%20%20%20x_train%2C%20x_test%2C%20y_train%2C%20y_test%20%3D%20train_test_split(features%2C%20survived%2C%20test_size%3D0.2%2C%20random_state%3D42)%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)%0A%20%20%20%20ebm.fit(x_train%2C%20y_train)%0A%20%20%20%20return%20ebm%2C%20x_test%2C%20y_test%0A%0A%0A%40app.cell%0Adef%20_(ebm%2C%20mo%2C%20x_test%2C%20y_test)%3A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20Test%20accuracy%3A%20**%7Bebm.score(x_test%2C%20y_test)%3A.3f%7D**.%20The%20features%2C%20in%20the%20order%20of%20%60feature_index%60%3A%0A%20%20%20%20%7B%22%2C%20%22.join(f%22%60%7Bi%7D%60%20%7Bname%7D%22%20for%20i%2C%20name%20in%20enumerate(ebm.feature_names_in_))%7D.%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%20High-level%20API%3A%20describe%20a%20graph%0A%0A%20%20%20%20%60describe_graph%60%20extracts%20the%20graph%20of%20one%20feature%2C%20turns%20it%20into%20text%2C%20and%20asks%20the%20LLM%20three%20questions%20in%20a%20row%0A%20%20%20%20(a%20first%20description%2C%20the%20surprising%20regions%2C%20a%20final%20summary).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ebm%2C%20mo)%3A%0A%20%20%20%20feature%20%3D%20mo.ui.dropdown(%0A%20%20%20%20%20%20%20%20options%3D%7Bname%3A%20i%20for%20i%2C%20name%20in%20enumerate(ebm.feature_names_in_)%7D%2C%0A%20%20%20%20%20%20%20%20value%3D%22age%22%2C%0A%20%20%20%20%20%20%20%20label%3D%22Feature%22%2C%0A%20%20%20%20)%0A%20%20%20%20with_context%20%3D%20mo.ui.checkbox(label%3D%22Tell%20the%20LLM%20about%20the%20dataset%20and%20the%20y-axis%22)%0A%20%20%20%20describe_graph_button%20%3D%20mo.ui.run_button(label%3D%22Describe%20the%20graph%22)%0A%20%20%20%20mo.hstack(%5Bfeature%2C%20with_context%2C%20describe_graph_button%5D%2C%20justify%3D%22start%22)%0A%20%20%20%20return%20describe_graph_button%2C%20feature%2C%20with_context%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20context%20for%20the%20LLM%3A%20what%20the%20data%20is%2C%20and%20what%20the%20y-axis%20of%20the%20graphs%20means%0A%20%20%20%20dataset_description%20%3D%20%22%22%22The%20RMS%20Titanic%20sank%20in%20the%20North%20Atlantic%20in%20April%201912%2C%20after%20hitting%20an%20iceberg.%0A%20%20%20%20Each%20row%20is%20a%20passenger%2C%20and%20the%20outcome%20is%20whether%20they%20survived.%0A%0A%20%20%20%20Features%3A%0A%20%20%20%20-%20pclass%20-%20Ticket%20class%3A%201%20(first)%2C%202%20(second)%20or%203%20(third).%0A%20%20%20%20-%20sex%20-%20The%20sex%20of%20the%20passenger.%0A%20%20%20%20-%20age%20-%20The%20age%20in%20years.%20Missing%20for%20some%20passengers.%0A%20%20%20%20-%20sibsp%20-%20The%20number%20of%20siblings%20and%20spouses%20aboard.%0A%20%20%20%20-%20parch%20-%20The%20number%20of%20parents%20and%20children%20aboard.%0A%20%20%20%20-%20fare%20-%20The%20price%20of%20the%20ticket%2C%20in%201912%20pounds.%0A%20%20%20%20-%20embarked%20-%20The%20port%20of%20embarkation%3A%20C%20(Cherbourg)%2C%20Q%20(Queenstown)%20or%20S%20(Southampton).%22%22%22%0A%20%20%20%20graph_description%20%3D%20%22The%20y-axis%20of%20the%20graph%20shows%20contributions%20in%20log-odds%20towards%20survival.%22%0A%20%20%20%20return%20dataset_description%2C%20graph_description%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20DescribeGraphRequest%2C%0A%20%20%20%20PromptOptions%2C%0A%20%20%20%20dataset_description%2C%0A%20%20%20%20describe_graph%2C%0A%20%20%20%20describe_graph_button%2C%0A%20%20%20%20ebm%2C%0A%20%20%20%20feature%2C%0A%20%20%20%20graph_description%2C%0A%20%20%20%20has_api_key%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20with_context%2C%0A)%3A%0A%20%20%20%20mo.stop(not%20describe_graph_button.value%2C%20mo.md(%22Press%20the%20button%20to%20ask%20the%20LLM.%22))%0A%20%20%20%20mo.stop(not%20has_api_key%2C%20mo.callout(%22Enter%20your%20OpenRouter%20API%20key%20above%20first.%22%2C%20kind%3D%22warn%22))%0A%20%20%20%20context%20%3D%20PromptOptions(dataset_description%3Ddataset_description%2C%20graph_description%3Dgraph_description)%0A%20%20%20%20with%20mo.status.spinner(%22Asking%20the%20LLM%20(three%20turns)...%22)%3A%0A%20%20%20%20%20%20%20%20description%20%3D%20describe_graph(%0A%20%20%20%20%20%20%20%20%20%20%20%20DescribeGraphRequest(ebm%2C%20feature.value%2C%20prompt%3Dcontext%20if%20with_context.value%20else%20PromptOptions())%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20mo.md(description)%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%20High-level%20API%3A%20describe%20the%20whole%20model%0A%0A%20%20%20%20%60describe_ebm%60%20describes%20every%20feature%20graph%20(concurrently)%2C%20then%20asks%20the%20LLM%20to%20summarize%20the%20model%2C%20with%20the%0A%20%20%20%20feature%20importances.%20The%20free%20models%20are%20rate%20limited%2C%20so%20this%20lowers%20the%20concurrency.%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%20describe_ebm_button%20%3D%20mo.ui.run_button(label%3D%22Describe%20the%20model%22)%0A%20%20%20%20describe_ebm_button%0A%20%20%20%20return%20(describe_ebm_button%2C)%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20DescribeEBMRequest%2C%0A%20%20%20%20PromptOptions%2C%0A%20%20%20%20dataset_description%2C%0A%20%20%20%20describe_ebm%2C%0A%20%20%20%20describe_ebm_button%2C%0A%20%20%20%20ebm%2C%0A%20%20%20%20graph_description%2C%0A%20%20%20%20has_api_key%2C%0A%20%20%20%20mo%2C%0A)%3A%0A%20%20%20%20mo.stop(not%20describe_ebm_button.value%2C%20mo.md(%22Press%20the%20button%20to%20ask%20the%20LLM.%22))%0A%20%20%20%20mo.stop(not%20has_api_key%2C%20mo.callout(%22Enter%20your%20OpenRouter%20API%20key%20above%20first.%22%2C%20kind%3D%22warn%22))%0A%20%20%20%20with%20mo.status.spinner(%22Asking%20the%20LLM%20(one%20chain%20per%20feature%2C%20then%20a%20summary)...%22)%3A%0A%20%20%20%20%20%20%20%20summary%20%3D%20describe_ebm(%0A%20%20%20%20%20%20%20%20%20%20%20%20DescribeEBMRequest(%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%20max_concurrency%3D2%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20prompt%3DPromptOptions(dataset_description%3Ddataset_description%2C%20graph_description%3Dgraph_description)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20).text%0A%20%20%20%20mo.md(summary)%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%20Low-level%20API%3A%20graphs%20and%20text%0A%0A%20%20%20%20The%20prompts%20are%20built%20from%20%60EBMGraph%60s.%20This%20part%20makes%20no%20LLM%20calls.%0A%0A%20%20%20%20%60EBMGraph.from_ebm%60%20extracts%20the%20graph%20of%20a%20feature%20from%20the%20EBM%3A%20the%20bins%20of%20the%20x-axis%2C%20the%20scores%20and%20their%0A%20%20%20%20standard%20deviations.%20%60simplify%60%20merges%20neighbouring%20bins%20whose%20scores%20differ%20by%20less%20than%20a%20fraction%20of%20the%20range%0A%20%20%20%20of%20the%20graph%2C%20which%20makes%20the%20text%20shorter.%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)%3A%0A%20%20%20%20age%20%3D%20ebm.feature_names_in_.index(%22age%22)%0A%20%20%20%20graph%20%3D%20EBMGraph.from_ebm(ebm%2C%20age)%0A%20%20%20%20simplified%20%3D%20graph.simplify(min_variation_per_cent%3D0.05)%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20f%22The%20graph%20of%20%60age%60%20has%20**%7Blen(graph.x_vals)%7D**%20bins%2C%20and%20**%7Blen(simplified.x_vals)%7D**%20after%20simplifying%20it.%22%2C%0A%20%20%20%20)%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%60to_text%60%20writes%20the%20graph%20as%20the%20LLM%20reads%20it.%20There%20is%20no%20tokenizer%20before%20the%20call%2C%20so%20the%20size%20is%20only%0A%20%20%20%20estimated%20by%20the%20%60check_budget%60%20function%2C%20here%20the%20BAML%20one.%20%60to_text%60%20simplifies%20the%20graph%20until%20the%20estimate%20is%0A%20%20%20%20within%20%60overbudget_percent%60%20(50%25%20by%20default)%20of%20%60max_tokens%60%2C%20and%20warns%20if%20it%20is%20still%20above%20%60max_tokens%60.%20The%0A%20%20%20%20provider's%20own%20error%20is%20the%20real%20limit.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(check_token_budget%2C%20graph%2C%20mo)%3A%0A%20%20%20%20mo.md(f%22%60%60%60%5Cn%7Bgraph.to_text(max_tokens%3D500%2C%20check_budget%3Dcheck_token_budget)%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%60from_text%60%20is%20the%20inverse%3A%20it%20reads%20a%20graph%20back%20from%20its%20text%20(or%20from%20a%20prompt%20that%20contains%20it).%20The%20standard%0A%20%20%20%20deviations%20are%20estimated%20from%20the%20confidence%20bounds%2C%20so%20they%20are%20only%20as%20precise%20as%20the%20rounding%20of%20the%20text.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(EBMGraph%2C%20graph%2C%20mo)%3A%0A%20%20%20%20parsed%20%3D%20EBMGraph.from_text(graph.to_text())%0A%20%20%20%20largest_error%20%3D%20max(abs(a%20-%20b)%20for%20a%2C%20b%20in%20zip(parsed.scores%2C%20graph.scores%2C%20strict%3DTrue))%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20f%22Parsed%20back%20**%7Blen(parsed.x_vals)%7D**%20bins%20of%20%60%7Bparsed.feature_name%7D%60.%20%22%0A%20%20%20%20%20%20%20%20f%22The%20largest%20difference%20of%20a%20score%20is%20**%7Blargest_error%3A.4f%7D**.%22%2C%0A%20%20%20%20)%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
d0e4369ad95c1198842a9174d179dd84