简体   繁体   English

最小二乘拟合/幂拟合 - 为什么我的变量是错误的数字

[英]Least Squares Fitting/Power Fit - Why is my variable the wrong number

so I have to find the coefficients of this function.所以我必须找到这个 function 的系数。

https://mathworld.wolfram.com/LeastSquaresFittingPowerLaw.html https://mathworld.wolfram.com/LeastSquaresFittingPowerLaw.html

I think I input the equation for the variable "a" right, the answer should be around 8. But it keeps giving me an answer of around 2. Did I go wrong somewhere in my process?我想我正确输入了变量“a”的方程式,答案应该在 8 左右。但它一直给我一个 2 左右的答案。我 go 在我的过程中的某个地方错了吗?

> import matplotlib.pyplot as mp
> import math
> 
> # (x, y) data values
> xList = [1, 2, 3, 4, 5, 6]
> yList = [8, 11, 19, 20, 18, 22]
> x = xList.copy()
> y = yList.copy()
> print (x)
> print (y)
> 
> Power Fit variables
> n = len(x)
> sumLog_x = 0
> sumLog_y = 0
> sumLog_xx = 0
> sumLog_xy = 0
> b = 0; b_Num = 0; b_Denom = 0
> a = 0; a_Num = 0; a_Denom = 0
> 
> for i in range(n) :
> sumLog_x += math.log(x[i])
> sumLog_y += math.log(y[i])
> sumLog_xx += math.log(x[i]) * math.log(x[i])
> sumLog_xy += math.log(x[i]) * math.log(y[i])
> 
> compute the exponent b
> b_Num = n * sumLog_xy - sumLog_x * sumLog_y 
> b_Denom = n * sumLog_xx - sumLog_x * sumLog_x
> b = b_Num / b_Denom
> 
> 
> a_Num = (sumLog_y) - (sumLog_x * b)
> a_Denom = n
> a = a_Num / a_Denom
> 
> print ("modeling: y = a * x^b")
> print ("exponent b = ", format(b, "0.6f"))
> print ("exponent a = ", format(a, "0.6f"))

My output is this我的output是这个

> [1, 2, 3, 4, 5, 6]
> [8, 11, 19, 20, 18, 22]
> modeling: y = a * x^b
> exponent b =  0.573009
> exponent a =  2.104825

Why is "a" coming out as 2.1?为什么“a”作为 2.1 出现?

Because you forgot to calculate correct A as np.exp(a).因为你忘了计算正确A作为 np.exp(a)。 Here is the final test code:下面是最终的测试代码:

#!/usr/bin/env ipython
import matplotlib.pyplot as plt
import math
import numpy as np

# (x, y) data values
xList = [1, 2, 3, 4, 5, 6]
yList = [8, 11, 19, 20, 18, 22]
x = xList.copy()
y = yList.copy()
print (x)
print (y)
 
#Power Fit variables
n = len(x)
sumLog_x = 0
sumLog_y = 0
sumLog_xx = 0
sumLog_xy = 0
b = 0; b_Num = 0; b_Denom = 0
a = 0; a_Num = 0; a_Denom = 0
nn = 0; 
for i in range(n) :
    sumLog_x += math.log(x[i])
    sumLog_y += math.log(y[i])
    sumLog_xx += math.log(x[i]) * math.log(x[i])
    sumLog_xy += math.log(x[i]) * math.log(y[i])
    nn += 1
#compute the exponent b
b_Num = n * sumLog_xy - sumLog_x * sumLog_y 
b_Denom = n * sumLog_xx - sumLog_x * sumLog_x
b = b_Num / b_Denom
 
 
a_Num = (sumLog_y) - (sumLog_x * b)
a_Denom = n
a = a_Num / a_Denom

print ("modeling: y = a * x^b")
print ("exponent b = ", format(b, "0.6f"))
print ("exponent a = ", format(a, "0.6f"))
# -----------------------------------------------------------
plt.plot(xList,yList,'k+',ms=10);
xx = np.arange(np.min(xList),np.max(xList),0.05);yy = np.exp(a)*xx**b
plt.plot(xx,yy,'r')
plt.show()

To me it seemed that the final solution is correct ie the least square fit matches with the data.在我看来,最终的解决方案似乎是正确的,即最小二乘法与数据相匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM