简体   繁体   English

用等于 0 的 function 拟合数据,无法转换为 f(x) = x 的形式

[英]Fit data with a function that equals 0 and could not be converted to the form f(x) = x

I have 2 columns and 31 rows in a pandas dataframe. I want to plot this x,y data and fit them to a complex function with 4 parameters.我在 pandas dataframe 中有 2 列和 31 行。我想要 plot 这个 x,y 数据并将它们拟合到具有 4 个参数的复杂 function。 The function looks something like this. function 看起来像这样。 The function has to be 0 function 必须为 0

# Data:
T,p = df["T"], df["p"] #31 rows

# known constants: a,b,Ta,c0,x

def c(T,v,VP,a=...,b=...,Ta=...,c0=...):
     c = c0 + a*(T-Ta) + b*t_r(T)**v/(t_r(T)**v-K(T,VP))
     return c

# t_r and K are other functions

def function(p,T,p0,v,N,VP,a,b,c0,x):
    return np.log(1-p) + p + (x*c(T,v,VP))*p**2 + (p0/N)*(R-0.5*R**3) # =0

I am interested in fitting the parameters N,p0,Vp我有兴趣拟合参数 N,p0,Vp

I tried to use Lmfit and changed my function to -> function(params,T,p)我尝试使用 Lmfit 并将我的 function 更改为 -> function(params,T,p)

from Lmfit import minimize, Parameters

## add Parameters 
params = Parameters() ##Class with a list of parameters

# add all constant Parameters with vary = False
params.add("a", value=...,vary=False)
...

## add variables to fit with vary = True, limits with min,max 
params.add("N",value=..., vary=True,min=0,max=...)
...


output = minimize(function,params) #Fit Results
output.params.pretty_print() #Show Results

Now I acquired the parameters, but I want to check if this makes sense by plot(T,p) for a more continous array, like:现在我获得了参数,但我想通过 plot(T,p) 来检查这对于更连续的数组是否有意义,例如:

Ts = np.linspace(10,60,1000) # x-array
ps = ...    ?                # y-array
plt.plot(Ts,ps,label="Fit")  # Plot Data

How could I obtain a function to calculate p for T on each point to plot it?我怎样才能得到一个 function 来计算 p for T 的每个点到 plot 呢?

I find an answer myself.我自己找到答案。

First I wrap my function in a way that the y-value p is the first argument.首先,我将 function 包装成 y 值 p 是第一个参数。 And I used lmfit parameter class as arguments. Lmfit Parameters are basically dictionaries.而我用的lmfit参数class为arguments。lmfit参数基本都是字典。

p_solvable = lambda p,T,parameter : function(p,T,parameter["p0"],parameter["v"],...)

Then I solve the equation by scipy.optimize.root_scalar with the brentq method.然后我用 scipy.optimize.root_scalar 用 brentq 方法求解方程。 The brentq method needs Brackets where the signs are changing. brentq 方法需要符号变化的括号。 I chose 1 as lower limit so np.log(1-p > 0) is defined and just the doubled maximum as upper limit.我选择 1 作为下限,因此 np.log(1-p > 0) 被定义并且只是最大值的两倍作为上限。

p_max = np.max(p)

def p(T):
   P_Init = [root_scalar(p_solvable,args=(T,parameter), method="brentq", bracket=[1,p_max*2]).root for T in T]
   return P_Init

Now I have a function where I can input f(x) = y and fit it with lmfit or scipy curve_fit现在我有一个 function,我可以在其中输入 f(x) = y 并使用 lmfit 或 scipy curve_fit 对其进行拟合

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找到 1/x 形式的 function 来拟合数据 python - find a function of the form 1/x like to fit data python 如果 id 等于 X,则应用函数 - Apply a function if id equals X Python | 以(1 / A)e ^(-x / A)形式绘制和拟合数据拟合,其中A是常数 - Python | Plotting & fitting a data fit in the form (1/A)e^(-x/A) in which A is a constant Python中是否有一个函数可以将数据向量放入(X,Y)表中? - Is there a function in Python to fit data vector into a (X,Y) table? 如何将数据点拟合到 Python 中的 function e**x? - How do I fit data points to the function e**x in Python? "为什么我尝试将 tanh(x) 函数拟合到数据效果不佳?" - Why is my attempt to fit a tanh(x) function to data not working well? 如何为 numpy 数组“x”实现 function f(x) = { x^2, x&gt;0 and -x^2, x&lt;0 }? - How do I implement the function f(x) = { x^2, x>0 and -x^2 , x<0 } for a numpy array 'x'? 适合y = x ** a形式的回归与numpy - fit regression of the form y = x ** a with numpy 警告:tensorflow:AutoGraph 无法转换<function _preprocess at 0x7f8ff80cd160>并将按原样运行</function> - WARNING:tensorflow:AutoGraph could not transform <function _preprocess at 0x7f8ff80cd160> and will run it as-is 从F(x)函数填充两个向量 - Filling Two Vectors from F(x) Function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM