python numpy/scipy 曲线拟合 [英] python numpy/scipy curve fitting

查看:45
本文介绍了python numpy/scipy 曲线拟合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些要点,我正在尝试为这些点拟合曲线.我知道存在 scipy.optimize.curve_fit 函数,但我不了解文档,即如何使用该函数.

I have some points and I am trying to fit curve for this points. I know that there exist scipy.optimize.curve_fit function, but I do not understand documentation, i.e how to use this function.

我的观点:np.array([(1, 1), (2, 4), (3, 1), (9, 3)])

谁能解释一下怎么做?

推荐答案

我建议你从简单的多项式拟合开始,scipy.optimize.curve_fit 尝试拟合您必须知道的函数 f点集.

I suggest you to start with simple polynomial fit, scipy.optimize.curve_fit tries to fit a function f that you must know to a set of points.

这是一个使用 numpy.polyfitpoly1d 的简单 3 度多项式拟合,第一个执行最小二乘多项式拟合,第二个计算新点:

This is a simple 3 degree polynomial fit using numpy.polyfit and poly1d, the first performs a least squares polynomial fit and the second calculates the new points:

import numpy as np
import matplotlib.pyplot as plt

points = np.array([(1, 1), (2, 4), (3, 1), (9, 3)])
# get x and y vectors
x = points[:,0]
y = points[:,1]

# calculate polynomial
z = np.polyfit(x, y, 3)
f = np.poly1d(z)

# calculate new x's and y's
x_new = np.linspace(x[0], x[-1], 50)
y_new = f(x_new)

plt.plot(x,y,'o', x_new, y_new)
plt.xlim([x[0]-1, x[-1] + 1 ])
plt.show()

这篇关于python numpy/scipy 曲线拟合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆