约束np.polyfit [英] Constrained np.polyfit

查看:199
本文介绍了约束np.polyfit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对一些实验数据进行二次拟合,并在numpy中使用polyfit.我正在寻找一条凹曲线,因此要确保二次项的系数为负,而且拟合本身也经过加权,因为这些点上有一些权重.有没有简单的方法可以做到这一点?谢谢.

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.

推荐答案

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 

这将始终使您对x ** 2的系数为负.

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

这篇关于约束np.polyfit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆