首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法在Streamlit中显示SHAP文本可视化

无法在Streamlit中显示SHAP文本可视化
EN

Stack Overflow用户
提问于 2021-12-08 15:20:50
回答 2查看 572关注 0票数 1

我正在尝试构建我的NLP项目的仪表板。因此,我使用BERT模型进行预测,SHAP包用于可视化,Streamlit用于创建仪表板:

代码语言:javascript
运行
复制
tokenizer = AutoTokenizer.from_pretrained(model_name_cla)
model = AutoModelForSequenceClassification.from_pretrained(model_name_cla)
labels = ['1- Tarife','2- Dateneingabe','3- Bestätigungsmail','4- Kundenbetreuung','5- Aufwand vom Vergleich bis Abschluss',
          '6- After-sales Wechselprozess','7 - Werbung/VX Kommunikation','8 - Sonstiges','9 - Nicht auswertbar']

def f(x):
    tv = torch.tensor([tokenizer.encode(v, padding='max_length', max_length=128, truncation=True) for v in x])
    attention_mask = (tv!=0).type(torch.int64)
    outputs = model(tv,attention_mask=attention_mask)[0].detach().cpu().numpy()
    scores = (np.exp(outputs).T / np.exp(outputs).sum(-1)).T
    val = sp.special.logit(scores)
    return val

text = ['This is just a test']

# build an explainer using a token masker
explainer = shap.Explainer(f, tokenizer, output_names=labels)

shap_values = explainer(text, fixed_context=1)

shap.plots.text(shap_values)

代码在我的jupyter笔记本上工作得很好,但是当我试图以.py文件的形式执行它时,没有任何事情发生。它既不显示任何内容,也不抛出错误。我的控制台在执行时只返回以下内容:

>

如何在流光中显示我的图形?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-12-10 05:15:48

这可以用Streamlit Components和最新的SHAP v0.36+ (它们定义了一个新的getjs方法)可视化,以绘制JS SHAP plots

(有些像summary_plot这样的情节实际上是Matplotlib,可以用st.pyplot绘制)

代码语言:javascript
运行
复制
import streamlit as st
import streamlit.components.v1 as components

def st_shap(plot, height=None):
    shap_html = f"<head>{shap.getjs()}</head><body>{plot.html()}</body>"
    components.html(shap_html, height=height)

st_shap(shap.plots.text(shap_values),400)

streamlit - 用Streamlit显示形状图中找到关于可视化形状的更详细的讨论

票数 1
EN

Stack Overflow用户

发布于 2022-03-15 23:58:08

您可以捕获shap图的输出并使用components.html呈现它。

代码语言:javascript
运行
复制
import streamlit.components.v1 as components
from IPython.core.interactiveshell import InteractiveShell
from IPython.utils import capture

def st_plot_text_shap(shap_val, height=None)
    InteractiveShell().instance()
    with capture.capture_output() as cap: 
        shap.plots.text(shap_val)
    components.html(cap.outputs[1].data['text/html'], height=height scrolling=True)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70277425

复制
相关文章

相似问题

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