import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def func(x, a, b, c, d):
return 1/(1+np.exp(-x*a+b)) # + c*x + d
xdata = np.linspace(0, 4, 50)
y = func(xdata, 2.5, 1.3, 0.5,0.2)
rng = np.random
y_noise = 0.1 * rng.normal(size=xdata.size)
ydata = y + y_noise
plt.plot(xdata, ydata, 'b-', label='data')
popt, pcov = curve_fit(func, xdata, ydata)
popt
# array([2.56274217, 1.37268521, 0.47427475])
plt.plot(xdata, func(xdata, *popt), 'r-',
label='fit: a=%5.3f, b=%5.3f, c=%5.3f, d=%5.3f' % tuple(popt))
popt, pcov = curve_fit(func, xdata, ydata, bounds=(0, [3., 1.2, 0.5,0.2]))
plt.plot(xdata, func(xdata, *popt), 'g--',
label='fit: a=%5.3f, b=%5.3f, c=%5.3f, d=%5.3f' % tuple(popt))
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
少量数据集的情况通过scipy优化来拟合函数
原创
©著作权归作者所有:来自51CTO博客作者mtj66的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:结合业务场景做好特征工程
下一篇:逆地理编码-离线版-part3
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
数据集 | 婚姻情况数据集
该数据集记录了十次该社会实验的数据。
数据集 数据 大数据