学习笔记(1)
一、解析法实现一元线性回归
代码如下(示例):
# 解析法实现一元线性回归
# 手动导入数据
x = [137.97, 104.50, 100.00, 124.32, 79.20, 99.00, 124.00, 114.00,
106.69, 138.05, 53.75, 46.91, 68.00, 63.02, 81.26, 86.21]
y = [145.00, 110.00, 93.00, 116.00, 65.32, 104.00,118.00, 91.00,
62.00, 133.00, 51.00, 45.00, 78.50, 69.65, 75.69, 95.30]
# 均值
meanX = sum(x) / len(x)
meanY = sum(y) / len(y)
# 计算w,b
sumXY = 0.0
sumX = 0.0
for i in range(len(x)):
sumXY += (x[i] - meanX) * (y[i] - meanY)
sumX += (x[i] - meanX) * (x[i] - meanX)
w = sumXY / sumX
b = meanY - w*meanX
print("w=", w)
print("b=", b)
# 预测房价
x_test = [128.15, 45.00, 141.43, 106.27, 99.00, 53.84, 85.36, 70.00]
print("面积\t估计房价")
for i in range(len(x_test)):
print(x_test[i], "\t", round(w*x_test[i]+b,2))
# Numpy实现
import numpy as np
from six import byte2int
x1 = np.array([137.97, 104.50, 100.00, 124.32, 79.20, 99.00, 124.00, 114.00,
106.69, 138.05, 53.75, 46.91, 68.00, 63.02, 81.26, 86.21])
y1 = np.array([145.00, 110.00, 93.00, 116.00, 65.32, 104.00,118.00, 91.00,
62.00, 133.00, 51.00, 45.00, 78.50, 69.65, 75.69, 95.30])
meanX1 = np.mean(x1)
meanY1 = np.mean(y1)
sumXY1 = np.sum((x1-meanX1) * (y1-meanY1))
sumX1 = np.sum((x1-meanX1) * (x1-meanX1))
w1 = sumXY1 / sumX1
b1 = meanY1 - w1*meanX1
print("w1=", w1)
print("b1=", b1)
x_test1 = np.array([128.15, 45.00, 141.43, 106.27, 99.00, 53.84, 85.36, 70.00])
y_pred1 = w1*x_test1+b1
print("面积\t估计房价")
for i in range(len(x_test1)):
print(x_test[i], "\t", round(y_pred1[i], 2))
# TensorFlow实现
import tensorflow as tf
x2 = tf.constant([137.97, 104.50, 100.00, 124.32, 79.20, 99.00, 124.00, 114.00,
106.69, 138.05, 53.75, 46.91, 68.00, 63.02, 81.26, 86.21])
y2 = tf.constant([145.00, 110.00, 93.00, 116.00, 65.32, 104.00,118.00, 91.00,
62.00, 133.00, 51.00, 45.00, 78.50, 69.65, 75.69, 95.30])
meanX2 = tf.reduce_mean(x2)
meanY2 = tf.reduce_mean(y2)
sumXY2 = tf.reduce_sum((x2-meanX2) * (y2-meanY2))
sumX2 = tf.reduce_sum((x2-meanX2) * (x2-meanX2))
w2 = sumXY2 / sumX2
b2 = meanY2 - w2*meanX2
print("w2=", w2)
print("b2=", byte2int)
x_test2 = tf.constant([128.15, 45.00, 141.43, 106.27, 99.00, 53.84, 85.36, 70.00])
y_pred2 = w2*x_test2+b2
print("面积\t估计房价")
for i in range(len(x_test2)):
print(x_test2[i], "\t", y_pred2[i])
完整代码:
# 解析法实现一元线性回归(完整程序)
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
with tf.device('/gpu:0'):
# 设置字体
plt.rcParams['font.sans-serif'] = ['SimHei']
# 加载样本数据
x = tf.constant([137.97, 104.50, 100.00, 124.32, 79.20, 99.00, 124.00, 114.00,
106.69, 138.05, 53.75, 46.91, 68.00, 63.02, 81.26, 86.21])
y = tf.constant([145.00, 110.00, 93.00, 116.00, 65.32, 104.00,118.00, 91.00,
62.00, 133.00, 51.00, 45.00, 78.50, 69.65, 75.69, 95.30])
# 学习模型
meanX = tf.reduce_mean(x)
meanY = tf.reduce_mean(y)
sumXY = tf.reduce_sum((x-meanX) * (y-meanY))
sumX = tf.reduce_sum((x-meanX) * (x-meanX))
w = sumXY / sumX
b = meanY - w*meanX
print("权值w=", w.numpy(),"\n偏置值b=", b.numpy())
print("线性模型:y=", w.numpy(), "*x+", b.numpy())
x_test = np.array([128.15, 45.00, 141.43, 106.27, 99.00, 53.84, 85.36, 70.00])
y_pred = (w*x_test+b).numpy()
# 预测房价
print("面积\t估计房价")
n = len(x_test)
for i in range(n):
print(x_test[i], "\t", round(y_pred[i], 2))
# 数据和模型可视化
plt.figure()
plt.scatter(x,y,color="red",label="销售记录")
plt.scatter(x_test,y_pred,color="blue",label="预测房价")
plt.plot(x_test,y_pred,color="green",label="拟合直线",linewidth=2)
plt.xlabel("面积(平方米)", fontsize=14)
plt.ylabel("价格(万元)", fontsize=14)
plt.xlim((40,150))
plt.ylim((40,150))
plt.suptitle("商品房销售价格评估系统v1.0", fontsize=20)
plt.legend(loc="upper left")
plt.show()
二、解析法实现多元线性回归
代码如下(示例):
# 解析法实现多元线性回归
#加载样本数据
import numpy as np
x1 = np.array([137.97, 104.50, 100.00, 124.32, 79.20, 99.00, 124.00, 114.00,
106.69, 138.05, 53.75, 46.91, 68.00, 63.02, 81.26, 86.21])
x2 = np.array([3,2,2,3,1,2,3,2,2,3,1,1,1,1,2,2])
y = np.array([145.00, 110.00, 93.00, 116.00, 65.32, 104.00,118.00, 91.00,
62.00, 133.00, 51.00, 45.00, 78.50, 69.65, 75.69, 95.30])
# 数据处理
x0 = np.ones(len(x1))
X = np.stack((x0,x1,x2), axis = 1)
Y = np.array(y).reshape(-1,1)
# 求解模型参数
Xt = np.transpose(X) # 计算X'
XtX_1 = np.linalg.inv(np.matmul(Xt,X)) # 计算(X'X)-1
XtX_1_Xt = np.matmul(XtX_1, Xt) # 计算(X'X-1)X'
W = np.matmul(XtX_1_Xt, Y) # W=(X'X-1)X'Y
W = W.reshape(-1)
print("多元线性回归方程:")
print("Y=", W[1], "*x1+", W[2], "*x2+", W[0])
# 预测房价
print("请输入房屋面积和房间数,预测房屋销售价格:")
x1_test = float(input("商品房面积:"))
x2_test = int(input("房间数:"))
y_pred = W[1]*x1_test + W[2]*x2_test + W[0]
print("预测价格:", round(y_pred,2),"万元")
完整代码:
# 解析法实现多元线性回归(tensorflow)
#加载样本数据
import tensorflow as tf
import numpy as np
x1 = tf.constant([137.97, 104.50, 100.00, 124.32, 79.20, 99.00, 124.00, 114.00,
106.69, 138.05, 53.75, 46.91, 68.00, 63.02, 81.26, 86.21])
x2 = tf.constant([3,2,2,3,1,2,3,2,2,3,1,1,1,1,2,2])
y = tf.constant([145.00, 110.00, 93.00, 116.00, 65.32, 104.00,118.00, 91.00,
62.00, 133.00, 51.00, 45.00, 78.50, 69.65, 75.69, 95.30])
# 数据处理
x0 = tf.ones(len(x1))
x2 = tf.cast(x2, dtype=tf.float32)
X = tf.stack((x0,x1,x2), axis = 1)
Y = tf.reshape(y,[-1,1])
#Y = tf.constant((y.numpy().reshape(-1,1)))
# 求解模型参数
Xt = tf.transpose(X, perm=[1,0]) # 计算X'
XtX = Xt@X
XtX_1 = tf.linalg.inv(XtX) # 计算X'X-1
W = XtX_1@Xt@Y # W=(X'X-1)X'Y
W = tf.reshape(W, -1)
W = W.numpy()
print("多元线性回归方程:")
print("Y=", W[1], "*x1+", W[2], "*x2+", W[0])
# 预测房价
print("请输入房屋面积和房间数,预测房屋销售价格:")
x1_test = float(input("商品房面积:"))
x2_test = int(input("房间数:"))
y_pred = W[1]*x1_test + W[2]*x2_test + W[0]
print("预测价格:", round(y_pred,2),"万元")
可视化:
from matplotlib import markers
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
x1 = x1 = np.array([137.97, 104.50, 100.00, 124.32, 79.20, 99.00, 124.00, 114.00,
106.69, 138.05, 53.75, 46.91, 68.00, 63.02, 81.26, 86.21])
x2 = np.array([3,2,2,3,1,2,3,2,2,3,1,1,1,1,2,2])
y = np.array([145.00, 110.00, 93.00, 116.00, 65.32, 104.00,118.00, 91.00,
62.00, 133.00, 51.00, 45.00, 78.50, 69.65, 75.69, 95.30])
#绘制散点图
W = np.array([11.96729093, 0.53488599, 14.33150378])
y_pred = W[1]*x1 + W[2]*x2 + W[0]
fig = plt.figure(figsize=(8,6))
ax3d = Axes3D(fig)
ax3d.scatter(x1, x2, y, color="b",marker="*")
ax3d.set_xlabel('Area',color='r',fontsize=16)
ax3d.set_ylabel('Room',color='r',fontsize=16)
ax3d.set_zlabel('Price',color='r',fontsize=16)
ax3d.set_yticks([1,2,3])
ax3d.set_zlim3d(30,160)
plt.show()
#绘制平面图
X1,X2 = np.meshgrid(x1,x2)
Y_PRED = W[0] + W[1]*X1 + W[2]*X2
fig = plt.figure(2)
ax3d = Axes3D(fig)
ax3d.plot_surface(X1, X2, Y_PRED, cmap="coolwarm")
ax3d.set_xlabel('Area',color='r',fontsize=16)
ax3d.set_ylabel('Room',color='r',fontsize=16)
ax3d.set_zlabel('Price',color='r',fontsize=16)
ax3d.set_yticks([1,2,3])
plt.show()
#绘制散点图和线框图
plt.rcParams['font.sans-serif'] = ['SimHei']
fig = plt.figure(3)
ax3d = Axes3D(fig)
ax3d.scatter(x1,x2,y,color='b',marker='*',label="销售记录")
ax3d.scatter(x1,x2,y_pred,color='r',label="预测房价")
ax3d.plot_wireframe(X1,X2,Y_PRED,color='c',linewidth=0.5,label="拟合平面")
ax3d.set_xlabel('Area',color='r',fontsize=14)
ax3d.set_ylabel('Room',color='r',fontsize=14)
ax3d.set_zlabel('Price',color='r',fontsize=14)
ax3d.set_yticks([1,2,3])
plt.suptitle("商品房销售回归模型", fontsize=20)
plt.legend(loc="upper left")
plt.show()
总结
记一下学习笔记!