猿问

如何在形状中绘制错误分类的样本?

我有一个基因数据集,其引起疾病的可能性得分在 0 到 1 之间(已知得分为 1 的基因会引起疾病,得分为 0.74 的基因可能会引起疾病)。我正在尝试建立一个机器学习模型来预测回归分类中新基因的疾病评分。


我想查看已知疾病基因但得分较低的基因的形状决策图(例如,得分为 1 的基因,但我的模型得分低于 0.8)。我正在努力将这些基因组合在一起进行绘图。


我的数据如下所示:


X:

Index   Feature1  Feature2   ... FeatureN

Gene1     1           0.2          10

Gene2     1           0.1          7

Gene3     0           0.3          10

#index is actually the index and not a column


Y:

Score

1

0.6

0.4

我运行带有嵌套交叉验证的 xgboost 回归器,查看 MSE、预测的 r2,并绘制观察值与预期值的关系图。我可以在观察到的与预期的图中看到,Y 中得分为 1 的基因有许多模型预测的低分,我想了解为什么模型使用 shap 来做到这一点。不幸的是,我无法提供示例数据。


我正在尝试调整为标签分类给出的示例 shap 代码:


import shap


xgbr = xgboost.XGBRegressor()

xgbr.fit(X_train, Y_train)


select = range(8) #I have 8 features after feature selection with BorutaShap

features = X.iloc[select]

features_display = X.loc[features.index]


explainer = shap.TreeExplainer(xgbr)

expected_value = explainer.expected_value


#Example code from https://slundberg.github.io/shap/notebooks/plots/decision_plot.html: 


y_pred = xgbr.predict(X) 

y_pred = (shap_values.sum(1) + expected_value) > 0

misclassified = y_pred != y_test[select]

shap.decision_plot(expected_value, shap_values, features_display, link='logit', highlight=misclassified)

我该如何选择,y_pred以便预测/基因本应为 1,但实际上低于 0.8(或任何低数字)?


编辑:为了回应给定的答案,我尝试过:


explainer = shap.TreeExplainer(xgbr)

shap_values = explainer.shap_values(X_test)


y_pred = xgbr.predict(X_test)

m = (y_pred <= 0.5) & (Y_test == 1)


shap.initjs()

shap.decision_plot(explainer.expected_value, shap_values,  X_test[m],  return_objects=True)

它运行但m长度为 171(我的 Y_test 数据中的全部行数),然后该图绘制了它看起来像的所有 171 - 而且我从查看数据知道应该只有一个基因 <= 0.5 并且但实际上得分为 1。


莫回无
浏览 102回答 2
2回答

慕尼黑5688855

首先,你提到在回归分类中预测新基因的疾病评分,你是什么意思?输出似乎是二进制的,0或1,因此这是一个二进制分类问题。您应该改用xgboost's 分类器。更新:让我们根据评论假设一个回归问题来模拟您的情况。尽管对于下面的示例,我们应该设置'objective':'multi:softmax'为输出实际标签。根据您的问题,您似乎要做的就是在那些未正确预测的样本上索引测试集,并分析误导性的特征,这具有一定的意义。让我们用一些示例数据集重现您的问题:from sklearn.datasets import load_irisfrom sklearn.model_selection import train_test_splitimport shapimport xgboostX,y = shap.datasets.iris()X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)model = xgboost.train(params={"learning_rate": 0.01},&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dtrain=xgboost.DMatrix(X_train, label=y_train),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num_boost_round =100)使用整个测试集的 SHAP 图非常简单。举个force_plot例子:explainer = shap.TreeExplainer(model)shap_values = explainer.shap_values(X_test)shap.initjs()shap.force_plot(explainer.expected_value, shap_values, X_test)现在,如果我们想对错误分类的样本执行相同的操作,我们需要查看输出概率。由于 iris 数据集有多个类,假设我们想要可视化force_plot那些应该分类为 的样本2,但我们有一个输出值如下1.7:y_pred = model.predict(xgboost.DMatrix(X_test))m = (y_pred <= 1.7) & (y_test == 2)现在我们使用掩码对集合执行布尔索引X_test,并更新shap_values:shap.initjs()c= explainer.shap_values(X_test[m])shap.force_plot(explainer.expected_value, shap_values, X_test[m])这告诉我们,花瓣的长度和宽度主要将回归推向更高的值。因此,它们可能是在错误分类中发挥主要作用的变量。同样,对于一个decision_plot:shap.decision_plot(explainer.expected_value, shap_values,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;X_test[m], feature_order='hclust',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return_objects=True)

拉莫斯之舞

由于我没有您的数据集,因此无法检查代码,但这里的一些想法可能会为您指明方向。看来你没有训练你的回归者。应该是像线一样xgbr = xgboost.XGBRegressor()xgbr.train(X, Y)现在你可以使用了xgbr.predict(X);)您还需要培训解释员:explainer = shap.TreeExplainer(xgbr)with warnings.catch_warnings():&nbsp; &nbsp; &nbsp;warnings.simplefilter("ignore")&nbsp; &nbsp; &nbsp;sh = explainer.shap_values(X)现在您可以选择值:misclassified = (y_pred <= 0.7) & (Y == 1)shap.decision_plot(expected_value, sh, features_display, link='logit', highlight=misclassified)在使用之前,shap我建议您检查回归器对数据的拟合程度。因此,为此我建议您将部分数据用于测试,而不是在训练中使用它。然后,您可以通过计算和比较测试集和训练集的 MSE 来评估拟合优度。差异越大,预测器的表现就越差。
随时随地看视频慕课网APP

相关分类

Python
我要回答