简体   繁体   English

将半高斯曲线/归一化到数据点

[英]Fitting half a Gaussian curve/ normalization to Data points

So I have two lists of data, which I can plot in a scatter plot, as such: 因此,我有两个数据列表,可以将它们绘制在散点图中,如下所示:

from matplotlib import pyplot as plt
x = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
y = [22.4155688819,22.3936180362,22.3177538001,22.1924849792,21.7721194577,21.1590235248,20.6670446864,20.4996957642,20.4260953411,20.3595072628,20.3926201626,20.6023149681,21.1694961343,22.1077417713,23.8270366414,26.5355924353,31.3179807276,42.7871637946,61.9639549412,84.7710953311]

plt.scatter(degrees,RMS_one_image)

This gives you a plot that looks like a Gaussian distribution, which is good as it should- 这样可以使您得到一个看起来像高斯分布的图,这应该很好, 要绘制的数据

My issue is however I am trying to fit a Gaussian distribution to this, and failing miserably because a. 但是,我的问题是我正在尝试使它适合高斯分布,并且由于a而惨败。 it's only half a Gaussian instead of a full one, and b. 它仅是高斯的一半而不是完整的高斯,并且b。 what I've used before has only ever used one bunch of numbers. 我以前用过的只用过一堆数字。 So something like: 所以像这样:

# best fit of data
num_bins = 20
(mu, sigma) = norm.fit(sixteen)

y = mlab.normpdf(num_bins, mu, sigma)

n, bins, patches = plt.hist(deg_array, num_bins, normed=1, facecolor='blue', alpha=0.5)
# add a 'best fit' line
y = mlab.normpdf(bins, mu, sigma)
plt.plot(bins, y, 'r--')

Does this approach work at all here, or am I going about this in the wrong way completely? 这种方法在这里根本行不通吗,还是我完全以错误的方式解决了这个问题? Thanks... 谢谢...

It seems that your normal solution is to find the expectation value and standard deviation of the data directly instead of using a least square fit. 看来,通常的解决方案是直接找到数据的期望值和标准偏差,而不是使用最小二乘拟合。 Here is a solution using curve_fit from scipy.optimize. 这是使用scipy.optimize中的curve_fit的解决方案。

from matplotlib import pyplot as plt
from scipy.optimize import curve_fit
import numpy as np

x = np.array([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19])
y = [22.4155688819,22.3936180362,22.3177538001,22.1924849792,21.7721194577,21.1590235248,20.6670446864,20.4996957642,20.4260953411,20.3595072628,20.3926201626,20.6023149681,21.1694961343,22.1077417713,23.8270366414,26.5355924353,31.3179807276,42.7871637946,61.9639549412,84.7710953311]

# Define a gaussian function with offset
def gaussian_func(x, a, x0, sigma,c):
    return a * np.exp(-(x-x0)**2/(2*sigma**2)) + c

initial_guess = [1,20,2,0]
popt, pcov = curve_fit(gaussian_func, x, y,p0=initial_guess)

xplot = np.linspace(0,30,1000)
plt.scatter(x,y)
plt.plot(xplot,gaussian_func(xplot,*popt))

plt.show() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM