pynverse inversefunc:当原始函数有多个变量时,如何得到可逆函数?

2024-04-20 05:33:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个这样的函数

def calculation_function(x,prefecture_name):
    # here, I omit the computation
return y

x和y之间的关系如下:

    y = calculation_function(x,'A')

enter image description here

现在,我想计算可逆函数。我得到了一些y值和相应的u名称,我想估计x的值。 我的密码是:

from pynverse import inversefunc

invcube = inversefunc(calculation_function)
aaa = invcube(0.05778898865521945,'A')

但结果显示

"calculation_function() missing 1 required positional argument: 'prefecture_name'"

如何将“地区名称”值传递给invcube? 非常感谢


Tags: the函数name名称returnhere关系def
1条回答
网友
1楼 · 发布于 2024-04-20 05:33:52

您可能希望在反转过程中忽略第二个参数,因为它被称为name。您可以使用functools.partial或简单的lambda执行此操作:

inversefunc(partial(calculation_function, prefecture_name="a"))
inversefunc(lambda x: calculation_function(x,"a"))

相关问题 更多 >