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

问题描述 投票:0回答:1

我有这样的功能

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的一些值和相应的prefecture_name,我想估计x的值。我的代码是:

from pynverse import inversefunc

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

但结果显示

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

如何将'prefecture_name'值传递给invcube?谢谢千差万别!

python
1个回答
0
投票
您可能想在倒置过程中省略第二个参数,因为它称为name。您可以使用functools.partial或简单的lambda:

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

© www.soinside.com 2019 - 2024. All rights reserved.