You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Shapash解释SVM(SVC)模型时触发异常的问题求助

问题描述

探索机器学习可解释性工具Shapash时,用RandomForestClassifier能正常生成可视化网页,但使用SVM(svm.SVC)初始化SmartExplainer时触发报错。

相关代码

# 训练SVM模型
svc = svm.SVC()
svc.fit(X_train_smote, y_train_smote)
y_pred = svc.predict(X_test)
print(f"F1 Score {f1_score(y_test, y_pred, average='macro')}")
print(f"Accuracy {accuracy_score(y_test, y_pred)}")
from shapash import SmartExplainer
xpl = SmartExplainer(model=svc)

报错信息

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
/tmp/ipykernel_13648/1233939729.py in <module>
----> 1 xpl = SmartExplainer(model=svc)

~/Python_AI/ai_env/lib/python3.8/site-packages/shapash/explainer/smart_explainer.py in __init__(self, model, backend, preprocessing, postprocessing, features_groups, features_dict, label_dict, title_story, palette_name, colors_dict, **kwargs)
    194         if isinstance(backend, str):
    195             backend_cls = get_backend_cls_from_name(backend)
--> 196             self.backend = backend_cls(
    197                 model=self.model, preprocessing=preprocessing, **kwargs)
    198         elif isinstance(backend, BaseBackend):

~/Python_AI/ai_env/lib/python3.8/site-packages/shapash/backend/shap_backend.py in __init__(self, model, preprocessing, explainer_args, explainer_compute_args)
     16         self.explainer_args = explainer_args if explainer_args else {}
     17         self.explainer_compute_args = explainer_compute_args if explainer_compute_args else {}
--> 18         self.explainer = shap.Explainer(model=model, **self.explainer_args)
     19 
     20     def run_explainer(self, x: pd.DataFrame) -> dict:

~/Python_AI/ai_env/lib/python3.8/site-packages/shap/explainers/_explainer.py in __init__(self, model, masker, link, algorithm, output_names, feature_names, **kwargs)
    166                 # if we get here then we don't know how to handle what was given to us
    167                 else:
--> 168                     raise Exception("The passed model is not callable and cannot be analyzed directly with the given masker! Model: " + str(model))
    169 
    170             # build the right subclass

Exception: The passed model is not callable and cannot be analyzed directly with the given masker! Model: SVC()
解决方案

报错核心原因是Shapash默认用Shap作为后端解释器,而默认的svm.SVC不满足Shap对模型的要求——需要模型支持概率输出或能被通用Explainer调用。提供两种解决办法:

方法1:开启SVC的概率输出

初始化SVC时添加probability=True参数,让模型支持概率预测,Shap的Explainer就能正常处理:

# 修改SVC初始化代码
svc = svm.SVC(probability=True)
svc.fit(X_train_smote, y_train_smote)
# 后续初始化SmartExplainer的代码不变
xpl = SmartExplainer(model=svc)

方法2:指定使用Shap的KernelExplainer

如果不想开启概率输出,可在初始化SmartExplainer时指定使用KernelExplainer,并传入训练数据作为参考样本:

# 初始化时指定后端参数
xpl = SmartExplainer(
    model=svc,
    backend="shap",
    explainer_args={
        "masker": X_train_smote,  # 传入训练集作为参考数据集
        "algorithm": "kernel"
    }
)

注意:KernelExplainer计算速度较慢,更适合小数据集的探索场景。

内容的提问来源于stack exchange,提问作者user22

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 20:00:16