python中polyfit的意外结果

2024-09-30 18:19:46 发布

您现在位置:Python中文网/ 问答频道 /正文

请允许我首先介绍问题的背景:

我参加了一个测验,它给了我一个数据集和一个逻辑方程:Logistic

然后询问该模型是否可以线性化,如果可以,则使用线性模型来评估ak的值

我试着把它线性化如下:Linearized 并用python编码:

t = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
y = np.array([43.65, 109.86, 187.21, 312.67, 496.58, 707.65, 960.25, 1238.75, 1560, 1824.29, 2199, 2438.89, 2737.71])
yAss = np.log(3000/y - 1)
cof = np.polyfit(t, yAss, deg = 1)
a = math.e**(cof[0]); 
k = -cof[1];
yAfter = 3000 / (1 + a*math.e**(-k*t))

sizeScalar = 10
fig = plt.figure(figsize = (sizeScalar*1.1, sizeScalar))
plt.plot(t, y, 'o', markersize = sizeScalar*0.75)
plt.plot(t, yAfter, 'r-')
plt.grid(True)
plt.show()

明白了,这显然是不正确的:Wrong_Result 巧合的是,我修改了部分代码:

t = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
y = np.array([43.65, 109.86, 187.21, 312.67, 496.58, 707.65, 960.25, 1238.75, 1560, 1824.29, 2199, 2438.89, 2737.71])
yAss = np.log(3000/y - 1)
cof = np.polyfit(t, yAss, deg = 1)
a = math.e**(-cof[1]); #<<<===============here. Before: a = math.e**(cof[0])
k = cof[0]; #<<<==========================and here, Before: k = -cof[1]
temp = 3000 / (1 + a*math.e**(-k*t))
yAfter = []
for itera in temp: #<<<=======================add this
    yAfter.append(3000 - itera)

sizeScalar = 10
fig = plt.figure(figsize = (sizeScalar*1.1, sizeScalar))
plt.plot(t, y, 'o', markersize = sizeScalar*0.75)
plt.plot(t, yAfter, 'r-')
plt.grid(True)
plt.show()

收到了一个似乎正确的序列?Holy_Crap 但怎么可能呢?我认为cof[0]是beta,cof1-k,如果是,我以前的代码应该有一些错误的概念。但是改变系数的顺序和符号,我得到了一个很合适的结果?!这纯粹是巧合吗? 那么测验的正确答案是什么呢


Tags: 模型logplotnpfigpltmatharray