1
votes

How to fit a non linear data's using scipy.optimize import curve_fit in Python using following 3 methods:

  1. Gaussian.
  2. Lorentz fit.
  3. 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.

1

1 Answers

6
votes

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.

enter image description here