我有一个只有5个数据点的散点图,我想拟合一条曲线。我尝试过polyfit和下面的代码,但是都不能用这么少的数据点生成曲线
def func(x, a, b, c):
return a * np.exp(-b * x) + c
plt.plot(xdata, ydata, ".", label="Data");
optimizedParameters, pcov = opt.curve_fit(func, xdata, ydata);
plt.plot(xdata, func(xdata, *optimizedParameters), label="fit");
附件是一个情节的例子,以及一个我试图产生的曲线的例子(为糟糕的绘图道歉)。谢谢!在
你必须选择你想要的适合曲线运动员之后。从你的绘画的外观看,你似乎在试图把它塑造成某种对数。
这是对数回归的图片:
对数回归的形式为y=A+B ln(x)。 这基本上是线性回归拟合,而不是拟合y与x 我们正在尝试适合y和ln(x)。
所以,您可以只需要获取数据集中点的x值的自然日志,并在它上执行线性回归算法。屈服系数为y=A+B ln(x)的A和B。
图片学分: http://mathworld.wolfram.com/LeastSquaresFittingLogarithmic.html
编辑:正如詹姆斯·菲利普斯在回答中指出的,也可以用y=Ax^(-B)+C的形式对曲线进行建模,因为对于如此少的点,如果图形具有水平渐近线或总是增长但减速,则无法确定。很多曲线都可能(例如y=A*B^(-x)+C可能是另一条曲线),但您需要选择之后的数据模型。
下面是一个使用注释中的数据的Python fitter示例,它适合于一个多形类型的等式。在这个例子中,不需要记录数据。这里X轴是按十年对数标度绘制的。请注意,示例代码中的数据是浮点数形式。在
指数函数不适合你的数据。考虑另一个建模函数。在
给定
编码
根据@Allan Lago的对数模型:
^{pr2}$使用@James Phillips的“多向性”模型:
相关问题 更多 >
编程相关推荐