如何在python中拟合非线性数据 [英] How to fit non-linear data's in python
问题描述
如何使用以下3种方法使用 scipy.optimize在
Python
中使用 scipy.optimize import curve_fit
拟合非线性数据:
- 高斯.
- 洛伦兹合身.
- 朗缪尔适合度.
我只可以从我的数据文件进行链接和绘图.
从matplotlib中的 导入pyplot作为plt从matplotlib导入样式将numpy导入为np导入pylab从scipy.optimize导入curve_fitstyle.use('ggplot')数据= np.genfromtxt('D:\ csvtrail3.csv',分隔符=',',跳过行= 1)x = data [:,0]y = data [:,1]data.fit_lorentzians()plt.plot(x,y)plt.title('史诗图')plt.ylabel('Y轴')plt.xlabel('X轴')plt.show()
请建议我如何为该数据归档行.我不想直接拟合.我想要顺滑.
通常,一旦我们知道最适合我们数据集的方程式, scipy.optimize.curve_fit
就会起作用.由于要拟合遵循高斯,洛伦兹等分布的数据集,因此可以通过提供它们的特定方程式来实现.
只是一个小例子:
将numpy导入为np导入matplotlib.pyplot作为plt从scipy.optimize导入curve_fit将numpy导入为npxdata = np.array([-2,-1.64,-1.33,-0.7,0,0.45,1.2,1.64,2.32,2.9])ydata = np.array([0.69,0.70,0.69,1.0,1.9,2.4,1.9,0.9,-0.7,-1.4])def func(x,p1,p2):返回p1 * np.cos(p2 * x)+ p2 * np.sin(p1 * x)#在此提供p0的初始参数,然后Python对其进行迭代#找到最合适的popt,pcov = curve_fit(func,xdata,ydata,p0 =(1.0,0.3))print(popt)#包含两个最合适的参数#执行平方和p1 = popt [0]p2 = popt [1]残差= ydata-func(xdata,p1,p2)fres =总和(残差** 2)打印(fres)xaxis = np.linspace(-2,3,100)#我们可以使用xdata进行绘制,但拟合效果不好curve_y = func(xaxis,p1,p2)plt.plot(xdata,ydata,'*')plt.plot(xaxis,curve_y,'-')plt.show()
以上内容是针对我的特定情况的,其中我只使用了适合的
How to fit a non linear data's using scipy.optimize import curve_fit
in Python
using following 3 methods:
- Gaussian.
- Lorentz fit.
- Langmuir fit.
I am just able to link and plot from my data file.
from matplotlib import pyplot as plt
from matplotlib import style
import numpy as np
import pylab
from scipy.optimize import curve_fit
style.use('ggplot')
data = np.genfromtxt('D:\csvtrail3.csv', delimiter=',', skiprows=1)
x=data[:,0]
y=data[:,1]
data.fit_lorentzians()
plt.plot(x, y)
plt.title('Epic chart')
plt.ylabel('Y Axis')
plt.xlabel('X Axis')
plt.show()
Kindly suggest me how to file the line for this data. I dont want straight fitting. I want smooth fitting.
In general scipy.optimize.curve_fit
works once we know the equation that best fits our dataset. Since you want to fit a dataset that follows a Gaussian, Lorentz etc, distributions, you can do so by providing their specific equations.
Just as a small example:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np
xdata = np.array([-2,-1.64,-1.33,-0.7,0,0.45,1.2,1.64,2.32,2.9])
ydata = np.array([0.69,0.70,0.69,1.0,1.9,2.4,1.9,0.9,-0.7,-1.4])
def func(x, p1,p2):
return p1*np.cos(p2*x) + p2*np.sin(p1*x)
# Here you give the initial parameters for p0 which Python then iterates over
# to find the best fit
popt, pcov = curve_fit(func,xdata,ydata,p0=(1.0,0.3))
print(popt) # This contains your two best fit parameters
# Performing sum of squares
p1 = popt[0]
p2 = popt[1]
residuals = ydata - func(xdata,p1,p2)
fres = sum(residuals**2)
print(fres)
xaxis = np.linspace(-2,3,100) # we can plot with xdata, but fit will not look good
curve_y = func(xaxis,p1,p2)
plt.plot(xdata,ydata,'*')
plt.plot(xaxis,curve_y,'-')
plt.show()
The above was for my specific case, wherein I just used a Harmonic addition formula that fit's my dataset. You can change accordingly, either a Gaussian equation, or any other equation, by providing it in the func
definition.
Your parameters will vary accordingly. If it is a Gaussian distribution, you will have your sigma
(standard deviation) and mean
as the unknown parameters.
这篇关于如何在python中拟合非线性数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!