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

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

问题描述

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

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

有人可以解释如何做吗?

解决方案

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

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

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()

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.

My points: np.array([(1, 1), (2, 4), (3, 1), (9, 3)])

Can anybody explain how to do that?

解决方案

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.

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天全站免登陆