入门指南#
安装
Interpret 在 Windows、Mac 和 Linux 上支持 Python 3.5+
pip install interpret
conda install -c conda-forge interpret
克隆 interpretml/interpret.git 并进入 interpret/scripts 目录,然后执行 make install
InterpretML 支持训练可解释模型(glassbox),以及解释现有的机器学习管道(blackbox)。 让我们通过使用 UCI 成人收入分类数据集的示例来了解每种方法。
下载并准备数据
首先,我们将数据加载到标准的pandas dataframe或numpy数组中,并创建训练/测试分割。使用InterpretML时,不需要对数据进行特殊的预处理。
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from interpret import set_visualize_provider
from interpret.provider import InlineProvider
set_visualize_provider(InlineProvider())
df = pd.read_csv(
"https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data",
header=None)
df.columns = [
"Age", "WorkClass", "fnlwgt", "Education", "EducationNum",
"MaritalStatus", "Occupation", "Relationship", "Race", "Gender",
"CapitalGain", "CapitalLoss", "HoursPerWeek", "NativeCountry", "Income"
]
X = df.iloc[:, :-1]
y = (df.iloc[:, -1] == " >50K").astype(int)
seed = 42
np.random.seed(seed)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=seed)
训练一个Glassbox模型
Glassbox 模型旨在完全可解释,并且通常提供与最先进方法相似的准确性。
InterpretML 允许您使用熟悉的 scikit-learn 界面训练许多最新的玻璃盒模型。
from interpret.glassbox import ExplainableBoostingClassifier
ebm = ExplainableBoostingClassifier()
ebm.fit(X_train, y_train)
ExplainableBoostingClassifier()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
ExplainableBoostingClassifier()
解释Glassbox
Glassbox模型可以在全局(整体行为)和局部(个别预测)两个层面上提供解释。
全局解释对于理解模型认为重要的内容以及识别其决策中的潜在缺陷(例如种族偏见)非常有用。
这里嵌入的内联可视化正是笔记本中生成的内容。
对于这个全局解释,初始摘要页面显示了最重要的特征。您可以使用下拉菜单搜索、筛选和选择单个特征以深入了解更多信息。
尝试查看“年龄”特征,看看高收入概率如何随年龄变化,或者查看“种族”或“性别”特征,观察模型可能学到的潜在偏见。
from interpret import show
show(ebm.explain_global())