[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"similar-jphall663--interpretable_machine_learning_with_python":3,"tool-jphall663--interpretable_machine_learning_with_python":61},[4,18,26,36,44,53],{"id":5,"name":6,"github_repo":7,"description_zh":8,"stars":9,"difficulty_score":10,"last_commit_at":11,"category_tags":12,"status":17},4358,"openclaw","openclaw\u002Fopenclaw","OpenClaw 是一款专为个人打造的本地化 AI 助手，旨在让你在自己的设备上拥有完全可控的智能伙伴。它打破了传统 AI 助手局限于特定网页或应用的束缚，能够直接接入你日常使用的各类通讯渠道，包括微信、WhatsApp、Telegram、Discord、iMessage 等数十种平台。无论你在哪个聊天软件中发送消息，OpenClaw 都能即时响应，甚至支持在 macOS、iOS 和 Android 设备上进行语音交互，并提供实时的画布渲染功能供你操控。\n\n这款工具主要解决了用户对数据隐私、响应速度以及“始终在线”体验的需求。通过将 AI 部署在本地，用户无需依赖云端服务即可享受快速、私密的智能辅助，真正实现了“你的数据，你做主”。其独特的技术亮点在于强大的网关架构，将控制平面与核心助手分离，确保跨平台通信的流畅性与扩展性。\n\nOpenClaw 非常适合希望构建个性化工作流的技术爱好者、开发者，以及注重隐私保护且不愿被单一生态绑定的普通用户。只要具备基础的终端操作能力（支持 macOS、Linux 及 Windows WSL2），即可通过简单的命令行引导完成部署。如果你渴望拥有一个懂你",349277,3,"2026-04-06T06:32:30",[13,14,15,16],"Agent","开发框架","图像","数据工具","ready",{"id":19,"name":20,"github_repo":21,"description_zh":22,"stars":23,"difficulty_score":10,"last_commit_at":24,"category_tags":25,"status":17},3808,"stable-diffusion-webui","AUTOMATIC1111\u002Fstable-diffusion-webui","stable-diffusion-webui 是一个基于 Gradio 构建的网页版操作界面，旨在让用户能够轻松地在本地运行和使用强大的 Stable Diffusion 图像生成模型。它解决了原始模型依赖命令行、操作门槛高且功能分散的痛点，将复杂的 AI 绘图流程整合进一个直观易用的图形化平台。\n\n无论是希望快速上手的普通创作者、需要精细控制画面细节的设计师，还是想要深入探索模型潜力的开发者与研究人员，都能从中获益。其核心亮点在于极高的功能丰富度：不仅支持文生图、图生图、局部重绘（Inpainting）和外绘（Outpainting）等基础模式，还独创了注意力机制调整、提示词矩阵、负向提示词以及“高清修复”等高级功能。此外，它内置了 GFPGAN 和 CodeFormer 等人脸修复工具，支持多种神经网络放大算法，并允许用户通过插件系统无限扩展能力。即使是显存有限的设备，stable-diffusion-webui 也提供了相应的优化选项，让高质量的 AI 艺术创作变得触手可及。",162132,"2026-04-05T11:01:52",[14,15,13],{"id":27,"name":28,"github_repo":29,"description_zh":30,"stars":31,"difficulty_score":32,"last_commit_at":33,"category_tags":34,"status":17},1381,"everything-claude-code","affaan-m\u002Feverything-claude-code","everything-claude-code 是一套专为 AI 编程助手（如 Claude Code、Codex、Cursor 等）打造的高性能优化系统。它不仅仅是一组配置文件，而是一个经过长期实战打磨的完整框架，旨在解决 AI 代理在实际开发中面临的效率低下、记忆丢失、安全隐患及缺乏持续学习能力等核心痛点。\n\n通过引入技能模块化、直觉增强、记忆持久化机制以及内置的安全扫描功能，everything-claude-code 能显著提升 AI 在复杂任务中的表现，帮助开发者构建更稳定、更智能的生产级 AI 代理。其独特的“研究优先”开发理念和针对 Token 消耗的优化策略，使得模型响应更快、成本更低，同时有效防御潜在的攻击向量。\n\n这套工具特别适合软件开发者、AI 研究人员以及希望深度定制 AI 工作流的技术团队使用。无论您是在构建大型代码库，还是需要 AI 协助进行安全审计与自动化测试，everything-claude-code 都能提供强大的底层支持。作为一个曾荣获 Anthropic 黑客大奖的开源项目，它融合了多语言支持与丰富的实战钩子（hooks），让 AI 真正成长为懂上",145895,2,"2026-04-08T11:32:59",[14,13,35],"语言模型",{"id":37,"name":38,"github_repo":39,"description_zh":40,"stars":41,"difficulty_score":32,"last_commit_at":42,"category_tags":43,"status":17},2271,"ComfyUI","Comfy-Org\u002FComfyUI","ComfyUI 是一款功能强大且高度模块化的视觉 AI 引擎，专为设计和执行复杂的 Stable Diffusion 图像生成流程而打造。它摒弃了传统的代码编写模式，采用直观的节点式流程图界面，让用户通过连接不同的功能模块即可构建个性化的生成管线。\n\n这一设计巧妙解决了高级 AI 绘图工作流配置复杂、灵活性不足的痛点。用户无需具备编程背景，也能自由组合模型、调整参数并实时预览效果，轻松实现从基础文生图到多步骤高清修复等各类复杂任务。ComfyUI 拥有极佳的兼容性，不仅支持 Windows、macOS 和 Linux 全平台，还广泛适配 NVIDIA、AMD、Intel 及苹果 Silicon 等多种硬件架构，并率先支持 SDXL、Flux、SD3 等前沿模型。\n\n无论是希望深入探索算法潜力的研究人员和开发者，还是追求极致创作自由度的设计师与资深 AI 绘画爱好者，ComfyUI 都能提供强大的支持。其独特的模块化架构允许社区不断扩展新功能，使其成为当前最灵活、生态最丰富的开源扩散模型工具之一，帮助用户将创意高效转化为现实。",108111,"2026-04-08T11:23:26",[14,15,13],{"id":45,"name":46,"github_repo":47,"description_zh":48,"stars":49,"difficulty_score":32,"last_commit_at":50,"category_tags":51,"status":17},4721,"markitdown","microsoft\u002Fmarkitdown","MarkItDown 是一款由微软 AutoGen 团队打造的轻量级 Python 工具，专为将各类文件高效转换为 Markdown 格式而设计。它支持 PDF、Word、Excel、PPT、图片（含 OCR）、音频（含语音转录）、HTML 乃至 YouTube 链接等多种格式的解析，能够精准提取文档中的标题、列表、表格和链接等关键结构信息。\n\n在人工智能应用日益普及的今天，大语言模型（LLM）虽擅长处理文本，却难以直接读取复杂的二进制办公文档。MarkItDown 恰好解决了这一痛点，它将非结构化或半结构化的文件转化为模型“原生理解”且 Token 效率极高的 Markdown 格式，成为连接本地文件与 AI 分析 pipeline 的理想桥梁。此外，它还提供了 MCP（模型上下文协议）服务器，可无缝集成到 Claude Desktop 等 LLM 应用中。\n\n这款工具特别适合开发者、数据科学家及 AI 研究人员使用，尤其是那些需要构建文档检索增强生成（RAG）系统、进行批量文本分析或希望让 AI 助手直接“阅读”本地文件的用户。虽然生成的内容也具备一定可读性，但其核心优势在于为机器",93400,"2026-04-06T19:52:38",[52,14],"插件",{"id":54,"name":55,"github_repo":56,"description_zh":57,"stars":58,"difficulty_score":10,"last_commit_at":59,"category_tags":60,"status":17},4487,"LLMs-from-scratch","rasbt\u002FLLMs-from-scratch","LLMs-from-scratch 是一个基于 PyTorch 的开源教育项目，旨在引导用户从零开始一步步构建一个类似 ChatGPT 的大型语言模型（LLM）。它不仅是同名技术著作的官方代码库，更提供了一套完整的实践方案，涵盖模型开发、预训练及微调的全过程。\n\n该项目主要解决了大模型领域“黑盒化”的学习痛点。许多开发者虽能调用现成模型，却难以深入理解其内部架构与训练机制。通过亲手编写每一行核心代码，用户能够透彻掌握 Transformer 架构、注意力机制等关键原理，从而真正理解大模型是如何“思考”的。此外，项目还包含了加载大型预训练权重进行微调的代码，帮助用户将理论知识延伸至实际应用。\n\nLLMs-from-scratch 特别适合希望深入底层原理的 AI 开发者、研究人员以及计算机专业的学生。对于不满足于仅使用 API，而是渴望探究模型构建细节的技术人员而言，这是极佳的学习资源。其独特的技术亮点在于“循序渐进”的教学设计：将复杂的系统工程拆解为清晰的步骤，配合详细的图表与示例，让构建一个虽小但功能完备的大模型变得触手可及。无论你是想夯实理论基础，还是为未来研发更大规模的模型做准备",90106,"2026-04-06T11:19:32",[35,15,13,14],{"id":62,"github_repo":63,"name":64,"description_en":65,"description_zh":66,"ai_summary_zh":66,"readme_en":67,"readme_zh":68,"quickstart_zh":69,"use_case_zh":70,"hero_image_url":71,"owner_login":72,"owner_name":73,"owner_avatar_url":74,"owner_bio":75,"owner_company":76,"owner_location":77,"owner_email":76,"owner_twitter":76,"owner_website":78,"owner_url":79,"languages":80,"stars":93,"forks":94,"last_commit_at":95,"license":76,"difficulty_score":32,"env_os":96,"env_gpu":97,"env_ram":97,"env_deps":98,"category_tags":109,"github_topics":111,"view_count":32,"oss_zip_url":76,"oss_zip_packed_at":76,"status":17,"created_at":130,"updated_at":131,"faqs":132,"releases":163},5521,"jphall663\u002Finterpretable_machine_learning_with_python","interpretable_machine_learning_with_python","Examples of techniques for training interpretable ML models, explaining ML models, and debugging ML models for accuracy, discrimination, and security.","interpretable_machine_learning_with_python 是一套基于 Python 的开源教程集合，旨在帮助开发者构建透明、可解释且负责任的机器学习模型。随着人工智能在关键决策中的应用日益广泛，传统模型常因“黑盒”特性而难以被理解或信任，甚至可能隐含歧视与安全漏洞。这套工具通过一系列 Jupyter 笔记本示例，提供了从模型训练、原理解析到调试优化的完整解决方案，有效解决了模型缺乏可解释性、公平性验证困难以及合规风险高等痛点。\n\n它特别适合数据科学家、分析师及需要向业务方解释模型逻辑的开发人员使用。无论是希望提升模型透明度，还是需应对监管审查与道德评估，都能从中找到实用方法。其技术亮点丰富，涵盖了单调 XGBoost 模型、SHAP 值解释、LIME 局部解释、决策树代理模型以及差异影响分析等前沿技术，并支持通过灵敏度与残差分析深入排查模型缺陷。此外，项目还提供了便捷的 H2O Aquarium 环境配置方案，让用户能快速上手实践，将复杂的理论转化为可落地的代码，助力打造更值得信赖的 AI 系统。","# Responsible Machine Learning with Python\nExamples of techniques for training interpretable machine learning (ML) models, explaining ML models, and debugging ML models for accuracy, discrimination, and security.\n\n\n### Overview\n\nUsage of artificial intelligence (AI) and ML models is likely to become more commonplace as larger swaths of the economy embrace automation and data-driven decision-making. While these predictive systems can be quite accurate, they have often been inscrutable and unappealable black boxes that produce only numeric predictions with no accompanying explanations. Unfortunately, recent studies and recent events have drawn attention to mathematical and sociological flaws in prominent weak AI and ML systems, but practitioners don’t often have the right tools to pry open ML models and debug them. This series of notebooks introduces several approaches that increase transparency, accountability, and trustworthiness in ML models. If you are a data scientist or analyst and you want to train accurate, interpretable ML models, explain ML models to your customers or managers, test those models for security vulnerabilities or social discrimination, or if you have concerns about documentation, validation, or regulatory requirements, then this series of Jupyter notebooks is for you! (But *please* don't take these notebooks or associated materials as legal compliance advice.)\n\nThe notebooks highlight techniques such as:\n* [Monotonic XGBoost models, partial dependence, individual conditional expectation plots, and Shapley explanations](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#enhancing-transparency-in-machine-learning-models-with-python-and-xgboost---notebook)\n* [Decision tree surrogates, reason codes, and ensembles of explanations](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#increase-transparency-and-accountability-in-your-machine-learning-project-with-python---notebook)\n* [Disparate impact analysis](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#increase-fairness-in-your-machine-learning-project-with-disparate-impact-analysis-using-python-and-h2o---notebook)\n* [LIME](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#explain-your-predictive-models-to-business-stakeholders-with-lime-using-python-and-h2o---notebook)\n* [Sensitivity and residual analysis](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#testing-machine-learning-models-for-accuracy-trustworthiness-and-stability-with-python-and-h2o---notebook)\n  * [Advanced sensitivity analysis for model debugging](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#part-1-sensitivity-analysis---notebook)\n  * [Advanced residual analysis for model debugging](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#part-2-residual-analysis---notebook)\n* [Detailed model comparison and model selection by cross-validated ranking](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#from-glm-to-gbm-building-the-case-for-complexity---notebook)\n\nThe notebooks can be accessed through:\n* [H2O Aquarium (Recommended)](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#h2o-aquarium-recommended)\n* [Virtualenv (Advanced)](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#virtualenv-installation)\n* [Docker container (Advanced)](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#docker-installation)\n* [Manual installation (Advanced)](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#manual-installation)\n\n#### Further reading:\n* [*Machine Learning: Considerations for fairly and transparently expanding access to credit*](http:\u002F\u002Finfo.h2o.ai\u002Frs\u002F644-PKX-778\u002Fimages\u002FMachine%20Learning%20-%20Considerations%20for%20Fairly%20and%20Transparently%20Expanding%20Access%20to%20Credit.pdf)\n* [*A Responsible Machine Learning Workflow with Focus on Interpretable Models, Post-hoc Explanation, and Discrimination Testing*](https:\u002F\u002Fwww.mdpi.com\u002F2078-2489\u002F11\u002F3\u002F137)\n* [*An Introduction to Machine Learning Interpretability, 2nd Edition*](https:\u002F\u002Fwww.h2o.ai\u002Fwp-content\u002Fuploads\u002F2019\u002F08\u002FAn-Introduction-to-Machine-Learning-Interpretability-Second-Edition.pdf)\n* [*On the Art and Science of Explainable Machine Learning*](https:\u002F\u002Farxiv.org\u002Fpdf\u002F1810.02909.pdf)\n* [*Proposals for model vulnerability and security*](https:\u002F\u002Fwww.oreilly.com\u002Fideas\u002Fproposals-for-model-vulnerability-and-security)\n* [*Proposed Guidelines for the Responsible Use of Explainable Machine Learning*](https:\u002F\u002Farxiv.org\u002Fpdf\u002F1906.03533.pdf)\n* [*Real-World Strategies for Model Debugging*](https:\u002F\u002Fmedium.com\u002F@jphall_22520\u002Fstrategies-for-model-debugging-aa822f1097ce)\n* [*Warning Signs: Security and Privacy in an Age of Machine Learning*](https:\u002F\u002Ffpf.org\u002Fwp-content\u002Fuploads\u002F2019\u002F09\u002FFPF_WarningSigns_Report.pdf)\n* [*Why you should care about debugging machine learning models*](https:\u002F\u002Fwww.oreilly.com\u002Fradar\u002Fwhy-you-should-care-about-debugging-machine-learning-models\u002F)\n\n***\n\n### Enhancing Transparency in Machine Learning Models with Python and XGBoost - [Notebook](https:\u002F\u002Fnbviewer.jupyter.org\u002Fgithub\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fblob\u002Fmaster\u002Fxgboost_pdp_ice.ipynb)\n\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fjphall663_interpretable_machine_learning_with_python_readme_b46995861b3b.png)\n\nMonotonicity constraints can turn opaque, complex models into transparent, and potentially regulator-approved models, by ensuring predictions only increase or only decrease for any change in a given input variable. In this notebook, I will demonstrate how to use monotonicity constraints in the popular open source gradient boosting package XGBoost to train an interpretable and accurate nonlinear classifier on the UCI credit card default data.\n\nOnce we have trained a monotonic XGBoost model, we will use partial dependence plots and individual conditional expectation (ICE) plots to investigate the internal mechanisms of the model and to verify its monotonic behavior. Partial dependence plots show us the way machine-learned response functions change based on the values of one or two input variables of interest while averaging out the effects of all other input variables. ICE plots can be used to create more localized descriptions of model predictions, and ICE plots pair nicely with partial dependence plots. An example of generating regulator mandated reason codes from high fidelity Shapley explanations for any model prediction is also presented. The combination of monotonic XGBoost, partial dependence, ICE, and Shapley explanations is likely one of the most direct ways to create an interpretable machine learning model today.\n\n\n### Increase Transparency and Accountability in Your Machine Learning Project with Python - [Notebook](https:\u002F\u002Fnbviewer.jupyter.org\u002Fgithub\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fblob\u002Fmaster\u002Fdt_surrogate_loco.ipynb)\n\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fjphall663_interpretable_machine_learning_with_python_readme_7f1af13e0daf.png)\n\nGradient boosting machines (GBMs) and other complex machine learning models are popular and accurate prediction tools, but they can be difficult to interpret. Surrogate models, feature importance, and reason codes can be used to explain and increase transparency in machine learning models. In this notebook, we will train a GBM on the UCI credit card default data. Then we’ll train a decision tree surrogate model on the original inputs and predictions of the complex GBM model and see how the variable importance and interactions displayed in the surrogate model yield an overall, approximate flowchart of the complex model’s predictions. We will also analyze the global variable importance of the GBM and compare this information to the surrogate model, our domain expertise, and our reasonable expectations.\n\nTo get a better picture of the complex model’s local behavior and to enhance the accountability of the model’s predictions, we will use a variant of the leave-one-covariate-out (LOCO) technique. LOCO enables us to calculate the local contribution each input variable makes toward each model prediction. We will then rank the local contributions to generate reason codes that describe, in plain English, the model’s decision process for every prediction.\n\n### Increase Fairness in Your Machine Learning Project with Disparate Impact Analysis using Python and H2O - [Notebook](https:\u002F\u002Fnbviewer.jupyter.org\u002Fgithub\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fblob\u002Fmaster\u002Fdia.ipynb)\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fjphall663_interpretable_machine_learning_with_python_readme_3e7bb08d172e.png\" height=\"400\">\n\nFairness is an incredibly important, but highly complex entity. So much so that leading scholars have yet to agree on a strict definition. However, there is a practical way to discuss and handle observational fairness, or how your model predictions affect different groups of people. This procedure is often known as disparate impact analysis (DIA). DIA is far from perfect, as it relies heavily on user-defined thresholds and reference levels to measure disparity and does not attempt to remediate disparity or provide information on sources of disparity, but it is a fairly straightforward method to quantify your model’s behavior across sensitive demographic segments or other potentially interesting groups of observations. Some types of DIA are also an accepted, regulation-compliant tool for fair-lending purposes in the U.S. financial services industry. If it’s good enough for multibillion-dollar credit portfolios, it’s probably good enough for your project.\n\nThis example DIA notebook starts by training a monotonic gradient boosting machine (GBM) classifier on the UCI credit card default data using the popular open source library, h2o. A probability cutoff for making credit decisions is selected by maximizing the F1 statistic and confusion matrices are generated to summarize the GBM’s decisions across men and women. A basic DIA procedure is then conducted using the information stored in the confusion matrices and some traditional fair lending measures.\n\n### Explain Your Predictive Models to Business Stakeholders with LIME using Python and H2O - [Notebook](https:\u002F\u002Fnbviewer.jupyter.org\u002Fgithub\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fblob\u002Fmaster\u002Flime.ipynb)\n\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fjphall663_interpretable_machine_learning_with_python_readme_66a5bc4aebcf.png)\n\nMachine learning can create very accurate predictive models, but these models can be almost impossible to explain to your boss, your customers, or even your regulators. This notebook will use (Local Interpretable Model-agnostic Explanations) LIME to increase transparency and accountability in a complex GBM model trained on the UCI credit card default data. LIME is a method for building linear surrogate models for local regions in a data set, often single rows of data. LIME sheds light on how model predictions are made and describes local model mechanisms for specific rows of data. Because the LIME sampling process may feel abstract to some practitioners, this notebook will also introduce a more straightforward method of creating local samples for LIME.\n\nOnce local samples have been generated, we will fit LIME models to understand local trends in the complex model’s predictions. LIME can also tell us the local contribution of each input variable toward each model prediction, and these contributions can be sorted to create reason codes -- plain English explanations of every model prediction. We will also validate the fit of the LIME model to enhance trust in our explanations using the local model’s R2 statistic and a ranked prediction plot.\n\n### Testing Machine Learning Models for Accuracy, Trustworthiness, and Stability with Python and H2O - [Notebook](https:\u002F\u002Fnbviewer.jupyter.org\u002Fgithub\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fblob\u002Fmaster\u002Fresid_sens_analysis.ipynb)\n\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fjphall663_interpretable_machine_learning_with_python_readme_96939b6efed1.png)\n\nBecause ML model predictions can vary drastically for small changes in input variable values, especially outside of training input domains, sensitivity analysis is perhaps the most important validation technique for increasing trust in ML model predictions. Sensitivity analysis investigates whether model behavior and outputs remain stable when input data is intentionally perturbed, or other changes are simulated in input data. In this notebook, we will enhance trust in a complex credit default model by testing and debugging its predictions with sensitivity analysis.\n\nWe’ll further enhance trust in our model using residual analysis. Residuals refer to the difference between the recorded value of a target variable and the predicted value of a target variable for each row in a data set. Generally, the residuals of a well-fit model should be randomly distributed, because good models will account for most phenomena in a data set, except for random error. In this notebook, we will create residual plots for a complex model to debug any accuracy problems arising from overfitting or outliers.\n\n### Machine Learning Model Debugging with Python: All Models are Wrong ... but Why is _My_ Model Wrong? (And Can I Fix It?)\n\n##### Part 1: Sensitivity Analysis - [Notebook](https:\u002F\u002Fnbviewer.jupyter.org\u002Fgithub\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fblob\u002Fmaster\u002Fdebugging_sens_analysis_redux.ipynb)\n\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fjphall663_interpretable_machine_learning_with_python_readme_9f03ae38bff8.png)\n\nSensitivity analysis is the perturbation of data under a trained model. It can take many forms and arguably Shapley feature importance, partial dependence, individual conditional expectation, and adversarial examples are all types of sensitivity analysis. This notebook focuses on using these different types of sensitivity analysis to discover error mechanisms and security vulnerabilities and to assess stability and fairness in a trained XGBoost model. It begins by loading the UCI credit card default data and then training an interpretable, monotonically constrained XGBoost model. After the model is trained, global and local Shapley feature importance is calculated. These Shapley values help inform the application of partial dependence and ICE, and together these results guide a search for adversarial examples. The notebook closes by exposing the trained model to a random attack and analyzing the attack results.\n\nThese model debugging exercises uncover accuracy, drift, and security problems such as over-emphasis of important features and impactful yet non-robust interactions. Several remediation mechanisms are proposed including editing of final model artifacts to remove or fix errors, missing value injection or regularization during training to lessen the impact of certain features or interactions, and assertion-based missing value injection during scoring to mitigate the effect of non-robust interactions.\n\n##### Part 2: Residual Analysis - [Notebook](https:\u002F\u002Fnbviewer.jupyter.org\u002Fgithub\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fblob\u002Fmaster\u002Fdebugging_resid_analysis_redux.ipynb)\n\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fjphall663_interpretable_machine_learning_with_python_readme_5500849433e7.png)\n\nIn general, residual analysis can be characterized as a careful study of when and how models make mistakes. A better understanding of mistakes will hopefully lead to fewer of them. This notebook uses variants of residual analysis to find error mechanisms and security vulnerabilities and to assess stability and fairness in a trained XGBoost model. It begins by loading the UCI credit card default data and then training an interpretable, monotonically constrained XGBoost gradient boosting machine (GBM) model. (Pearson correlation with the prediction target is used to determine the direction of the monotonicity constraints for each input variable.) After the model is trained, its logloss residuals are analyzed and explained thoroughly and the constrained GBM is compared to a benchmark linear model. These model debugging exercises uncover accuracy, drift, and security problems such as over-emphasis of important variables and strong signal in model residuals. Several remediation mechanisms are proposed including missing value injection during training, additional data collection, and use of assertions to correct known problems during scoring.\n\n### From GLM to GBM: Building the Case For Complexity - [Notebook](https:\u002F\u002Fnbviewer.jupyter.org\u002Fgithub\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fblob\u002Fmaster\u002Fglm_mgbm_gbm.ipynb)\n\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fjphall663_interpretable_machine_learning_with_python_readme_c1133b8df09c.png)\n\nThis notebook uses the same credit card default scenario to show how monotonicity constraints, Shapley values and other post-hoc explanations, and discrimination testing can enable practitioners to create direct comparisons between GLM and GBM models. Several candidate probability of default models are selected for comparison using feature selection methods, like LASSO, and by cross-validated ranking. Comparisons then enable building from GLM to more complex GBM models in a step-by-step manner, while retaining model transparency and the ability to test for discrimination. This notebook shows that GBMs can yield better accuracy, more revenue, and that GBMs are also likely to fulfill many model documentation, adverse action notice, and discrimination testing requirements.\n\n## Using the Examples\n\n### H2O Aquarium (recommended)\n\nH2O Aquarium is a free educational environment that hosts versions of these notebooks among many other H2o-related resources. To use these notebooks in Aquarium:\n\n1. Navigate to the Aquarium URL: https:\u002F\u002Faquarium.h2o.ai.\n\n2. Create a new Aquarium account.\n\n3. Check the registered email inbox and use the temporary password to login to Aquarium.\n\n4. Click `Browse Labs`.\n\n5. Click `View Detail` under *Open Source MLI Workshop*.\n\n6. Click `Start Lab` (this can take several minutes).\n\n7. Click on the *Jupyter URL* when it becomes available.\n\n8. Enter the token `h2o`.\n\n9. Click the `patrick_hall_mli` folder.\n\n10. Browse\u002Frun the Jupyter notebooks.\n\n11. Click `End Lab` when you are finished.\n\n### Virtualenv Installation\n\nFor avid Python users, creating a Python virtual environment is a convenient way to run these notebooks.\n\n1. Install [Git](https:\u002F\u002Fgit-scm.com\u002Fdownloads).\n\n2. Clone this repository with the examples.\u003C\u002Fbr>\n`$ git clone https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python.git`\n\n3. Install Anaconda Python 5.1.0 from the [Anaconda archives](https:\u002F\u002Frepo.continuum.io\u002Farchive\u002F) and add it to your system path.\n\n4. Change directories into the cloned repository.\u003C\u002Fbr>\n`$ cd interpretable_machine_learning_with_python`\n\n5. Create a Python 3.6 virtual environment.\u003C\u002Fbr>\n`$ virtualenv -p \u002Fpath\u002Fto\u002Fanaconda3\u002Fbin\u002Fpython3.6 env_iml`\n\n6. Activate the virtual environment.\u003C\u002Fbr>\n`$ source env_iml\u002Fbin\u002Factivate`\n\n7. Install the correct packages for the example notebooks.\u003C\u002Fbr>\n`$ pip install -r requirements.txt`\n\n8. Start Jupyter.\u003C\u002Fbr>\n`$ jupyter notebook`\n\n### Docker Installation\n\nA Dockerfile is provided to build a docker container with all necessary packages and dependencies. This is a way to use these examples if you are on Mac OS X, \\*nix, or Windows 10. To do so:\n\n1. Install and start [docker](https:\u002F\u002Fwww.docker.com\u002F).\n\nFrom a terminal:\n\n2. Create a directory for the Dockerfile.\u003C\u002Fbr>\n`$ mkdir anaconda_py36_h2o_xgboost_graphviz_shap`\n\n3. Fetch the Dockerfile.\u003C\u002Fbr>\n`$ curl https:\u002F\u002Fraw.githubusercontent.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fmaster\u002Fanaconda_py36_h2o_xgboost_graphviz_shap\u002FDockerfile > anaconda_py36_h2o_xgboost_graphviz_shap\u002FDockerfile`\n\n4. Build a docker image from the Dockefile.\u003C\u002Fbr>\n`docker build -t iml anaconda_py36_h2o_xgboost_graphviz_shap`\n\n5. Start the docker image and the Jupyter notebook server.\u003C\u002Fbr>\n `docker run -i -t -p 8888:8888 iml:latest \u002Fbin\u002Fbash -c \"\u002Fopt\u002Fconda\u002Fbin\u002Fjupyter notebook --notebook-dir=\u002Finterpretable_machine_learning_with_python --allow-root --ip='*' --port=8888 --no-browser\"`\n\n6. Navigate to port 8888 on your machine, probably `http:\u002F\u002Flocalhost:8888\u002F`.\n\n\n### Manual Installation\n\n1. Anaconda Python 5.1.0 from the [Anaconda archives](https:\u002F\u002Frepo.continuum.io\u002Farchive\u002F).\n2. [Java](https:\u002F\u002Fjava.com\u002Fdownload).\n3. The latest stable [h2o](https:\u002F\u002Fwww.h2o.ai\u002Fdownload\u002F) Python package.\n4. [Git](https:\u002F\u002Fgit-scm.com\u002Fdownloads).\n5. [XGBoost](https:\u002F\u002Fgithub.com\u002Fdmlc\u002Fxgboost) with Python bindings.\n6. [GraphViz](http:\u002F\u002Fwww.graphviz.org\u002F).\n7. [Seaborn](https:\u002F\u002Fpypi.org\u002Fproject\u002Fseaborn\u002F) package.\n8. [Shap](https:\u002F\u002Fpypi.org\u002Fproject\u002Fshap\u002F) package.  \n\nAnaconda Python, Java, Git, and GraphViz must be added to your system path.\n\nFrom a terminal:\n\n9. Clone the repository with examples.\u003C\u002Fbr>\n`$ git clone https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python.git`\n\n10. `$ cd interpretable_machine_learning_with_python`\n\n11. Start the Jupyter notebook server.\u003C\u002Fbr>\n`$ jupyter notebook`\n\n12. Navigate to the port Jupyter directs you to on your machine, probably `http:\u002F\u002Flocalhost:8888\u002F`.\n","# 使用 Python 进行负责任的机器学习\n训练可解释机器学习（ML）模型、解释 ML 模型以及针对准确性、歧视性和安全性调试 ML 模型的技术示例。\n\n\n### 概述\n\n随着经济的更大范围采用自动化和数据驱动决策，人工智能（AI）和 ML 模型的使用很可能会变得更加普遍。尽管这些预测系统可能非常准确，但它们往往被视为难以理解且无法申诉的黑箱，只产生数值预测而没有相应的解释。不幸的是，近期的研究和事件已经引起了人们对一些知名弱 AI 和 ML 系统中数学及社会学缺陷的关注，然而从业者通常缺乏合适的工具来深入分析并调试这些 ML 模型。本系列笔记本介绍了几种能够提高 ML 模型透明度、可问责性及可信度的方法。如果你是一名数据科学家或分析师，希望训练出准确且可解释的 ML 模型、向客户或管理层解释 ML 模型、测试这些模型是否存在安全漏洞或社会歧视问题，或者对文档记录、验证和监管要求有所顾虑，那么这套 Jupyter 笔记本就非常适合你！（但请不要将这些笔记本或相关材料视为法律合规建议。）\n\n这些笔记本重点介绍了以下技术：\n* [单调 XGBoost 模型、部分依赖图、个体条件期望图和 Shapley 解释](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#enhancing-transparency-in-machine-learning-models-with-python-and-xgboost---notebook)\n* [决策树代理模型、原因代码以及解释集合](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#increase-transparency-and-accountability-in-your-machine-learning-project-with-python---notebook)\n* [差异影响分析](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#increase-fairness-in-your-machine-learning-project-with-disparate-impact-analysis-using-python-and-h2o---notebook)\n* [LIME](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#explain-your-predictive-models-to-business-stakeholders-with-lime-using-python-and-h2o---notebook)\n* [敏感性分析和残差分析](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#testing-machine-learning-models-for-accuracy-trustworthiness-and-stability-with-python-and-h2o---notebook)\n  * [用于模型调试的高级敏感性分析](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#part-1-sensitivity-analysis---notebook)\n  * [用于模型调试的高级残差分析](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#part-2-residual-analysis---notebook)\n* [基于交叉验证排名的详细模型比较与选择](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#from-glm-to-gbm-building-the-case-for-complexity---notebook)\n\n这些笔记本可以通过以下方式访问：\n* [H2O Aquarium（推荐）](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#h2o-aquarium-recommended)\n* [Virtualenv（高级）](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#virtualenv-installation)\n* [Docker 容器（高级）](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#docker-installation)\n* [手动安装（高级）](https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python#manual-installation)\n\n#### 延伸阅读：\n* [*机器学习：公平透明地扩大信贷获取渠道的考量*](http:\u002F\u002Finfo.h2o.ai\u002Frs\u002F644-PKX-778\u002Fimages\u002FMachine%20Learning%20-%20Considerations%20for%20Fairly%20and%20Transparently%20Expanding%20Access%20to%20Credit.pdf)\n* [*以可解释模型、事后解释和歧视检测为重点的责任型机器学习工作流*](https:\u002F\u002Fwww.mdpi.com\u002F2078-2489\u002F11\u002F3\u002F137)\n* [*机器学习可解释性入门（第二版）*](https:\u002F\u002Fwww.h2o.ai\u002Fwp-content\u002Fuploads\u002F2019\u002F08\u002FAn-Introduction-to-Machine-Learning-Interpretability-Second-Edition.pdf)\n* [*关于可解释机器学习的艺术与科学*](https:\u002F\u002Farxiv.org\u002Fpdf\u002F1810.02909.pdf)\n* [*关于模型脆弱性和安全性的建议*](https:\u002F\u002Fwww.oreilly.com\u002Fideas\u002Fproposals-for-model-vulnerability-and-security)\n* [*可解释机器学习负责任使用指南草案*](https:\u002F\u002Farxiv.org\u002Fpdf\u002F1906.03533.pdf)\n* [*模型调试的实战策略*](https:\u002F\u002Fmedium.com\u002F@jphall_22520\u002Fstrategies-for-model-debugging-aa822f1097ce)\n* [*警示信号：机器学习时代的安全与隐私*](https:\u002F\u002Ffpf.org\u002Fwp-content\u002Fuploads\u002F2019\u002F09\u002FFPF_WarningSigns_Report.pdf)\n* [*为什么你应该关注机器学习模型的调试*](https:\u002F\u002Fwww.oreilly.com\u002Fradar\u002Fwhy-you-should-care-about-debugging-machine-learning-models\u002F)\n\n***\n\n### 使用 Python 和 XGBoost 提升机器学习模型透明度 - [笔记本](https:\u002F\u002Fnbviewer.jupyter.org\u002Fgithub\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fblob\u002Fmaster\u002Fxgboost_pdp_ice.ipynb)\n\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fjphall663_interpretable_machine_learning_with_python_readme_b46995861b3b.png)\n\n单调性约束可以将不透明、复杂的模型转变为透明且可能获得监管部门批准的模型，方法是确保对于任何输入变量的变化，预测结果只会增加或只会减少。在本笔记本中，我将演示如何在流行的开源梯度提升库 XGBoost 中使用单调性约束，在 UCI 信用卡违约数据集上训练一个可解释且准确的非线性分类器。\n\n一旦我们训练好了一个单调的 XGBoost 模型，我们将使用部分依赖图和个体条件期望（ICE）图来探究模型的内部机制，并验证其单调行为。部分依赖图展示了机器学习响应函数如何根据一两个感兴趣的输入变量的取值而变化，同时平均掉了其他所有输入变量的影响。ICE 图则可以用来更局部地描述模型的预测结果，而且 ICE 图与部分依赖图相辅相成。此外，还展示了一个从高精度 Shapley 解释中生成监管部门要求的原因代码的例子，适用于任何模型预测。单调 XGBoost、部分依赖图、ICE 图和 Shapley 解释的结合，很可能是当今创建可解释机器学习模型最直接的方式之一。\n\n### 使用 Python 提升机器学习项目的透明度和可解释性——[Notebook](https:\u002F\u002Fnbviewer.jupyter.org\u002Fgithub\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fblob\u002Fmaster\u002Fdt_surrogate_loco.ipynb)\n\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fjphall663_interpretable_machine_learning_with_python_readme_7f1af13e0daf.png)\n\n梯度提升机（GBM）及其他复杂机器学习模型是广受欢迎且精准的预测工具，但其决策过程往往难以理解。通过代理模型、特征重要性和原因代码，我们可以更好地解释这些模型并提高其透明度。在本笔记本中，我们将基于 UCI 信用卡违约数据训练一个 GBM 模型，随后使用原始输入及 GBM 的预测结果来训练一棵决策树代理模型。通过分析代理模型所展示的变量重要性和交互作用，我们可以获得该复杂模型预测的整体近似流程图。此外，我们还将评估 GBM 的全局特征重要性，并将其与代理模型、领域专业知识以及合理预期进行对比。\n\n为了更深入地了解复杂模型的局部行为并增强其预测的可解释性，我们将采用一种变体的“逐协变量剔除法”（LOCO）。LOCO 能够计算每个输入变量对单个模型预测的局部贡献。接着，我们根据这些局部贡献进行排序，生成原因代码，用通俗易懂的语言描述模型针对每条样本的决策过程。\n\n### 使用 Python 和 H2O 进行差异影响分析，提升机器学习项目的公平性——[Notebook](https:\u002F\u002Fnbviewer.jupyter.org\u002Fgithub\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fblob\u002Fmaster\u002Fdia.ipynb)\n\n\u003Cimg src=\"https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fjphall663_interpretable_machine_learning_with_python_readme_3e7bb08d172e.png\" height=\"400\">\n\n公平性是一个极其重要但又极为复杂的概念，以至于学界至今仍未就其严格定义达成共识。然而，我们仍有一种实用的方法来讨论和衡量观察层面的公平性，即你的模型预测如何影响不同人群。这种方法通常被称为差异影响分析（DIA）。尽管 DIA 并不完美——它高度依赖用户设定的阈值和参照水平来衡量差异，且无法直接解决或揭示差异来源——但它确实是一种相对简单直观的方式，用于量化模型在敏感人口统计子群体或其他感兴趣观测组中的表现差异。在美国金融服务行业中，某些类型的 DIA 已被认可为符合监管要求的公平信贷工具。既然连数十亿美元规模的信贷组合都能接受，那么你的项目也完全可以采用这种方法。\n\n本示例 DIA 笔记本首先利用流行的开源库 h2o，在 UCI 信用卡违约数据上训练一个单调递增的梯度提升机分类器。我们通过最大化 F1 分数来确定信用决策的概率阈值，并生成混淆矩阵以总结 GBM 在男性和女性群体上的决策情况。随后，我们将基于混淆矩阵中的信息以及一些传统的公平信贷指标，执行基本的差异影响分析流程。\n\n### 使用 LIME 和 Python、H2O 向业务相关方解释你的预测模型——[Notebook](https:\u002F\u002Fnbviewer.jupyter.org\u002Fgithub\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fblob\u002Fmaster\u002Flime.ipynb)\n\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fjphall663_interpretable_machine_learning_with_python_readme_66a5bc4aebcf.png)\n\n机器学习能够构建出非常精确的预测模型，但这些模型往往难以向你的上司、客户甚至监管部门解释清楚。本笔记本将借助 LIME（局部可解释的模型无关解释）方法，提升基于 UCI 信用卡违约数据训练的复杂 GBM 模型的透明度和可解释性。LIME 是一种为数据集中的局部区域（通常是单个样本）构建线性代理模型的技术。它能够揭示模型预测的形成机制，并针对特定样本描述其局部运作原理。考虑到 LIME 的采样过程可能对部分从业者而言较为抽象，本笔记本还将介绍一种更为简便的本地样本生成方法。\n\n生成本地样本后，我们将拟合 LIME 模型，以理解复杂模型预测中的局部趋势。LIME 还能告诉我们每个输入变量对单个预测的局部贡献，这些贡献可以进一步排序，形成原因代码——即对每条预测的通俗解释。此外，我们还将通过 LIME 模型的 R² 统计量和排序后的预测图来验证模型的拟合效果，从而增强人们对解释结果的信任。\n\n### 使用 Python 和 H2O 测试机器学习模型的准确性、可信度和稳定性——[Notebook](https:\u002F\u002Fnbviewer.jupyter.org\u002Fgithub\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fblob\u002Fmaster\u002Fresid_sens_analysis.ipynb)\n\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fjphall663_interpretable_machine_learning_with_python_readme_96939b6efed1.png)\n\n由于机器学习模型的预测结果会因输入变量值的微小变化而发生显著波动，尤其是在超出训练数据范围的情况下，敏感性分析便成为提升模型预测可信度的最重要验证手段之一。敏感性分析旨在探究当输入数据被有意扰动或模拟其他变化时，模型的行为和输出是否保持稳定。在本笔记本中，我们将通过对复杂信用违约模型进行敏感性分析，进一步提升其预测的可信度。\n\n此外，我们还将借助残差分析来增强模型的可信度。残差是指数据集中每一行目标变量的实际值与预测值之间的差异。一般来说，拟合良好的模型的残差应呈随机分布，因为优秀的模型应当能够捕捉到数据中的大部分规律，仅留下随机误差。在本笔记本中，我们将为复杂模型绘制残差图，以排查由过拟合或异常值引起的准确性问题。\n\n### 使用 Python 调试机器学习模型：所有模型都有错……但为什么_我的_模型错了？（我又能如何修复它呢？）\n\n##### 第一部分：敏感性分析 - [Notebook](https:\u002F\u002Fnbviewer.jupyter.org\u002Fgithub\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fblob\u002Fmaster\u002Fdebugging_sens_analysis_redux.ipynb)\n\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fjphall663_interpretable_machine_learning_with_python_readme_9f03ae38bff8.png)\n\n敏感性分析是在训练好的模型下对数据进行扰动。它可以有多种形式，可以说 Shapley 特征重要性、部分依赖图、个体条件期望以及对抗样本都属于敏感性分析的范畴。本 Notebook 重点探讨如何利用这些不同类型的敏感性分析来发现错误机制和安全漏洞，并评估已训练 XGBoost 模型的稳定性与公平性。首先加载 UCI 信用卡违约数据集，然后训练一个可解释且具有单调性约束的 XGBoost 模型。模型训练完成后，计算全局和局部的 Shapley 特征重要性。这些 Shapley 值有助于指导部分依赖图和 ICE 的应用，而这些结果共同引导我们寻找对抗样本。最后，Notebook 将训练好的模型暴露于随机攻击之下，并对攻击结果进行分析。\n\n通过这些模型调试练习，可以揭示出准确性、模型漂移以及安全性方面的问题，例如对重要特征的过度强调，以及虽然显著但不够鲁棒的交互作用。文中还提出了几种修复机制，包括编辑最终的模型产物以移除或修正错误；在训练过程中通过注入缺失值或正则化来削弱某些特征或交互作用的影响；以及在评分阶段使用断言式缺失值注入来缓解非鲁棒交互作用带来的影响。\n\n##### 第二部分：残差分析 - [Notebook](https:\u002F\u002Fnbviewer.jupyter.org\u002Fgithub\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fblob\u002Fmaster\u002Fdebugging_resid_analysis_redux.ipynb)\n\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fjphall663_interpretable_machine_learning_with_python_readme_5500849433e7.png)\n\n总体而言，残差分析可以被描述为对模型何时以及如何犯错的细致研究。更好地理解错误的原因，有望减少错误的发生。本 Notebook 利用多种残差分析方法来寻找错误机制和安全漏洞，并评估已训练 XGBoost 模型的稳定性与公平性。首先加载 UCI 信用卡违约数据集，随后训练一个可解释且具有单调性约束的 XGBoost 梯度提升机（GBM）模型。（根据各输入变量与预测目标之间的皮尔逊相关系数来确定单调性约束的方向。）模型训练完成后，对其 logloss 残差进行全面深入的分析与解释，并将该约束 GBM 与基准线性模型进行对比。这些模型调试练习揭示了准确性、漂移以及安全性方面的问题，比如对重要变量的过度强调，以及模型残差中仍存在的强信号。文中提出了一些修复措施，包括在训练过程中注入缺失值、进一步收集数据，以及在评分时使用断言来纠正已知问题。\n\n### 从 GLM 到 GBM：为何需要复杂模型 - [Notebook](https:\u002F\u002Fnbviewer.jupyter.org\u002Fgithub\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fblob\u002Fmaster\u002Fglm_mgbm_gbm.ipynb)\n\n![](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fjphall663_interpretable_machine_learning_with_python_readme_c1133b8df09c.png)\n\n本 Notebook 同样以信用卡违约场景为例，展示了单调性约束、Shapley 值等事后解释方法，以及歧视检测技术如何帮助从业者在 GLM 和 GBM 模型之间进行直接比较。通过 LASSO 等特征选择方法及交叉验证排名，选取了几种候选的违约概率模型进行对比。通过这些对比，可以在逐步推进的过程中由 GLM 过渡到更复杂的 GBM 模型，同时保持模型的透明性并具备歧视检测能力。本 Notebook 表明，GBM 不仅能带来更高的准确率和收益，还很可能满足许多关于模型文档记录、不利决定通知以及歧视检测等方面的要求。\n\n## 如何使用这些示例\n\n### H2O Aquarium（推荐）\n\nH2O Aquarium 是一个免费的教育环境，其中托管了这些 Notebook 的版本以及其他众多 H2O 相关资源。要在 Aquarium 中使用这些 Notebook：\n\n1. 访问 Aquarium 的网址：https:\u002F\u002Faquarium.h2o.ai。\n\n2. 创建一个新的 Aquarium 账户。\n\n3. 查看注册邮箱，使用临时密码登录 Aquarium。\n\n4. 点击“浏览实验室”。\n\n5. 在 *开源 MLI 工作坊* 下点击“查看详情”。\n\n6. 点击“启动实验室”（这可能需要几分钟）。\n\n7. 当 *Jupyter URL* 可用时，点击它。\n\n8. 输入令牌 `h2o`。\n\n9. 点击 `patrick_hall_mli` 文件夹。\n\n10. 浏览或运行 Jupyter Notebook。\n\n11. 完成后点击“结束实验室”。\n\n### Virtualenv 安装\n\n对于热衷于 Python 的用户来说，创建一个 Python 虚拟环境是运行这些 Notebook 的便捷方式。\n\n1. 安装 [Git](https:\u002F\u002Fgit-scm.com\u002Fdownloads)。\n\n2. 克隆包含这些示例的仓库。\u003C\u002Fbr>\n`$ git clone https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python.git`\n\n3. 从 [Anaconda 存档](https:\u002F\u002Frepo.continuum.io\u002Farchive\u002F) 安装 Anaconda Python 5.1.0，并将其添加到系统路径中。\n\n4. 切换到克隆的仓库目录。\u003C\u002Fbr>\n`$ cd interpretable_machine_learning_with_python`\n\n5. 创建一个 Python 3.6 虚拟环境。\u003C\u002Fbr>\n`$ virtualenv -p \u002Fpath\u002Fto\u002Fanaconda3\u002Fbin\u002Fpython3.6 env_iml`\n\n6. 激活虚拟环境。\u003C\u002Fbr>\n`$ source env_iml\u002Fbin\u002Factivate`\n\n7. 安装示例 Notebook 所需的正确包。\u003C\u002Fbr>\n`$ pip install -r requirements.txt`\n\n8. 启动 Jupyter。\u003C\u002Fbr>\n`$ jupyter notebook`\n\n### Docker 安装\n\n提供了一个 Dockerfile，用于构建包含所有必要包和依赖项的 Docker 容器。如果你使用的是 Mac OS X、\\*nix 或 Windows 10，可以通过这种方式使用这些示例。具体步骤如下：\n\n1. 安装并启动 [Docker](https:\u002F\u002Fwww.docker.com\u002F)。\n\n从终端：\n\n2. 创建一个用于存放 Dockerfile 的目录。\u003C\u002Fbr>\n`$ mkdir anaconda_py36_h2o_xgboost_graphviz_shap`\n\n3. 获取 Dockerfile。\u003C\u002Fbr>\n`$ curl https:\u002F\u002Fraw.githubusercontent.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fmaster\u002Fanaconda_py36_h2o_xgboost_graphviz_shap\u002FDockerfile > anaconda_py36_h2o_xgboost_graphviz_shap\u002FDockerfile`\n\n4. 根据 Dockerfile 构建 Docker 镜像。\u003C\u002Fbr>\n`docker build -t iml anaconda_py36_h2o_xgboost_graphviz_shap`\n\n5. 启动 Docker 镜像及 Jupyter Notebook 服务器。\u003C\u002Fbr>\n `docker run -i -t -p 8888:8888 iml:latest \u002Fbin\u002Fbash -c \"\u002Fopt\u002Fconda\u002Fbin\u002Fjupyter notebook --notebook-dir=\u002Finterpretable_machine_learning_with_python --allow-root --ip='*' --port=8888 --no-browser\"`\n\n6. 在你的机器上访问端口 8888，通常是 `http:\u002F\u002Flocalhost:8888\u002F`。\n\n### 手动安装\n\n1. Anaconda Python 5.1.0，可从 [Anaconda 存档](https:\u002F\u002Frepo.continuum.io\u002Farchive\u002F) 下载。\n2. [Java](https:\u002F\u002Fjava.com\u002Fdownload)。\n3. 最新稳定版的 [h2o](https:\u002F\u002Fwww.h2o.ai\u002Fdownload\u002F) Python 包。\n4. [Git](https:\u002F\u002Fgit-scm.com\u002Fdownloads)。\n5. 带有 Python 绑定的 [XGBoost](https:\u002F\u002Fgithub.com\u002Fdmlc\u002Fxgboost)。\n6. [GraphViz](http:\u002F\u002Fwww.graphviz.org\u002F)。\n7. [Seaborn](https:\u002F\u002Fpypi.org\u002Fproject\u002Fseaborn\u002F) 包。\n8. [Shap](https:\u002F\u002Fpypi.org\u002Fproject\u002Fshap\u002F) 包。\n\nAnaconda Python、Java、Git 和 GraphViz 必须添加到系统的环境变量中。\n\n在终端中：\n\n9. 克隆包含示例的仓库。\u003C\u002Fbr>\n`$ git clone https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python.git`\n\n10. `$ cd interpretable_machine_learning_with_python`\n\n11. 启动 Jupyter Notebook 服务器。\u003C\u002Fbr>\n`$ jupyter notebook`\n\n12. 打开 Jupyter 指向的本地端口，通常是 `http:\u002F\u002Flocalhost:8888\u002F`。","# interpretable_machine_learning_with_python 快速上手指南\n\n本指南旨在帮助开发者快速掌握如何使用 Python 训练可解释的机器学习模型、解释模型预测以及调试模型的准确性、公平性和安全性。本项目基于一系列 Jupyter Notebook，涵盖了单调约束 XGBoost、代理模型、差异影响分析（DIA）、LIME 及敏感性分析等核心技术。\n\n## 环境准备\n\n在开始之前，请确保您的开发环境满足以下要求：\n\n*   **操作系统**：Linux, macOS 或 Windows (推荐 Linux\u002FmacOS 以获得最佳兼容性)\n*   **Python 版本**：Python 3.6 或更高版本\n*   **核心依赖库**：\n    *   `jupyter` \u002F `jupyterlab`\n    *   `xgboost`\n    *   `h2o` (H2O.ai Python 客户端)\n    *   `lime`\n    *   `scikit-learn`\n    *   `pandas`, `numpy`, `matplotlib`, `seaborn`\n*   **前置知识**：具备基础的 Python 编程能力和机器学习概念理解。\n\n> **提示**：国内用户若遇到 PyPI 下载缓慢问题，建议在安装时使用清华或阿里镜像源。\n\n## 安装步骤\n\n本项目提供多种运行方式，推荐使用 **H2O Aquarium** (简化版) 或 **Virtualenv** (标准版)。以下介绍最常用的 Virtualenv 安装流程。\n\n### 1. 创建虚拟环境\n```bash\npython -m venv iml_env\nsource iml_env\u002Fbin\u002Factivate  # Windows 用户请使用: iml_env\\Scripts\\activate\n```\n\n### 2. 安装依赖包\n使用国内镜像源加速安装核心依赖：\n```bash\npip install -i https:\u002F\u002Fpypi.tuna.tsinghua.edu.cn\u002Fsimple jupyterlab xgboost scikit-learn pandas numpy matplotlib seaborn\npip install -i https:\u002F\u002Fpypi.tuna.tsinghua.edu.cn\u002Fsimple lime\n```\n\n### 3. 安装 H2O Python 客户端\nH2O 是该项目中用于公平性分析和模型训练的重要组件：\n```bash\npip install -i https:\u002F\u002Fpypi.tuna.tsinghua.edu.cn\u002Fsimple h2o\n```\n\n### 4. 获取项目代码\n克隆仓库并进入目录：\n```bash\ngit clone https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python.git\ncd interpretable_machine_learning_with_python\n```\n\n### 5. 启动 Jupyter Lab\n```bash\njupyter lab\n```\n在浏览器中打开对应的 `.ipynb` 文件即可开始学习。\n\n## 基本使用\n\n本项目由多个独立的 Notebook 组成，每个 Notebook 针对不同的可解释性技术。以下是两个最核心的使用场景示例。\n\n### 场景一：构建带单调约束的可解释 XGBoost 模型\n此示例展示如何训练一个符合监管要求的单调模型，并使用部分依赖图（PDP）和个体条件期望图（ICE）进行解释。\n\n1.  打开 Notebook: `xgboost_pdp_ice.ipynb`\n2.  核心代码逻辑如下：\n\n```python\nimport xgboost as xgb\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\n\n# 1. 加载数据 (以 UCI 信用卡违约数据为例)\ndata = pd.read_csv('default_of_credit_card_clients.csv')\nX = data.drop('default payment next month', axis=1)\ny = data['default payment next month']\n\n# 2. 定义单调约束 (例如：余额越高，违约概率越大 -> 约束为 1)\n# 假设第 0 列是余额，设置为单调递增\nmonotone_constraints = {0: 1} \n\n# 3. 训练带有单调约束的 XGBoost 模型\ndtrain = xgb.DMatrix(X, label=y)\nparams = {\n    'objective': 'binary:logistic',\n    'monotone_constraints': '(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)', # 需根据实际特征数量调整\n    'eval_metric': 'auc'\n}\nmodel = xgb.train(params, dtrain, num_boost_round=100)\n\n# 4. 后续步骤请参考 Notebook 中的 PDP 和 ICE 绘图代码，以可视化模型行为\n```\n\n### 场景二：使用 LIME 解释黑盒模型预测\n此示例展示如何利用 LIME 向业务利益相关者解释复杂模型（如 GBM）的单个预测结果。\n\n1.  打开 Notebook: `lime.ipynb`\n2.  核心代码逻辑如下：\n\n```python\nimport lime\nimport lime.lime_tabular\nimport h2o\nfrom h2o.estimators import H2OGradientBoostingEstimator\n\n# 1. 初始化 H2O 并训练一个黑盒 GBM 模型\nh2o.init()\ndf = h2o.import_file(\"default_of_credit_card_clients.csv\")\ngbm = H2OGradientBoostingEstimator()\ngbm.train(x=list(df.columns[:-1]), y=df.columns[-1], training_frame=df)\n\n# 2. 准备解释器 (将 H2O 模型包装为 Python 函数供 LIME 调用)\ndef predict_fn(data):\n    # 将 numpy 数组转换为 H2O Frame 并预测\n    hf = h2o.H2OFrame(pd.DataFrame(data, columns=X_columns))\n    preds = gbm.predict(hf).as_data_frame().values\n    return preds[:, 1] # 返回正类概率\n\n# 3. 创建 LIME 解释器\nexplainer = lime.lime_tabular.LimeTabularExplainer(\n    training_data=X_train.values,\n    feature_names=X_columns,\n    class_names=['No Default', 'Default'],\n    mode='classification'\n)\n\n# 4. 解释单个样本\ninstance = X_test.iloc[0].values\nexp = explainer.explain_instance(instance, predict_fn, num_features=5)\n\n# 5. 展示结果\nexp.show_in_notebook(show_table=True, show_all=False)\n```\n\n### 其他关键模块\n*   **公平性分析**：运行 `dia.ipynb` 进行差异影响分析（Disparate Impact Analysis），检测模型在不同人口统计群体间的偏见。\n*   **代理模型**：运行 `dt_surrogate_loco.ipynb` 训练决策树代理模型，将复杂的 GBM 转化为可理解的规则流。\n*   **模型调试**：运行 `resid_sens_analysis.ipynb` 进行残差和敏感性分析，评估模型的稳定性和鲁棒性。\n\n> **注意**：本工具包提供的代码和技术旨在辅助模型理解和调试，不构成法律合规建议。在涉及金融信贷等强监管场景时，请结合具体法律法规使用。","某金融科技公司数据团队正在构建自动信贷审批模型，亟需向监管机构证明算法的公平性与决策透明度。\n\n### 没有 interpretable_machine_learning_with_python 时\n- 模型如同“黑盒”，只能输出通过率数字，无法向被拒客户提供具体的拒绝理由（Reason Codes），导致客诉激增。\n- 难以察觉模型是否存在对特定性别或种族群体的隐性歧视，面临巨大的合规与法律风险。\n- 当模型出现异常预测时，缺乏残差分析和敏感性测试工具，开发人员只能盲目调整参数，排查效率极低。\n- 业务部门因不信任无法解释的 AI 预测，坚持沿用低效的人工审核流程，自动化转型陷入僵局。\n\n### 使用 interpretable_machine_learning_with_python 后\n- 利用 LIME 和 Shapley 值分析生成个体层面的解释，为每位被拒客户自动生成清晰、可理解的拒绝原因代码。\n- 通过差异影响分析（Disparate Impact Analysis）量化并消除了模型中的性别与种族偏见，确保符合公平借贷法规。\n- 借助高级敏感性与残差分析笔记本快速定位模型缺陷，精准修复了导致极端值预测错误的特征交互问题。\n- 用单调性 XGBoost 和决策树代理模型构建了透明且准确的评分卡，成功说服业务方全面上线自动化审批系统。\n\ninterpretable_machine_learning_with_python 将不可知的算法黑盒转化为透明、可信且合规的决策引擎，真正实现了负责任的机器学习落地。","https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002Fjphall663_interpretable_machine_learning_with_python_b4699586.png","jphall663","ph_","https:\u002F\u002Foss.gittoolsai.com\u002Favatars\u002Fjphall663_7996fe93.png","Helping people manage AI, machine learning, and analytics risks at hallresearch.ai; GWU assistant prof. ",null,"Washington, DC","hallresearch.ai","https:\u002F\u002Fgithub.com\u002Fjphall663",[81,85,89],{"name":82,"color":83,"percentage":84},"Jupyter Notebook","#DA5B0B",99.7,{"name":86,"color":87,"percentage":88},"Python","#3572A5",0.3,{"name":90,"color":91,"percentage":92},"Dockerfile","#384d54",0,681,208,"2026-04-02T20:53:37","未说明 (支持运行 Jupyter Notebook 的任何操作系统)","未说明",{"notes":99,"python":97,"dependencies":100},"该项目主要由一系列 Jupyter Notebooks 组成，用于演示可解释性机器学习技术。官方推荐使用 'H2O Aquarium' 环境运行，也支持通过 Virtualenv、Docker 容器或手动安装方式部署。涉及的主要库包括 XGBoost（用于单调约束模型）、H2O.ai（用于公平性分析和 LIME）、以及标准的科学计算栈。文中未明确指定具体的 Python 版本或硬件资源需求，通常取决于所加载数据集的大小及所选模型的复杂度。",[101,102,103,104,105,106,107,108],"jupyter","xgboost","h2o","lime","scikit-learn","pandas","numpy","matplotlib",[16,110,14],"其他",[112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,104,103],"machine-learning","python","fatml","xai","gradient-boosting-machine","decision-tree","data-science","fairness","interpretable-machine-learning","interpretability","machine-learning-interpretability","iml","accountability","transparency","data-mining","interpretable-ml","interpretable","interpretable-ai","2026-03-27T02:49:30.150509","2026-04-08T20:29:15.777155",[133,138,143,148,153,158],{"id":134,"question_zh":135,"answer_zh":136,"source_url":137},25054,"无法将 Graphviz 的 dot 文件转换为 png 图片，如何解决？","在调用 subprocess 执行转换命令时，需要添加 shell=True 参数。请尝试修改代码为：_ = subprocess.call(png_args, shell=True)。","https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fissues\u002F4",{"id":139,"question_zh":140,"answer_zh":141,"source_url":142},25055,"O'Reilly Safari 上的相关视频链接失效或显示 404 错误怎么办？","部分视频可能因过时已被 O'Reilly 移除。建议尝试使用项目提供的 aquarium、docker 或 virtualenv 安装方案作为替代环境来运行代码。","https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fissues\u002F10",{"id":144,"question_zh":145,"answer_zh":146,"source_url":147},25056,"Dockerfile 中的 h2o 版本过旧导致无法计算 SHAP 值，如何解决？","请使用更新后的 Dockerfile，新版本已修复 h2o 问题并支持 SHAP 值计算。临时解决方案是使用 aquarium 环境。","https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fissues\u002F11",{"id":149,"question_zh":150,"answer_zh":151,"source_url":152},25057,"在哪里可以找到 YouTube 视频中提到的演讲幻灯片（PDF）？","幻灯片已上传至 GitHub，下载地址为：https:\u002F\u002Fgithub.com\u002Fh2oai\u002Fh2o-meetups\u002Fblob\u002Fmaster\u002F2018_04_30_NYC_MLI_good_bad_ugly\u002FMLI_good_bad_ugly.pdf。此外，还可以参考 mli-resources 和 awesome-machine-learning-interpretability 仓库获取更多资源。","https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fissues\u002F3",{"id":154,"question_zh":155,"answer_zh":156,"source_url":157},25058,"如何计算不利影响比率（Adverse Impact Ratio）以评估模型公平性？","可以通过以下公式计算：1. 不利影响 (Adverse Impact) = (tp + fp) \u002F (tp + fp + tn + fn)；2. 不利影响差异比 (Disparity Ratio) = 非参考组的不利影响 \u002F 参考组的不利影响；3. 若差异比处于设定的高低阈值之间，则视为满足不利影响平等 (Parity)。","https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fissues\u002F9",{"id":159,"question_zh":160,"answer_zh":161,"source_url":162},25059,"O'Reilly 视频链接偶尔无法访问是普遍问题吗？","这通常是暂时性问题或与用户所在地区网络有关。如果遇到无法访问，可以先稍后重试，或直接使用项目提供的本地环境（如 docker\u002Faquarium）运行示例，不依赖在线视频资源。","https:\u002F\u002Fgithub.com\u002Fjphall663\u002Finterpretable_machine_learning_with_python\u002Fissues\u002F7",[]]