如何使用这样的高斯函数进行更好的曲线拟合?

时间:2018-08-23 08:42:51

标签: python python-3.x curve-fitting gaussian

我有数据,并且正在用高斯曲线拟合对数据进行拟合。蓝色的项目符号是我的数据。高斯从零开始,看起来像红色曲线。但是我想要一些看起来更像绿色曲线的东西。我在互联网上找到的所有高斯曲线拟合示例均从零开始。也许还有另一个函数可以更改起始y值或类似的值?

enter image description here

到目前为止,这是我的代码:

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

path = 'Proben Bilder v06 results'
filename = '00_sumListe.csv'

# read csv file with scale data
x = []
y = []
with open(os.path.join(path, filename), 'r') as csvfile:
    sumFile = csv.reader(csvfile, delimiter=',')
    for row in sumFile:
        id = float(row[0])
        sumListe = -float(row[1])
        x = np.append(x, id)
        y = np.append(y, sumListe)
y = y-min(y)
# x = np.arange(10)
# y = np.array([0, 1, 2, 3, 4, 5, 4, 3, 2, 1])

# weighted arithmetic mean (corrected - check the section below)
mean = sum(x * y) / sum(y)
sigma = np.sqrt(sum(y * (x - mean)**2) / sum(y))


def gauss(x, a, x0, sigma):             # x0 = mü
    return a * np.exp(-(x - x0)**2 / (2 * sigma**2))


popt, pcov = curve_fit(gauss, x, y, p0=[max(y), mean, sigma])
# plt.gca().invert_yaxis()
plt.plot(x, y, 'b+:', label='data')
plt.plot(x, gauss(x, *popt), 'r-', label='fit')
plt.legend()
plt.title('Fig. 3 - Fit for Time Constant')
plt.xlabel('steps')
plt.ylabel('mean value')
plt.show()

我的数据要写在这里有点大...我无法加载它,还是可以吗?

有人有更好的主意吗?

1 个答案:

答案 0 :(得分:4)

您可以修改高斯函数,以使y轴上的偏移量可能,从而使您更适应。这要求您在javax.net

中添加额外的初始猜测
p0

enter image description here