Python Scipy优化自定义成本函数
(抱歉,成本函数可能是错误的术语) 我试图拟合一些质谱数据,我知道大部分峰是什么,但不是全部。因此,当我完成时,我期望有一个正的剩余。看起来大多数scipy拟合算法只是试图最小化rms残差,所以我没有看到惩罚负残差多于正残差的方法 下面是一些简单的代码:Python Scipy优化自定义成本函数,python,scipy,Python,Scipy,(抱歉,成本函数可能是错误的术语) 我试图拟合一些质谱数据,我知道大部分峰是什么,但不是全部。因此,当我完成时,我期望有一个正的剩余。看起来大多数scipy拟合算法只是试图最小化rms残差,所以我没有看到惩罚负残差多于正残差的方法 下面是一些简单的代码: import numpy as np from scipy import optimize import matplotlib.pyplot as plt def gaussian(x, a, x0, sigma): return
import numpy as np
from scipy import optimize
import matplotlib.pyplot as plt
def gaussian(x, a, x0, sigma):
return a * np.exp(-(x - x0)**2.0 / (2.0 * sigma**2))
def get_y_with_noise(x, peaks):
"""Return your input spectrum with some lazy shot like noise."""
n = len(x)
y = np.zeros(n)
for peak in peaks:
y += gaussian(x, *peak)
# Add some noise.
y += np.random.randn(n) * 0.05 * np.sqrt(y)
y[np.where(y < 0.0)] = 0.0
return y
def fit_peaks_and_plot(x, peaks):
"""Generate the fit some peaks."""
y = get_y_with_noise(x, peaks)
# Make a really good guess of starting params.
p0 = peaks[0]
# Fit the data.
popt, pcov = optimize.curve_fit(gaussian, x, y, p0=p0)
# Plot residuals. Look works great.
plt.figure()
plt.plot(x, y)
plt.plot(x, gaussian(x, *popt))
plt.plot(x, y - gaussian(x, *popt))
plt.show()
# Define our data range.
x = np.arange(-10.0, 10.0, 0.01)
# Some peaks that separate nicely.
peaks_1 = [[1.0, 0.0, 1.0], [0.5, 5.0, 1.0]]
# Some peaks that are too close for comforter.
peaks_2 = [[1.0, 0.0, 1.0], [0.5, 2.0, 1.0]]
# Set up some peak.
fit_peaks_and_plot(x, peaks_1)
fit_peaks_and_plot(x, peaks_2)
作为玩具模型,您可以尝试最小化:
np.sum( ((f(xdata, *popt) - ydata) / sigma)**2 )
weighted_res = (f(xdata, *popt) - ydata) / sigma
weighted_res[np.where(weighted_res < 0.0)] *= 10.0
np.sum(weighted_res)
weighted_res=(f(扩展数据,*popt)-ydata)/sigma
加权分辨率[np.式中(加权分辨率<0.0)]*=10.0
np.和(加权)
显然,我可以定义一个函数,返回加权的_res并尝试将其拟合为零,但这似乎是一种非常迂回的方法