1,目标函数为y=w1*x1+w2*x2+b
2,已知x1,x2,y的几组数据,利用梯度不断下降求出w1,w1,b(数据自己写的,不精确)
x1 = [123, 134, 145, 156, 178, 189, 198, 200]#面积
x2= [2, 3, 4, 5, 6, 7, 8, 9]#房间数
y= [100, 130, 150, 167, 178, 187, 199, 212]#价格
3,因为数据差的过大,所以先对数据进行归一化处理,原理为:
代码实现如下
def f(list=[]):
xmin = min(list)
xmax = max(list)
for i, x in enumerate(list):
list[i] = (x - xmin) / (xmax - xmin)
return list
4,定义学习率等变量,利用数组存储数据,方便画图。同时对不同变量求偏导。
learn=0.001#学习率
m=8#变量数
w1=w2=b=1#赋初值
#数组存储数据
storage2=[]
storage1=[]
storage3=[]
d= [0, 0, 0]
#求导
for i in range(m-1):
d[0]+=(w1*x1[i+1]+w2*x2[i+1]-y[i+1]+b)*x1[i+1]
d[1]+=(w2*x2[i+1]+w1*x1[i+1]-y[i+1]+b)*x2[i+1]
d[2]+=(w1*x1[i+1]+w2*x2[i+1]+b)-y[i+1]
5,设置误差值,作为判定条件
for i in range(m):
error1 += w1 * x1[i] + w2 * x2[i] + b - y[i]
6,定义循环,选择判定条件,误差足够小,跳出循环,学习完成。
while True:
if abs(error1)<5:
break
else:
error=0
7,输出所得变量值,得到预测函数
print(w1)
print(w2)
print(b)
8,运行后,结果如下。由于数据过于随意,结果较复杂,我从网上抄了一下别人的规范数据。
9,网上找的数据。
x = [150,200,250,300,350,400,600]
y = [6450,7450,8450,9450,11450,15450,18450]
10,由于数据只有两组,我对代码进行简化,完整如下:
def f(list=[]):
xmin = min(list)
xmax = max(list)
for i, x in enumerate(list):
list[i] = (x - xmin) / (xmax - xmin)
return list
x = f([150,200,250,300,350,400,600])
y =f( [6450,7450,8450,9450,11450,15450,18450])
learn = 0.00001
m =7
w = 0
b = 0
error0=0
error1=0
epsilon=0.000001
storage0 = []
storage1 = []
while True:
diff = [0, 0]
for i in range(m):
diff[0] +=w*x[i]+b - y[i]
diff[1] += (w*x[i] +b- y[i]) * x[i]
b = b - learn / m * diff[0]
w = w - learn / m * diff[1]
storage0.append(b)
storage1.append(w)
error1 = 0
for i in range(len(x)):
error1 += (y[i] - (b + w * x[i])) ** 2 / 2
if abs(error1 - error0) < epsilon:
break
else:
error0 = error1
print(w,b)