利用一元线性回归的解析解公式:
数据来源于tensorflow.keras的内置数据集boston_housing,通过切片只取平均房间数作为自变量,利用tensorflow进行矩阵运算。
版本一如下:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
def weight(x,y):
x_mean=tf.reduce_mean(x,0)
y_mean=tf.reduce_mean(y,0)
n=len(x)
i=1
fenzi=0
fenmu=0
while i<=n:
fenzi=fenzi+(x[i-1]-x_mean)*(y[i-1]-y_mean)
fenmu=fenmu+(x[i-1]-x_mean)**2
i=i+1
weight=fenzi/fenmu
return weight
def bias(x,y,w):
x_mean=tf.reduce_mean(x,0)
y_mean=tf.reduce_mean(y,0)
bias=y_mean-w*x_mean
return bias
#main
boston_housing=tf.keras.datasets.boston_housing
(train_x,train_y),(test_x,test_y)=boston_housing.load_data(test_split=0)
room=train_x[:,5]
w=weight(room,train_y)
b=bias(room,train_y,w)
print("拟合结果 w=%f,b=%f" % (w,b))
y_pred=tf.constant(w*room+b)
plt.rcParams["font.sans-serif"]="SimHei"
plt.figure()
plt.scatter(room,train_y,color="red",s=2,label="原始数据")
plt.scatter(room,y_pred,color="blue",s=2,label="预测房价")
plt.plot(room,y_pred,label="拟合曲线")
plt.xlabel("平均房间数")
plt.ylabel("房价")
plt.xlim(2.5,10)
plt.suptitle("一元线性回归波士顿房价预测" ,fontsize=17)
plt.legend()
plt.show()
后来发现可以利用对矩阵中元素求和来代替函数weight( )里的循环算法,于是对这个函数的定义作了调整:
def weight(x,y):
x_mean=tf.reduce_mean(x,0)
y_mean=tf.reduce_mean(y,0)
n=len(x)
i=1
fenzi=tf.reduce_sum((x-x_mean)*(y-y_mean))
fenmu=tf.reduce_sum(pow((x-x_mean),2))
weight=fenzi/fenmu
return weight
输出结果如下