首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python如何将shap值转换为概率增加/减少?

问python如何将shap值转换为概率增加/减少?
EN

Stack Overflow用户
提问于 2022-12-03 05:36:17
回答 1查看 34关注 0票数 0

shap的回购问题:https://github.com/slundberg/shap/issues/2783

因此,目前,我知道如何将基准(预期)值从日志赔率转换为概率,

代码语言:javascript
复制
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_train)
odds = np.exp(explainer.expected_value)
odds / (1 + odds)

这很好,但是当我尝试将每个单独的shap值转换为概率增加/减少时,问题就出现了。这个公式不起作用,所以我想知道如何才能得到每个特性所贡献的增减百分比

​

​

基本上,每个长度(比如图片上用红色标注的长度)所占的百分比是多少?

我在寻找一个离散的数字,它对应于每个特征条的增减百分比(在概率上,而不是对数概率)。

代码语言:javascript
复制
# this generates the plot
shap.force_plot(
    explainer.expected_value,
    shap_values[1, :],
    X_train.iloc[1, :],
    link='logit'
)
EN

回答 1

Stack Overflow用户

发布于 2022-12-04 00:03:34

我想我可能已经找到了答案,不确定它是否正确,因为唯一的比较方法是通过视觉近似,但这是我想出的。如果有人可以尝试,并确定是计算是关闭的,那将是惊人的!

首先,我们必须创建一个帮助函数来将日志赔率转换为概率。

代码语言:javascript
复制
def lo_to_prob(x):
    odds = np.exp(x)
    return odds / (1 + odds)

建立我们的模型

代码语言:javascript
复制
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_train)

这是我找到的转换公式

代码语言:javascript
复制
# 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

检查图表,看看我们感兴趣的列的条形线的长度是否与计算得到的长度有关。

代码语言:javascript
复制
shap.force_plot(
    explainer.expected_value,
    shap_values[observation, :],
    X_train.iloc[observation, :],
    link='logit'
)

同样,也不确定这是否是100%正确的,因为唯一的方法来验证是视觉。看上去很近。试一试让我知道

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74664259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档