为什么optimize.curve_fit在较小的数据集上不起作用?

问题描述 投票:0回答:1

我已经对我的数据H2OCO2执行了分段线性拟合。它适用于288个数据点的数据集,但不适用于144个数据点的数据集。我的代码如下:

#Piecewiselinear fit
x = np.array(H2O)
y = np.array(CO2)
p , e = optimize.curve_fit(piecewise_linear, x, y)
xd = np.linspace(0, 1, 1000)

使用144数据点的数据集,运行optimize.curve_fit时唯一的不同。我收到以下消息OptimizeWarning: Covariance of the parameters could not be estimated category=OptimizeWarning。这些是不同的情节:Correct fit (288 points)Incorrect fit (144 points)

出了什么问题?我该如何解决?

python numpy statistics linear-regression piecewise
1个回答
0
投票

我最终改变了通用方法

def piecewise_linear(x, x0, y0, k1, k2):
    return np.piecewise(x, [x < x0], [lambda x:k1*x + y0-k1*x0, lambda x:k2*x + y0-k2*x0])

my_pwlf = pwlf.PiecewiseLinFit(x, y)
breaks = my_pwlf.fit(2) #if you want multiple breakpoints change the number in the bracket.
print(breaks)

x_hat = np.linspace(x.min(), x.max(), 100)
y_hat = my_pwlf.predict(x_hat)

这里,我也得到了断点的x值。

© www.soinside.com 2019 - 2024. All rights reserved.