合并EBMs#

在本笔记本中,我们将拟合几个可解释的增强机(EBMs),然后将它们合并成一个单一的EBM模型。最终的EBM模型保留了组件EBM模型的玻璃盒可解释性,使我们能够查看合并模型的全局和局部解释。

这个笔记本可以在我们的examples folder在GitHub上找到。

# install interpret if not already installed
try:
    import interpret
except ModuleNotFoundError:
    !pip install --quiet interpret pandas scikit-learn
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from interpret import show

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]

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)

探索数据集

from interpret.data import ClassHistogram

hist = ClassHistogram().explain_data(X_train, y_train, name = 'Train Data')
show(hist)

训练多个EBM模型

from interpret.glassbox import ExplainableBoostingClassifier

# Fitting multiple EBM models with different training datasets and random seeds
seed = 1
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=seed)
ebm1 = ExplainableBoostingClassifier(random_state=seed)
ebm1.fit(X_train, y_train)  

seed +=10
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=seed)
ebm2 = ExplainableBoostingClassifier(random_state=seed)
ebm2.fit(X_train, y_train)  

seed +=10
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=seed)
ebm3 = ExplainableBoostingClassifier(random_state=seed)
ebm3.fit(X_train, y_train)
ExplainableBoostingClassifier(random_state=21)
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.
show(ebm1.explain_global(name='EBM 1'))

合并多个训练好的EBM模型

from interpret.glassbox import merge_ebms

merged_ebm = merge_ebms([ebm1, ebm2 , ebm3])
show(merged_ebm.explain_global(name='Merged EBM'))