[英]finding non linear equation python scipy
I am new to analytics , I am looking for a solution to find a model to solve non linear equation of the form Y=a(X1^b) + c(X2^d) + e
( where X1 , X2 are independent variables) Below is a full set , unfortunately we dont have much observations , all we need is any simple fit.But this data does not have any outliers , every observations has to be considered. 我是分析新手,我正在寻找一种解决方案,以找到一种模型来求解形式为
Y=a(X1^b) + c(X2^d) + e
(其中X1,X2是自变量)的非线性方程以下是一组完整的数据,很遗憾,我们没有太多的观察值,我们只需要简单的拟合即可。但是此数据没有任何异常值,必须考虑每个观察值。
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np
import sympy as sym
x1=np.array([217,160,97,75])
x2=np.array([5.319751464,6.88536761,5.319751464,5.319751464])
x3=np.array([143.420635344,36.7384534912,23.420635344,1.420635344])
y=np.array([14,7,7,1])
def func(X, a, b, c ,d , e ):
x1,x2 = X
return a*x1**b + c*x2**d + e
popt, pcov = curve_fit(func, (x1,x2), y)
plt.plot(y, func((x1,x2), *popt), label="Fitted Curve")
plt.legend(loc='upper left')
plt.show()
but running curve_fit gives me error saying that the 但是运行curve_fit给我错误说
TypeError: Improper input: N=5 must not exceed M=4
TypeError:输入错误:N = 5不能超过M = 4
Then i had to add few more dummy inputs as observations of almost similar values adding decimal point difference which resulted in error 然后,我不得不添加更多的虚拟输入,以观察几乎相似的值,并加上小数点差,这会导致错误
x1=np.array([217,160,97,75,76,219])
x2=np.array([5.319751464,6.88536761,5.319751464,5.319751464,5.319751464,5.319751464])
x3=np.array([143.420635344,36.7384534912,23.420635344,1.420635344,1.420635344,143.420635344])
y=np.array([14,7,7,1,1,14])
RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 1200.
RuntimeError:找不到最佳参数:函数调用的数量已达到maxfev = 1200。
Then I had to remove variable d and keep function as 然后我必须删除变量d并保持函数为
def func(X, a, b, c ,e ):
x1,x2 = X
return a*x1**b + c*x2 + e
Finally it did run but again with below warning , but results are not good 最终它确实运行了,但再次出现以下警告,但效果不佳
RuntimeWarning: overflow encountered in power
RuntimeWarning:电源遇到溢出
Note that 注意
x3 = max(x2 - {(x1^2)*2.6},0)
x3 = max(x2-{(x1 ^ 2)* 2.6},0)
and solving 和解决
y=a*(x3^b) gives a=0.89 and b=0.58 with r2=0.98 and error=0.19 which is the best one i could get so far
y = a *(x3 ^ b)给出a = 0.89和b = 0.58,其中r2 = 0.98和error = 0.19,这是到目前为止我能得到的最好的结果
But i would like to have the result in a generalised form without me trying to equate a relation. 但是我想以广义形式获得结果,而无需尝试将一个关系等同起来。 Because based on data set ,the function x3=f(x1,x2) can change and it is not a fixed equation for all cases.
因为基于数据集,函数x3 = f(x1,x2)可以更改,并且并非在所有情况下都是固定的方程式。
A very good approach to get acceptable fit results is the use of initial guesses of the free parameters. 获得可接受的拟合结果的一种非常好的方法是使用自由参数的初始猜测。
curve_fit
takes a p0
argument. curve_fit
采用p0
参数。 If your data does not vary too much between two data sets, this should be a proper way. 如果您的数据在两个数据集之间的差异不大,这应该是一种正确的方法。 In my experience, taking some afford to guess good start values
p0
and set limits by the parameter bounds
is a good approach. 以我的经验,可以让一些人猜测良好的起始值
p0
并通过参数bounds
设置极限是一个很好的方法。 Otherwise you could try to interpolate the data, if you don't have to know the mathematical relation. 否则,如果您不必了解数学关系,则可以尝试对数据进行插值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.