import numpy as np
import matplotlib.pyplot as plt
q0 = np.random.random()
q = np.random.normal(0,1,2)
alpla = 0.0000000001
x = np.array([[2104,3],[1600,3],[2400,3],[1416,2],[3000,4]])
t = np.array([400,330,369,232,540])
eps = 1e-4
e0 = 999999
e1 = 999999
e2 = 999999
i = 0
r = i+1
while e0>=eps or e1>=eps or e2>=eps:#公式里sum all需要i=1开始到m,把i变换成r=i+1
y = x[i,0:2]*q[r]-t[r]+q0
e0 = np.sum(y)
e1 = e0*x[i,0]
e2 = e0*x[i,1]
q0 = q0-alpla*e0
q1 = q1-alpla*e1
q2 = q2-alpla*e2
i +=1
print(q0, q1, q2)
0.4956192638418069 -456.0601881268437 -0.27744242850992407
import numpy as np
import matplotlib.pyplot as plt
q = np.random.normal(0,1,2)
q
array([-0.91225843, -0.52892458])
import numpy as np
import matplotlib.pyplot as plt
q0 = np.random.random()
q1 = np.random.random()
q2 = np.random.random()
q = np.array([q1,q2])
alpla = 0.0000001
x = np.array([[2104,3],[1600,3],[2400,3],[1416,2],[3000,4]])
t = np.array([400,330,369,232,540])
eps = 1e-4
e0 = 99999
e1 = 99999
e2 = 99999
i = 0
r = i+1
while e0>=eps or e1>=eps or e2>=eps:#公式里sum all需要i=1开始到m,把i变换成r=i+1
y = x[i,0:2]*q[r]-t[r]+q0*1
e0 = np.sum(y)
e1 = e0*x[i,0]
e2 = e0*x[i,1]
q0 = q0-alpla*e0
q1 = q1-alpla*e1
q2 = q2-alpla*e2
i +=1
print(q0, q1, q2)
0.3664233164410757 0.28367059369830744 0.35920645564439635
|