gpt4 book ai didi

python - Matplotlib- 绘制具有三部分的分段线性函数

转载 作者:太空宇宙 更新时间:2023-11-04 04:54:36 31 4
gpt4 key购买 nike

我有一些数据,我想用一个包含三个部分的分段线性函数来拟合。所以如果有两个拐点,就像这样:

enter image description here

不幸的是,当我使用下面的代码时,我没有得到正确的数据,它看起来像这样

enter image description here

谁知道怎么回事?谢谢!

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

fit_days = np.array([1786,1852,2067,2142,2143,2313,2320,2426,2550,2681,2685,3037,3109,3178,3436,3485,3512,3914,4013,4016,4220,4268,4372,4577,4584,4966,5011,5387,5748,5810,6003,6129,6170,6283,6605,6693,6973,7085,7228,7620,7730,7901,8139,8370,8448,8737,8824,9089,9233,9321,9509,9568,9642,9756,9915,10601,10942], dtype=np.float)
fit_expt= np.array([.6,.62,.62,.65,.64,.63,.67,.69,.64,.67,.66,.67,.64,.685,.705,.707,.708,.694,.754,.745,.729,.736,.727,.757,.747,.764,.775,.79,.811,.815,.815,.833,.831,.829,.843,.858,.880,.872,.874,.893,.8905,.8916,.9095,.9142,.9109,.9185,.9169,.9251,.9290,.9304,.9467,.9378,0.9464,0.9508,0.9583,0.9857,0.9975],dtype=np.float)

xr2= fit_days[26:57]
yr2= fit_expt[26:57]
p0 = [np.mean(xr2), np.mean(yr2), 1, 1,1]

p , e = optimize.curve_fit(piecewise_linear2, xr2, yr2, p0)
x = np.linspace((xr2[0]-100), 11000, 3000)
#p , e = optimize.curve_fit(piecewise_linear2, xr2, yr2)
xd = np.linspace(0, 19, 11000)
plt.plot(xr2, yr2, "o",color="#2ca02c")
plt.plot(x, piecewise_linear2(x, *p),linestyle='dashed',color="#2ca02c")

最佳答案

也许您想重读 numpy.piecewise文档:

numpy.piecewise(x, condlist, funclist, *args, **kw)

Given a set of conditions and corresponding functions, evaluate each function on the input data wherever its condition is true.

如果你想有2个功能,你需要2个条件,

np.piecewise(x, [x < x0, x>= x0],  [lambda x:k1*x + y0-k1*x0, lambda x:k2*x + y0-k2*x0])

例子:

import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize as optimize
def piecewise_linear2(x, x0, y0, k1, k2):
return np.piecewise(x, [x < x0, x>= x0],
[lambda x:k1*x + y0-k1*x0,
lambda x:k2*x + y0-k2*x0])

fit_days = np.array([1786,1852,2067,2142,2143,2313,2320,2426,2550,2681,2685,3037,3109,3178,3436,3485,3512,3914,4013,4016,4220,4268,4372,4577,4584,4966,5011,5387,5748,5810,6003,6129,6170,6283,6605,6693,6973,7085,7228,7620,7730,7901,8139,8370,8448,8737,8824,9089,9233,9321,9509,9568,9642,9756,9915,10601,10942], dtype=np.float)
fit_expt= np.array([.6,.62,.62,.65,.64,.63,.67,.69,.64,.67,.66,.67,.64,.685,.705,.707,.708,.694,.754,.745,.729,.736,.727,.757,.747,.764,.775,.79,.811,.815,.815,.833,.831,.829,.843,.858,.880,.872,.874,.893,.8905,.8916,.9095,.9142,.9109,.9185,.9169,.9251,.9290,.9304,.9467,.9378,0.9464,0.9508,0.9583,0.9857,0.9975],dtype=np.float)

xr2= fit_days[26:57]
yr2= fit_expt[26:57]
p0 = [np.mean(xr2), np.mean(yr2), 1, 1]

p , e = optimize.curve_fit(piecewise_linear2, xr2, yr2, p0)
x = np.linspace((xr2[0]-100), 11000, 3000)
#p , e = optimize.curve_fit(piecewise_linear2, xr2, yr2)
xd = np.linspace(0, 19, 11000)
plt.plot(xr2, yr2, "o",color="#2ca02c")
plt.plot(x, piecewise_linear2(x, *p),linestyle='dashed',color="#2ca02c")

plt.show()

enter image description here

关于python - Matplotlib- 绘制具有三部分的分段线性函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47358953/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com