Constrained np.polyfit

﹥>﹥吖頭↗ 提交于 2020-05-27 02:15:00

问题


I am trying to fit a quadratic to some experimental data and using polyfit in numpy. I am looking to get a concave curve, and hence want to make sure that the coefficient of the quadratic term is negative, also the fit itself is weighted, as in there are some weights on the points. Is there an easy way to do that? Thanks.


回答1:


The use of weights is described here (numpy.polyfit). Basically, you need a weight vector with the same length as x and y.

To avoid the wrong sign in the coefficient, you could use a fit function definition like

def fitfunc(x,a,b,c):
    return -1 * abs(a) * x**2 + b * x + c 

This will give you a negative coefficient for x**2 at all times.




回答2:


You can use curve_fit .

Or you can run polyfit with rank 2 and if the last coefficient is bigger than 0. run again linear polyfit (polyfit with rank 1)



来源:https://stackoverflow.com/questions/26785173/constrained-np-polyfit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!