Py-*_*ser 5 function gaussian python-3.x
我知道这些问题已经被问了几次,但我仍然无法得到它:我想定义一个返回多个参数的函数.
我编写了以下代码来将数据拟合到偏斜的高斯函数:
def skew(x, sigmag, mu, alpha, c, a):
normpdf = (1/(sigmag*np.sqrt(2*math.pi)))*np.exp(-(np.power((x-mu),2)/(2*np.power(sigmag,2))))
normcdf = (0.5*(1+sp.erf((alpha*((x-mu)/sigmag))/(np.sqrt(2)))))
return 2*a*normpdf*normcdf + c
popt, pcov = curve_fit(skew, xdata, ydata, p0=(5.5, 57636., 4.5, 0.0001, 0.01))
y_fit= skew(xdata, popt[0], popt[1], popt[2], popt[3], popt[4])
Run Code Online (Sandbox Code Playgroud)
但是,我的想法是获得数据分布的峰值,而不是skew函数返回的平均值作为最佳拟合值之一.因此,我需要mode分配,可以计算为最大值normpdf.
如何normpdf从定义的函数中获取并获得最大拟合数据?
您的代码不是我们可以运行的最小、完整和可验证的示例,并且您没有提供示例输出,但我想我看到了这个问题。您似乎在询问popt, pcov = curve_fit(...)行中使用的逗号“,”元组解包运算符。我们将保持该行不变,并mode从您的功能中恢复。用这个:
def skew2(x, sigmag, mu, alpha, c, a):
normpdf = (1 / (sigmag * np.sqrt(2 * math.pi))) * np.exp(-(np.power((x - mu), 2) / (2 * np.power(sigmag, 2))))
normcdf = (0.5 * (1 + sp.erf((alpha * ((x - mu) / sigmag)) / (np.sqrt(2)))))
return 2 * a * normpdf * normcdf + c, max(normpdf)
def skew(x, sigmag, mu, alpha, c, a):
return skew2(x, sigmag, mu, alpha, c, a)[0]
popt, pcov = curve_fit(skew, xdata, ydata, p0=(5.5, 57636., 4.5, 0.0001, 0.01))
y_fit, mode = skew2(xdata, *popt[:5])
Run Code Online (Sandbox Code Playgroud)