目录
- 【1】解析法实现一元线性回归
- python列表实现
- 利用Numpy实现
- 利用TensorFlow实现
- 数据和模型可视化
- 【2】解析法实现多元线性回归
- 利用Numpy实现
- 需要用到的NumPy数组运算函数
- 数据和模型可视化
- 绘制空间点集:
- 绘制空间平面图:
- 绘制线框图并且与散点图对比:
【1】解析法实现一元线性回归
公式:
代码:
python列表实现
#加载样本数据
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)
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
x=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])
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])
meanX=np.mean(x)
meanY=np.mean(y)
sumXY=np.sum((x-meanX)*(y-meanY))
sumX=np.sum((x-meanX)*(x-meanX))
w=sumXY/sumX
b=meanY-w*meanX
print("w=",w)
print("b=",b)
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
print("面积\t估计房价")
for i in range(y_pred.size):print(x_test[i],"\t",np.round(y_pred[i],2))
利用TensorFlow实现
import tensorflow as tf
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)
print("b=",b)
x_test=tf.constant([128.15,45.00,141.43,106.27,99.00,53.84,85.36,70.00])
y_pred = w*x_test+b
print(y_pred)
数据和模型可视化
#解析法实现一元线性回归
# #Realization of one variable linear regression by analytic method
#导入库
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
#设置字体
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])
#学习模型-计算w,b
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())
print("偏置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()#将张量类型转化为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.xlabel("价格(万元)",fontsize=14)
#设置坐标轴的范围
plt.xlim((40,150))
plt.ylim((40,150))
#设置标题文字和字号
plt.suptitle("商品房销售价格评估系统v1.0",fontsize=20)
#在左上方显示图例
plt.legend(loc="upper left")
#显示整个绘图
plt.show()
结果:
【2】解析法实现多元线性回归
利用Numpy实现
需要用到的NumPy数组运算函数
功能 | 函数 |
---|---|
数组堆叠 | np.stack( ) |
改变数组形状 | np.reshape( ) |
矩阵相乘 | np.matmul( ) |
矩阵转置 | np.transpose( ) |
矩阵求逆 | np.linalg.inv( ) |
公式:
代码:
#解析法实现多元线性回归
#Realization of multiple linear regression by analytic method
#导入库
import numpy as np
#=======================【1】加载样本数据===============================================
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])
#=======================【2】数据处理===============================================
x0=np.ones(len(x1))
#堆叠属性数组,构造属性矩阵
#从(16,)到(16,3),因为新出现的轴是第二个轴所以axis为1
X=np.stack((x0,x1,x2),axis=1)
print(X)
#得到形状为一列的数组
Y=np.array(y).reshape(-1,1)
print(Y)
#=======================【3】求解模型参数===============================================
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)-1X'
W=np.matmul(XtX_1_Xt,Y) #W=((X'X)-1)X'Y
print(W)
W=W.reshape(-1)
print(W)
print("多元线性回归方程")
print("Y=",W[1],"*x1+",W[2],"*x2+",W[0])
#=======================【4】预测房价===============================================
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),"万元")
效果:
数据和模型可视化
绘制空间点集:
#解析法实现多元线性回归
#Realization of multiple linear regression by analytic method
#导入库与模块
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#=======================【1】加载样本数据===============================================
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])
#=======================【2】数据处理===============================================
x0=np.ones(len(x1))
#堆叠属性数组,构造属性矩阵
#从(16,)到(16,3),因为新出现的轴是第二个轴所以axis为1
X=np.stack((x0,x1,x2),axis=1)
print(X)
#得到形状为一列的数组
Y=np.array(y).reshape(-1,1)
print(Y)
#=======================【3】求解模型参数===============================================
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)-1X'
W=np.matmul(XtX_1_Xt,Y) #W=((X'X)-1)X'Y
print(W)
W=W.reshape(-1)
print(W)
print("多元线性回归方程")
print("Y=",W[1],"*x1+",W[2],"*x2+",W[0])
#模型和数据可视化
fig=plt.figure(figsize=(8,6))
#创建3D绘图对象
ax3d=Axes3D(fig)
#用来改变视角的函数
#elev:视角的水平高度 水平旋转的角度
#ax3d.view_init(elev=0,azim=-90)
#绘制散点图
#张量和数组都可以作为散点函数的输入提供点坐标
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)
#设置y轴坐标值刻度
ax3d.set_yticks([1,2,3])
ax3d.set_zlim3d(30,160)
plt.show()
绘制空间平面图:
#解析法实现多元线性回归
#Realization of multiple linear regression by analytic method
#导入库与模块
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#=======================【1】加载样本数据===============================================
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])
#=======================【2】数据处理===============================================
x0=np.ones(len(x1))
#堆叠属性数组,构造属性矩阵
#从(16,)到(16,3),因为新出现的轴是第二个轴所以axis为1
X=np.stack((x0,x1,x2),axis=1)
print(X)
#得到形状为一列的数组
Y=np.array(y).reshape(-1,1)
print(Y)
#=======================【3】求解模型参数===============================================
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)-1X'
W=np.matmul(XtX_1_Xt,Y) #W=((X'X)-1)X'Y
print(W)
W=W.reshape(-1)
print(W)
print("多元线性回归方程")
print("Y=",W[1],"*x1+",W[2],"*x2+",W[0])
#模型和数据可视化
X1,X2=np.meshgrid(x1,x2)
#平面方程
Y_PRED=W[0]+W[1]*X1+W[2]*X2
#创建3D绘图对象
fig=plt.figure()
ax3d=Axes3D(fig)
#绘制空间平面图
ax3d.plot_surface(X1,X2,Y_PRED,cmap="coolwarm")
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.show()
绘制线框图并且与散点图对比:
#解析法实现多元线性回归
#Realization of multiple linear regression by analytic method
#导入库与模块
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#=======================【1】加载样本数据===============================================
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])
#=======================【2】数据处理===============================================
x0=np.ones(len(x1))
#堆叠属性数组,构造属性矩阵
#从(16,)到(16,3),因为新出现的轴是第二个轴所以axis为1
X=np.stack((x0,x1,x2),axis=1)
print(X)
#得到形状为一列的数组
Y=np.array(y).reshape(-1,1)
print(Y)
#=======================【3】求解模型参数===============================================
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)-1X'
W=np.matmul(XtX_1_Xt,Y) #W=((X'X)-1)X'Y
print(W)
W=W.reshape(-1)
print(W)
print("多元线性回归方程")
print("Y=",W[1],"*x1+",W[2],"*x2+",W[0])
y_pred=W[0]+W[1]*x1+W[2]*x2
#设置字体
plt.rcParams['font.sans-serif'] =['SimHei']
#模型和数据可视化
X1,X2=np.meshgrid(x1,x2)
#平面方程
Y_PRED=W[0]+W[1]*X1+W[2]*X2
#创建3D绘图对象
fig=plt.figure()
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()