简体   繁体   English

Scipy sigmoid 曲线拟合

[英]Scipy sigmoid curve fitting

I have some data points and would like to find a fitting function, I guess a cumulative Gaussian sigmoid function would fit, but I don't really know how to realize that.我有一些数据点,想找到一个拟合函数,我猜累积高斯 sigmoid 函数会拟合,但我真的不知道如何实现。

This is what I have right now:这就是我现在所拥有的:

import numpy as np
import pylab
from scipy.optimize import curve_fit

def sigmoid(x, a, b):
     y = 1 / (1 + np.exp(-b*(x-a)))
     return y

xdata = np.array([400, 600, 800, 1000, 1200, 1400, 1600])
ydata = np.array([0, 0, 0.13, 0.35, 0.75, 0.89, 0.91])
         
popt, pcov = curve_fit(sigmoid, xdata, ydata)
print(popt)

x = np.linspace(-1, 2000, 50)
y = sigmoid(x, *popt)

pylab.plot(xdata, ydata, 'o', label='data')
pylab.plot(x,y, label='fit')
pylab.ylim(0, 1.05)
pylab.legend(loc='best')
pylab.show()

But I get the following warning:但我收到以下警告:

.../scipy/optimize/minpack.py:779: OptimizeWarning: Covariance of the parameters could not be estimated category=OptimizeWarning) .../scipy/optimize/minpack.py:779: OptimizeWarning: 无法估计参数的协方差 category=OptimizeWarning)

Can anyone help?任何人都可以帮忙吗? I'm also open for any other possibilities to do it!我也愿意接受任何其他可能性! I just need a curve fit in any way to this data.我只需要以任何方式拟合这些数据的曲线。

You could set some reasonable bounds for parameters, for example, doing您可以为参数设置一些合理的界限,例如,执行

def fsigmoid(x, a, b):
    return 1.0 / (1.0 + np.exp(-a*(x-b)))

popt, pcov = curve_fit(fsigmoid, xdata, ydata, method='dogbox', bounds=([0., 600.],[0.01, 1200.]))

I've got output我有输出

[7.27380294e-03 1.07431197e+03]

and curve looks like和曲线看起来像

在此处输入图片说明

First point at (400,0) was removed as useless. (400,0) 处的第一个点被删除为无用。 You could add it, though result won't change much...您可以添加它,但结果不会有太大变化......

UPDATE更新

Note, that bounds are set as ([low_a,low_b],[high_a,high_b]), so I asked for scale to be within [0...0.01] and location to be within [600...1200]请注意,边界设置为 ([low_a,low_b],[high_a,high_b]),所以我要求比例在 [0...0.01] 内,位置在 [600...1200] 内

You may have noticed the resulting fit is completely incorrect.您可能已经注意到结果拟合完全不正确。 Try passing some decent initial parameters to curve_fit , with the p0 argument:尝试使用p0参数将一些不错的初始参数传递给curve_fit

popt, pcov = curve_fit(sigmoid, xdata, ydata, p0=[1000, 0.001])

should give a much better fit, and probably no warning either.应该提供更好的拟合,并且可能也没有警告。

(The default starting parameters are [1, 1]; that is too far from the actual parameters to obtain a good fit.) (默认的起始参数是 [1, 1];这与实际参数相差太远,无法获得良好的拟合。)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM