有没有办法在 SHAP 蜂群图中自定义特征顺序?
Is there a way to customize the feature order in a SHAP beeswarm plot?
我想知道是否有办法更改 SHAP 蜂群图中特征的显示顺序。文档描述了“转换”,例如使用 shap_values.abs
或 shap_values.abs.mean(0)
来更改方式排序是计算出来的,但我真正想要的是放入一个特征或索引列表并按此排序。
来自文档:
shap.plots.beeswarm(shap_values, order=shap_values.abs)
This is the resulting plot
这是排序的默认实现:
import xgboost
import shap
X, y = shap.datasets.adult()
model = xgboost.XGBClassifier().fit(X, y)
explainer = shap.Explainer(model, X)
shap_values = explainer(X)
shap.plots.beeswarm(shap_values, max_display=12, order=shap.Explanation.abs.mean(0))
然后,如果您想手动定义输出列的顺序:
order = [
"Country",
"Workclass",
"Education-Num",
"Marital Status",
"Occupation",
"Relationship",
"Race",
"Sex",
"Capital Gain",
"Capital Loss",
"Hours per week",
"Age",
]
col2num = {col: i for i, col in enumerate(X.columns)}
order = list(map(col2num.get, order))
shap.plots.beeswarm(shap_values, max_display=12, show=False, color_bar=False, order=order)
plt.colorbar()
plt.show()
我想知道是否有办法更改 SHAP 蜂群图中特征的显示顺序。文档描述了“转换”,例如使用 shap_values.abs
或 shap_values.abs.mean(0)
来更改方式排序是计算出来的,但我真正想要的是放入一个特征或索引列表并按此排序。
来自文档:
shap.plots.beeswarm(shap_values, order=shap_values.abs)
This is the resulting plot
这是排序的默认实现:
import xgboost
import shap
X, y = shap.datasets.adult()
model = xgboost.XGBClassifier().fit(X, y)
explainer = shap.Explainer(model, X)
shap_values = explainer(X)
shap.plots.beeswarm(shap_values, max_display=12, order=shap.Explanation.abs.mean(0))
然后,如果您想手动定义输出列的顺序:
order = [
"Country",
"Workclass",
"Education-Num",
"Marital Status",
"Occupation",
"Relationship",
"Race",
"Sex",
"Capital Gain",
"Capital Loss",
"Hours per week",
"Age",
]
col2num = {col: i for i, col in enumerate(X.columns)}
order = list(map(col2num.get, order))
shap.plots.beeswarm(shap_values, max_display=12, show=False, color_bar=False, order=order)
plt.colorbar()
plt.show()