问题描述
我有一些数据点,想找到一个拟合函数,我想一个累积的高斯S型函数就可以了,但我真的不知道如何实现。
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.
这就是我现在拥有的:
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()
但我收到以下警告:
... / scipy / optimize / minpack.py:779:O ptimizeWarning:无法估计参数的协方差
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.]))
我已经输出了
[7.27380294e-03 1.07431197e+03]
曲线看起来像
第一个点是(400, 0)被删除为无用。您可以添加它,尽管结果不会有太大变化...
First point at (400,0) was removed as useless. You could add it, though result won't change much...
UPDATE
请注意,被设置为[[low_a,low_b],[high_a,high_b]),所以我要求比例尺在[0 ... 0.01]内,位置在[600 ... 1200]
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]
这篇关于Scipy S型曲线拟合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!