使用 scipy.curve_fit 强制特定拦截
Forcing specific intercepts using scipy.curve_fit
我目前正在尝试多种方法来拟合并随后使用二阶多项式函数转换一些数据。为此,我一直在使用以下代码:
import matplotlib.pyplot as plt
import numpy
from scipy.optimize import curve_fit
def fitFunc(self, x,a,b,c):
return a*x**2 + b*x + c
def calcQuadratic(self,data):
""" This function fits the specified function in 'fitFunc'
to the data, using the curve_fit package from scipy.optimize.
INPUT: A list of (m/z,int) tuples
OUTPUT: The parameters for the fitted function
"""
expected = []
observed = []
for i in data:
expected.append(i[0])
observed.append(i[1])
z = curve_fit(self.fitFunc, observed, expected)
#############
# Plot Code #
#############
newX = numpy.linspace(0,400,2500)
yNew = self.fitFunc(newX,*z[0])
fig = plt.figure()
ax = fig.add_subplot(111)
plt.scatter(expected,observed,label='raw')
plt.plot(newX,yNew,label='obs-exp')
plt.legend()
plt.show()
###############
# end of plot #
###############
return z[0]
随后我基本上通过以下方式转换数据:
new = []
for i in data:
new.append((fitFunc(i[0],*z[0]),i[1]))
问题
上述转换可能导致我的 Y 截距具有正 X 值。这样做的结果是,在转换之后,我现在找到了相同值的数据(见下图)
紫色线连接的数据点是问题案例的例子,在~5秒和~110秒观察到的数据在转换后将被强制为~100秒的时间。
问题
因此,我想知道是否有办法强制函数最大值(或最小值)为X = 0?我也愿意接受其他绕过这个问题的建议(目前,我忽略多项式的左半部分作为临时脏 hack/fix)。
附加信息
删除拟合函数的 b*x
部分是不可能的,因为该函数应该也能够 return (接近)线性拟合,请参见下图
如果 "near-linear" 和二次结果都是合理的可能性,我怀疑您正在寻找某种经验拟合。也许幂律会是更好的模型:a*x**b + c
。这包括抛物线(b
为 2)和直线(b
为 1)
的可能性
我目前正在尝试多种方法来拟合并随后使用二阶多项式函数转换一些数据。为此,我一直在使用以下代码:
import matplotlib.pyplot as plt
import numpy
from scipy.optimize import curve_fit
def fitFunc(self, x,a,b,c):
return a*x**2 + b*x + c
def calcQuadratic(self,data):
""" This function fits the specified function in 'fitFunc'
to the data, using the curve_fit package from scipy.optimize.
INPUT: A list of (m/z,int) tuples
OUTPUT: The parameters for the fitted function
"""
expected = []
observed = []
for i in data:
expected.append(i[0])
observed.append(i[1])
z = curve_fit(self.fitFunc, observed, expected)
#############
# Plot Code #
#############
newX = numpy.linspace(0,400,2500)
yNew = self.fitFunc(newX,*z[0])
fig = plt.figure()
ax = fig.add_subplot(111)
plt.scatter(expected,observed,label='raw')
plt.plot(newX,yNew,label='obs-exp')
plt.legend()
plt.show()
###############
# end of plot #
###############
return z[0]
随后我基本上通过以下方式转换数据:
new = []
for i in data:
new.append((fitFunc(i[0],*z[0]),i[1]))
问题
上述转换可能导致我的 Y 截距具有正 X 值。这样做的结果是,在转换之后,我现在找到了相同值的数据(见下图)
紫色线连接的数据点是问题案例的例子,在~5秒和~110秒观察到的数据在转换后将被强制为~100秒的时间。
问题
因此,我想知道是否有办法强制函数最大值(或最小值)为X = 0?我也愿意接受其他绕过这个问题的建议(目前,我忽略多项式的左半部分作为临时脏 hack/fix)。
附加信息
删除拟合函数的 b*x
部分是不可能的,因为该函数应该也能够 return (接近)线性拟合,请参见下图
如果 "near-linear" 和二次结果都是合理的可能性,我怀疑您正在寻找某种经验拟合。也许幂律会是更好的模型:a*x**b + c
。这包括抛物线(b
为 2)和直线(b
为 1)