多元线性回归:波士顿房价预测问题
程序员文章站
2022-07-08 09:42:14
...
1.读取数据
从boston.csv中读取所需数据
import numpy as np
import matplotlib.pyplot as plt
import tensorflow.compat.v1 as tf
import pandas as pd
from sklearn.utils import shuffle
df = pd.read_csv("data/boston.csv",header=0)
#打印摘要
print(df.describe())
2.数据预处理
设置数据输入格式,归一化
归一化能提升模型的收敛速度,提升模型的精度
df = df.values
df = np.array(df)
#归一化
for i in range(12):
df[:,i] = (df[:,i] - df[:,i].min())/(df[:,i].max()-df[:,i].min())
x_data = df[:,:12]
y_data = df[:,12]
#print(x_data,"\n shape = ",x_data.shape)
#print(y_data,"\n shape = ",y_data.shape)
x = tf.placeholder(tf.float32,[None,12],name="X")
y = tf.placeholder(tf.float32,[None,1],name="Y")
3.定义模型
定义多元线性回归模型
with tf.name_scope("Model"):
w = tf.Variable(tf.random_normal([12,1],stddev=0.01),name="w")
b = tf.Variable(1.0,name="b")
def model(x,w,b):
return tf.matmul(x,w)+b
pred = model(x,w,b)
4.训练模型
train_epoch = 50
learn_rate = 0.01
with tf.name_scope("LossFunction"):
loss_function = tf.reduce_mean(tf.pow(y-pred,2))
optimizer = tf.train.GradientDescentOptimizer(learn_rate).minimize(loss_function)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
loss_list = []
for epoch in range(train_epoch):
loss_sum = 0.0
for xs,ys in zip(x_data,y_data):
xs = xs.reshape(1,12)
ys = ys.reshape(1,1)
_,loss = sess.run([optimizer,loss_function],feed_dict={x:xs,y:ys})
loss_sum = loss_sum + loss
xvalues,yvalues = shuffle(x_data,y_data)
b0temp = b.eval(session=sess)
w0temp = w.eval(session=sess)
loss_avg = loss_sum/len(y_data)
loss_list.append(loss_avg)
print("epoch = ",epoch+1,"loss = ",loss_avg,"w = ",w0temp,"b = ",b0temp)
5.测试模型
n = np.random.randint(506)
print(n)
x_test = x_data[n]
x_test = x_test.reshape(1,12)
predict = sess.run(pred,feed_dict = {x:x_test})
print("预测值:%f" % predict)
print("标签值:%f" % y_data[n] )
6.查看每一epoch平均损失值
plt.plot(loss_list)
上一篇: Jmeter接口测试/并发测试