首页 > 编程问答 >LimeTabular TypeError:__init__() 缺少 1 个必需的位置参数:'model'

LimeTabular TypeError:__init__() 缺少 1 个必需的位置参数:'model'

时间:2024-07-28 12:57:16浏览次数:9  
标签:python interpreter lime

我已经开始学习Lime了。当我使用LimeTabular形式的Interpret时,我遇到一个问题,我按照标准完成了功能,但它不起作用。 错误是 TypeError: init () 缺少 1 个必需的位置参数:'model'

# %% Imports
from utils import DataLoader
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import f1_score, accuracy_score
from interpret.blackbox import LimeTabular
from interpret import show

# %% Load and preprocess data
data_loader = DataLoader()
data_loader.load_dataset()
data_loader.preprocess_data()
# Split the data for evaluation
X_train, X_test, y_train, y_test = data_loader.get_data_split()
# Oversample the train data
X_train, y_train = data_loader.oversample(X_train, y_train)
print(X_train.shape)
print(X_test.shape)

# %% Fit blackbox model
rf = RandomForestClassifier()
rf.fit(X_train, y_train)
y_pred = rf.predict(X_test)
print(f"F1 Score {f1_score(y_test, y_pred, average='macro')}")
print(f"Accuracy {accuracy_score(y_test, y_pred)}")

# %% Apply lime
# Initilize Lime for Tabular data
lime = LimeTabular(predict_fn=rf.predict_proba,
                   data=X_train, 
                   random_state=1)
# Get local explanations
lime_local = lime.explain_local(X_test[-20:], 
                                y_test[-20:], 
                                name='LIME')

show(lime_local)

# %%

错误消息表明你正在使用 LimeTabular 类的一个旧版本,该版本没有将 model 作为 __init__ 方法中的必需参数。要解决此问题,你需要 将预测函数 ( rf.predict_proba ) 直接传递给 explain_local 方法 ,而不是在初始化 LimeTabular 时传递。

以下是如何修改代码的方法:

# %% Imports
from utils import DataLoader
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import f1_score, accuracy_score
from interpret.blackbox import LimeTabular
from interpret import show

# %% Load and preprocess data
data_loader = DataLoader()
data_loader.load_dataset()
data_loader.preprocess_data()
# Split the data for evaluation
X_train, X_test, y_train, y_test = data_loader.get_data_split()
# Oversample the train data
X_train, y_train = data_loader.oversample(X_train, y_train)
print(X_train.shape)
print(X_test.shape)

# %% Fit blackbox model
rf = RandomForestClassifier()
rf.fit(X_train, y_train)
y_pred = rf.predict(X_test)
print(f"F1 Score {f1_score(y_test, y_pred, average='macro')}")
print(f"Accuracy {accuracy_score(y_test, y_pred)}")

# %% Apply lime
# Initilize Lime for Tabular data
lime = LimeTabular(data=X_train, 
                   random_state=1)

# Get local explanations
# 将 predict_fn 传递给 explain_local
lime_local = lime.explain_local(X_test[-20:], 
                                y_test[-20:], 
                                name='LIME',
                                predict_fn=rf.predict_proba)

show(lime_local)

# %%

通过此更改,你应该能够在不出现 TypeError 的情况下运行代码。

标签:python,interpreter,lime
From: 78802948

相关文章

  • AttributeError:'int'对象没有属性'index'(python)
    我正在Python上进行“猜单词”,但我无法弄清楚这个错误。AttributeError:'int'objecthasnoattribute'index'(python)它在线上给了我一个错误letterIndex=word.index(guess)defcheckLetter(word):blanks='_'*len(str(word))print('W......
  • 尝试在Python中使用for循环来输出大于或等于序列中的数字
    这是我的Python代码:largest_so_far=-1print('before',largest_so_far)forthe_numin[9,41,12,3,74,15]:ifthe_num>largest_so_far:largest_so_far=the_numprint(largest_so_far,'isbiggerthan',the_num)......
  • 如何在 wxPython 的 for 循环中添加文本输入框?
    我是wxPython的新手,正在开发一个带有GUI的基本程序,让用户标记图像。现在,当用户单击“导入”按钮时,他们可以选择一个目录。然后,代码使用matplotlib在for循环中显示该目录中的每个图像。但是,我不知道如何在for循环中访问用户输入。这就是该函数现在的样子:importmatplo......
  • 【Python】字母 Rangoli 图案
    一、题目YouaregivenanintegerN.YourtaskistoprintanalphabetrangoliofsizeN.(RangoliisaformofIndianfolkartbasedoncreationofpatterns.)Differentsizesofalphabetrangoliareshownbelow:#size3----c------c-b-c--c-b-a-b-c--c......
  • python 闭包、装饰器
    一、闭包:1.外部函数嵌套内部函数 2.外部函数返回内部函数 3.内部函数可以访问外部函数局部变量         闭包(Closure)是指在一个函数内部定义的函数,并且内部函数可以访问外部函数的局部变量,即使外部函数已经执行完毕,这种现象称为闭包。在Python中,闭包常常用......
  • 掌握 IPython %%time 魔法命令:高效测量代码块执行时间
    引言在编程和数据分析中,了解代码的执行时间是优化性能的关键步骤。IPython,作为一个强大的交互式计算环境,提供了多种工具来帮助用户测量和优化代码。其中,%%time魔法命令是IPython中用来测量代码块执行时间的便捷工具。本文将详细介绍%%time魔法命令的使用方法,并通过一......
  • 探索 IPython 中的 %%javascript 魔法命令:运行 JavaScript 代码的秘籍
    引言IPython是一个强大的交互式计算环境,它不仅支持Python语言,还通过各种魔法命令扩展了其功能。其中,%%javascript魔法命令是IPython扩展中一个非常有趣的特性,它允许用户在IPython环境中直接运行JavaScript代码。这对于需要在数据科学和科学计算中使用JavaScript......
  • pythonasm库分析,看看你和自学编程小学生的差距
    下面是pythonasm.asm库的源代码fromkeystoneimport*fromcapstoneimport*assembly_instructions=[]#储存汇编指令的列表#汇编指令写入列表defmov(reg1,reg2):assembly_instructions.append(f"mov{reg1},{reg2}")defdb(value):assembly_instructio......
  • 【Python系列】Python 中的垃圾收集:深入理解与实践
    ......
  • Python酷库之旅-第三方库Pandas(050)
    目录一、用法精讲181、pandas.Series.var方法181-1、语法181-2、参数181-3、功能181-4、返回值181-5、说明181-6、用法181-6-1、数据准备181-6-2、代码示例181-6-3、结果输出182、pandas.Series.kurtosis方法182-1、语法182-2、参数182-3、功能182-4、返回值1......