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

使用LIME时出现TypeError: unhashable type: 'slice'错误,求排查方案

解决LIME Explainer初始化时的TypeError: unhashable type: 'slice'问题

这个错误其实是个容易忽略的语法小问题——你在初始化LimeTabularExplainer的时候,位置参数X_train_undersample和后面的关键字参数feature_names之间缺少了逗号,导致Python误解了你的代码意图。

错误原因详解

你写的代码里:

explainer = lime.lime_tabular.LimeTabularExplainer(X_train_undersample feature_names = feature_names,class_names=['0','1'], kernel_width=3)

X_train_undersample后面直接跟着feature_names = feature_names,Python会把这部分解析成尝试对X_train_undersample进行切片/索引操作,把feature_names = feature_names当成索引表达式的一部分。但feature_names通常是字符串列表,属于不可哈希的类型,而切片操作要求索引必须是可哈希的,所以就触发了TypeError: unhashable type: 'slice'。

修正后的代码

只需要在X_train_undersample后面补上逗号,就能正常运行了:

# Create the LIME Explainer
explainer = lime.lime_tabular.LimeTabularExplainer(
    X_train_undersample,  # 补上这个逗号,分隔位置参数和关键字参数
    feature_names=feature_names,
    class_names=['0','1'],
    kernel_width=3
)

小提示

写Python函数调用时,位置参数和关键字参数之间一定要用逗号分隔,哪怕参数换行也要注意这个细节,否则很容易出现这类语法解析错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:15:44