Python Basic Guassian fit from Scratch| Scipy Curve_fit| Matplotlib|Numpy| PYTHON FOR DATA SCIENCE

preview_player
Показать описание
This is a beginner video about gaussian fitting Scipy Curve_fit.
Welcome to this tutorial on fitting a Gaussian curve to a scatter plot using Scipy!

Scipy is a powerful library for scientific computing and provides various tools for curve fitting. In this tutorial, we will show you how to use Scipy to fit a Gaussian curve to a scatter plot.

We will start by generating some random data and plotting it as a scatter plot using Matplotlib. We will then use Scipy to fit a Gaussian curve to the data and plot the curve on top of the scatter plot.

To accomplish this, we will use the curve_fit function from Scipy's optimize module, which uses the least-squares method to fit a function to data.

We will also discuss the various parameters of the curve_fit function and how to choose an appropriate initial guess for the parameters.

By the end of this tutorial, you will have a clear understanding of how to use Scipy to fit a Gaussian curve to a scatter plot, and how to interpret the results.

So, whether you are a data analyst, a scientist, or simply interested in learning more about data analysis using Python, this tutorial is for you! Don't forget to like, comment, and subscribe to our channel for more exciting tutorials on data analysis and machine learning using Python.

-------------------PYTHON CODE------------------------
# import modules
import numpy as np
# add interactive control to plot
%matplotlib widget

# create x,y data
len(x_data) # gives length of data
# plot scatter

# now we have to fit these data as per gaussian distribution = Amp* exp(-x**2/a**2)
# define a function call it gauss

def gauss(x_data,amp,mean,sigma):

# fit gaussian with parameters and determine covariance matrxi 3 x3
popt,pcov= curve_fit(gauss,x_data,y_data,p0=[95,1,1]) # po is guess initial values
# now plot gauss fitted


# add title

# add x,y label

# you can also show grid in plot

Рекомендации по теме
Комментарии
Автор


# import modules
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# add interactive control to plot
%matplotlib widget

# create x, y data
x_data= np.linspace(-10, 10, 21) # produces 20 data points from -10 to 10
len(x_data) # gives length of data
y_data= np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 100, 95, 90, 80, 70, 60, 50, 40, 30, 20, 10])
# plot scatter
plt.scatter(x_data, y_data, label='data', color='red', s=200)

# now we have to fit these data as per gaussian distribution = Amp* exp(-x**2/a**2)
# define a function call it gauss

def gauss(x_data, amp, mean, sigma):
return

# fit gaussian with parameters and determine covariance matrxi 3 x3
popt, pcov= curve_fit(gauss, x_data, y_data, p0=[95, 1, 1]) # po is guess initial values
# now plot gauss fitted

plt.plot(x_data, gauss(x_data, *popt), color='k', linestyle='dashed', label='gaussin fit', lw=5)

# add title
plt.title('Gaussian fit', fontsize=20, color='green')

# add x, y label
plt.xlabel('x data')
plt.ylabel('y data')

# you can also show grid in plot
plt.grid()




plt.legend(loc='best') # you can control location of legend

plt.show()

Astro_Jyoti