小编tra*_*ter的帖子

如何使用 Pipeline 将 SHAP 与 sklearn 的线性 SVC 模型结合使用?

我正在使用 sklearn 的线性 SVC 模型进行文本分类。现在我想使用 SHAP ( https://github.com/slundberg/shap )可视化哪些单词/标记对分类决策影响最大。

现在这不起作用,因为我收到一个错误,该错误似乎源自我定义的管道中的矢量化步骤 - 这里出了什么问题?

我在这种情况下使用 SHAP 的一般方法是否正确?

x_Train, x_Test, y_Train, y_Test = train_test_split(df_all['PDFText'], df_all['class'], test_size = 0.2, random_state = 1234)

pipeline = Pipeline([
    (
        'tfidv',
        TfidfVectorizer(
            ngram_range=(1,3), 
            analyzer='word',
            strip_accents = ascii,
            use_idf = True,
            sublinear_tf=True, 
            max_features=6000, 
            min_df=2, 
            max_df=1.0
        )
    ),
    (
        'lin_svc',
        svm.SVC(
            C=1.0,
            probability=True,
            kernel='linear'
        )
    )
])

pipeline.fit(x_Train, y_Train)

shap.initjs()

explainer = shap.KernelExplainer(pipeline.predict_proba, x_Train)
shap_values = explainer.shap_values(x_Test, nsamples=100)

shap.force_plot(explainer.expected_value[0], shap_values[0][0,:], x_Test.iloc[0,:])
Run Code Online (Sandbox Code Playgroud)

这是我收到的错误消息:

Provided model function fails when applied to …
Run Code Online (Sandbox Code Playgroud)

pipeline svc scikit-learn text-classification shap

9
推荐指数
1
解决办法
9360
查看次数

标签 统计

pipeline ×1

scikit-learn ×1

shap ×1

svc ×1

text-classification ×1