如何找到拟合指数函数的 x?

How for find x for a fitted exponential function?

fp.append(np.polyfit(train_x, train_y, 2))
f.append(np.poly1d(fp))
print(np.poly1d(fp))
threshold = fsolve(f, 50)

以上代码成功找到了 y=50 的 x 值。但是当我尝试对拟合指数函数执行相同操作时,我无法理解该怎么做。

def f_exp(x, a, b, c):
    y = a * np.exp(-b * x) + c
    return y
popt, pcov = curve_fit(f_exp, train_x, train_y)
print(popt)
threshold = fsolve(f_exp, 50) fails with  :TypeError: f_exp() missing 3 required positional arguments: 'a', 'b', and 'c'

如果我添加 *popt 然后我得到

threshold = fsolve(f_exp(*popt), 50) fails with: TypeError: f_exp() missing 1 required positional argument: 'c'

我假设我需要添加 x 值,但这是我试图找到的值...无论如何,添加一些值而不是 x 会导致另一个错误:

threshold = fsolve(f_exp(1, *popt), 50) fails with: TypeError: 'numpy.float64' object is not callable

我想您需要将具有优化参数的 f_exp 函数传递给 fsolve(即,将 a、b 和 c 参数设置为从 curve_fit 获得的值)。为此,您可以使用 functools.partial 函数:

popt, pcov = curve_fit(f_exp, train_x, train_y)
print(popt)
import functools
# preparing arguments
kwargs = dict(zip(['a', 'b', 'c'], popt))
optimized_f_exp = functools.partial(f_exp, **kwargs)
threshold = fsolve(optimized_f_exp, 50)

我们在这里所做的基本上是通过将原始函数的 abc args 部分固定为 [=18] 来创建一个新函数 optimized_f_exp =](它为什么叫 partial)。