题目二
代码:
import numpy as np
from numpy import *
x=np.array([64.3,99.6,145.45,63.75,135.46,92.85,86.97,144.76,59.3,116.03])
y=np.array([62.55,82.42,132.62,73.31,131.05,86.57,85.49,127.44,55.25,104.84])
sum_x=0
sum_y=0
w_1=0
mul_1=0
mul_2=0
c=1.0
for index in range(len(x)):
sum_x=sum_x+x[index]
x_1=sum_x/10
sum_y=sum_y+y[index]
y_1=sum_y/10
for index_1 in range(len(x)):
sub_x=x[index_1]-x_1
sub_y=y[index_1]-y_1
mul_1=float(sub_x*sub_y)
w_1=w_1+mul_1
mul_2=mul_2+float(sub_x*sub_x)
w=w_1/mul_2
b=y_1-w*x_1
print("w=%f"%(w))
print("b=%f"%(b))
结果: