XAI 可解释的AI :如何使用LIME 和 SHAP更好地解释模型的预测( 二 )


# Import the LimeTabularExplainer modulefrom lime.lime_tabular import LimeTabularExplainer# Get the class namesclass_names = ['Has diabetes', 'No diabetes']# Get the feature namesfeature_names = list(X_train.columns)# Fit the Explainer on the training data set using the LimeTabularExplainer explainer = LimeTabularExplainer(X_train.values, feature_names = feature_names, class_names = class_names, mode = 'classification')代码中我们使用class_names创建了两个标签,而不是 1 和 0因为使用名字会更加的直观 。
对单例进行解释说明
这里的解释是针对测试数据中的单个实例进行的
#Perform the explanation on the 8th instance in the test dataexplaination = explainer.explain_instance(X_test.iloc[8], rf_clf.predict_proba)# show the result of the model's explainationexplaination.show_in_notebook(show_table = True, show_all = False)

XAI 可解释的AI :如何使用LIME 和 SHAP更好地解释模型的预测

文章插图
 
该模型以 73% 的置信度预测该特定患者患有糖尿病,并解释该预测,因为血糖水平高于 99,血压高于 70 。在右侧,我们可以看到患者特征的值。
总结本文中接单的介绍了如何使用 SHAP 和 LIME 解释您的机器学习模型 。现在,你也可以对构建的模型进行可解释性分析了,这可以帮助决策者和其他利益相关者获得更多的可见性并理解导致模型输出的决策的解释 。,你可以在下面的资源中找到本文包含的两个Python包,阅读他们的文档可以找到更加高级的使用方式 。
作者:Zoumana Keita

【XAI 可解释的AI :如何使用LIME 和 SHAP更好地解释模型的预测】


推荐阅读