shap的回购问题:https://github.com/slundberg/shap/issues/2783
因此,目前,我知道如何将基准(预期)值从日志赔率转换为概率,
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_train)
odds = np.exp(explainer.expected_value)
odds / (1 + odds)这很好,但是当我尝试将每个单独的shap值转换为概率增加/减少时,问题就出现了。这个公式不起作用,所以我想知道如何才能得到每个特性所贡献的增减百分比

基本上,每个长度(比如图片上用红色标注的长度)所占的百分比是多少?
我在寻找一个离散的数字,它对应于每个特征条的增减百分比(在概率上,而不是对数概率)。
# this generates the plot
shap.force_plot(
explainer.expected_value,
shap_values[1, :],
X_train.iloc[1, :],
link='logit'
)发布于 2022-12-04 00:03:34
我想我可能已经找到了答案,不确定它是否正确,因为唯一的比较方法是通过视觉近似,但这是我想出的。如果有人可以尝试,并确定是计算是关闭的,那将是惊人的!
首先,我们必须创建一个帮助函数来将日志赔率转换为概率。
def lo_to_prob(x):
odds = np.exp(x)
return odds / (1 + odds)建立我们的模型
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_train)这是我找到的转换公式
# this is the row that we want to find the shap outcomes for
observation = 0
# this is the column that we want to find the percent change for
column_num = 2
# this formula gives us the outcome probability number
shap_outcome_val = lo_to_prob(
explainer.expected_value + shap_values[observation, :].sum()
)
# this give us the shap outcome value, without the single column
shap_outcome_minus_one_column = lo_to_prob(
explainer.expected_value + shap_values[observation, :].sum() - shap_values[observation, :][column_num]
)
# simply subtract the 2 probabilities to get the pct increase that the one column provides
pct_change = shap_outcome_val - shap_outcome_minus_one_column
pct_change检查图表,看看我们感兴趣的列的条形线的长度是否与计算得到的长度有关。
shap.force_plot(
explainer.expected_value,
shap_values[observation, :],
X_train.iloc[observation, :],
link='logit'
)同样,也不确定这是否是100%正确的,因为唯一的方法来验证是视觉。看上去很近。试一试让我知道
https://stackoverflow.com/questions/74664259
复制相似问题