使用python进行高斯拟合

问题描述

嗨,很抱歉,如果这是一个菜鸟问题。我是python的新手。我试图在数据集上拟合高斯曲线,但我不确定哪里出错了。我正在跟踪一些我在网上找到的示例,但是它不起作用。请检查图像以查看输出中发生了什么。

fit is just coming as straight line

我将数据存储在excel中,并按如下所示导入了它:

import numpy as np
from scipy.optimize import curve_fit
import pandas as pd

df = pd.read_csv(r'C:\Users\B.d\Documents\interpolate,matplotlib and other example tutorials\ExtendedAxialString4.csv')

x =df.iloc[:,0] #position

y =df.iloc[:,1]   #value 

#then for the gaussian

n = len(x)

mean = sum(x*y)/n                  

sigma = sum(y*(x-mean)**2)/n       


def gaus(x,a,x0,sigma):

    return a*exp(-(x-x0)**2/(2*sigma**2))

popt,pcov = curve_fit(gaus,x,y,p0=[1,mean,sigma])

#plt.title('Gaussian fit,$f(x) = A e^{(-(x-x_0)^2/(2sigma^2))}$')


plt.plot(x,'b+:',label='data')

plt.plot(x,gaus(x,*popt),'ro:',label='fit')

plt.legend()

plt.title('fig. 3 - Axial deformation')

plt.xlabel('numbering')

plt.ylabel('deformation(mm)')

plt.show() 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)