栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

用numpy / scipy拟合的6度曲线

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用numpy / scipy拟合的6度曲线

您可以使用

scipy.optimize.curve_fit
(在合理的情况下)使您想要的任何功能适合数据。该功能的签名是

curve_fit(f, xdata, ydata, p0=None, sigma=None, **kw)

并使用非线性最小二乘拟合将函数拟合到f数据ydata(xdata)。在您的情况下,我会尝试类似的方法:

import numpyfrom scipy.optimize import curve_fitimport matplotlib.pyplot as pltdef _polynomial(x, *p):    """Polynomial fitting function of arbitrary degree."""    poly = 0.    for i, n in enumerate(p):        poly += n * x**i    return poly# Define some test data:x = numpy.linspace(0., numpy.pi)y = numpy.cos(x) + 0.05 * numpy.random.normal(size=len(x))# p0 is the initial guess for the fitting coefficients, set the length# of this to be the order of the polynomial you want to fit. Here I# have set all the initial guesses to 1., you may have a better idea of# what values to expect based on your data.p0 = numpy.ones(6,)coeff, var_matrix = curve_fit(_polynomial, x, y, p0=p0)yfit = [_polynomial(xx, *tuple(coeff)) for xx in x] # I'm sure there is a better        # way of doing thisplt.plot(x, y, label='Test data')plt.plot(x, yfit, label='fitted data')plt.show()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/650767.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号