如何使用lmfit模型来解决/找到给定y值的x值?



我知道如何使用模型从给定的x值中获得y值,例如

import numpy as np
import lmfit
mod=lmfit.models.ExponentialModel()
x_test = np.array([100,40,200, 500])
y_test = np.array([0.7, 0.85, 0.5, 0.3])
pars = mod.guess(y_test, x_test)
out = mod.fit(y_test, pars, x=x_test)
x0 = 10
y0 = out.eval(x=x0)

但是我如何使用模型来找到给定y值的x值?我试过了:

y0 = 10
x0 = out.eval(y=y0)

这似乎不是我想要的。

好,x为独立值。函数y(x)可以有多个x值,其中y=y0或至少y ~= y0。或者可能没有任何地方适合y=y0.

您可能会发现使用np.where()和/或np.argmin()来查找靠近函数取某个值的点是有用的。我经常使用这些作为辅助函数:

def index_of(array, value):
"""
return index of array *at or below* value
returns 0 if value < min(array)
>> ix = index_of(array, value)
Arguments
---------
array  (ndarray-like):  array to find index in
value  (float): value to find index of
Returns
-------
integer for index in array at or below value
"""
if value < min(array):
return 0
return max(np.where(array<=value)[0])
def index_nearest(array, value):
"""
return index of array *nearest* to value
>>> ix = index_nearest(array, value)
Arguments
---------
array  (ndarray-like):  array to find index in
value  (float): value to find index of
Returns
-------
integer for index in array nearest value
"""
return np.abs(array-value).argmin()

也许这些对你会有帮助。

最新更新