栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

线性回归——多元线性回归

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

线性回归——多元线性回归

文章目录
    • 理论推导
    • 代码
      • 梯度下降法-二元线性回归-3D绘图
      • sklearn-多元线性回归

理论推导

预测值: θ 0 θ_0 θ0​+ θ 1 θ_1 θ1​ x 1 x_1 x1​+…+ θ n θ_n θn​ x n x_n xn​
       qquad;;; = θ 0 θ_0 θ0​ x 0 x_0 x0​+ θ 1 θ_1 θ1​ x 1 x_1 x1​+…+ θ n θ_n θn​* x n x_n xn​ qquadqquadqquad ( x 0 x_0 x0​=1)
       qquad;;; = θ T θ^T θTx
一个样本的误差:( y ( i ) y^{(i)} y(i) - θ T θ^T θT x ( i ) x^{(i)} x(i))
整个样本集代价函数(最小二乘法):J(θ)= 1 2 m 1 over 2m 2m1​ ∑ i = 1 m sum_{i=1}^{m} ∑i=1m​ ( y ( i ) − θ T ∗ x ( i ) ) 2 (y^{(i)}-θ^T*x^{(i)})^2 (y(i)−θT∗x(i))2
采用梯度下降法进行优化:
θ 0 θ_0 θ0​= θ 0 θ_0 θ0​- 1 m 1 over m m1​ ∑ i = 1 m sum_{i=1}^{m} ∑i=1m​ ( y ( i ) − θ T ∗ x ( i ) ) (y^{(i)}-θ^T*x^{(i)}) (y(i)−θT∗x(i))×(- x 0 x_0 x0​)
θ 1 θ_1 θ1​= θ 1 θ_1 θ1​- 1 m 1 over m m1​ ∑ i = 1 m sum_{i=1}^{m} ∑i=1m​ ( y ( i ) − θ T ∗ x ( i ) ) (y^{(i)}-θ^T*x^{(i)}) (y(i)−θT∗x(i))×(- x 1 x_1 x1​)
.
θ n θ_n θn​= θ n θ_n θn​- 1 m 1 over m m1​ ∑ i = 1 m sum_{i=1}^{m} ∑i=1m​ ( y ( i ) − θ T ∗ x ( i ) ) (y^{(i)}-θ^T*x^{(i)}) (y(i)−θT∗x(i))×(- x n x_n xn​)

代码 梯度下降法-二元线性回归-3D绘图
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#读入数据
data=np.genfromtxt("Delivery.csv",delimiter=',')
print(data)

#切分数据
#样本个特征值-不要最后一列
x=data[:,:-1]
#真实值-最后一列
y=data[:,-1]
print(x)
print(y)

#学习率
learning_rate=0.0001
#待确定参数
th0=0
th1=0
th2=0
#迭代次数
loop=1000

#梯度下降法——二元:y=th0+th1*x[0]*th2*x[1]
def gradient_descent(x,y,th0,th1,th2,learning_rate,loop):
    #数据总量
    m=float(len(x))
    for i in range(loop):
        #临时变量
        th0_temp=0
        th1_temp=0
        th2_temp=0
        for j in range(0,len(x)):
            th0_temp+=-(1/m)*(y[j]-th0-th1*x[j,0]-th2*x[j,1])
            #求导有负号
            th1_temp+=-(1/m)*(y[j]-th0-th1*x[j,0]-th2*x[j,1])*x[j,0]
            th2_temp+=-(1/m)*(y[j]-th0-th1*x[j,0]-th2*x[j,1])*x[j,1]
        #更新三个参数
        th0-=learning_rate*th0_temp
        th1-=learning_rate*th1_temp
        th2-=learning_rate*th2_temp
    return th0,th1,th2

#损失
def error (th0,th1,th2,x,y):
    totalError=0
    for i in range(len(x)):
        totalError+=(y[i]-th0-th1*x[i,0]-th2*x[i,1])**2
    return totalError/float(len(x))
th0,th1,th2=gradient_descent(x,y,th0,th1,th2,learning_rate,loop)
print("{0},{1},{2},{3}".format(th0,th1,th2,error(th0,th1,th2,x,y)))

#画图-3d
ax=plt.figure().add_subplot(111,projection='3d')
#散点图
ax.scatter(x[:,0],x[:,1],y,c='r',marker='o',s=100)
x1=x[:,0]
x2=x[:,1]
#生成网格矩阵
x1,x2=np.meshgrid(x1,x2)
z=th0+th1*x1+th2*x2
ax.plot_surface(x1,x2,z)
#设置坐标轴
ax.set_xlabel('x1')
ax.set_ylabel('x2')
ax.set_zlabel('y')
#图像显示
plt.show()

sklearn-多元线性回归
import numpy as np
from sklearn import linear_model
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#读入数据
data=np.genfromtxt("Delivery.csv",delimiter=',')
print(data)

#切分数据
x=data[:,:-1]
y=data[:,-1]
print(x)
print(y)

#创建模型
model=linear_model.LinearRegression()
#填入参数
model.fit(x,y)

#输出待确定的系数
print("各待确定系数为:",model.coef_)
#输出截距=偏置
print("截距:",model.intercept_)
#测试
test=[[102,4]]
answer_test=model.predict(test)
print("测试结果",answer_test)

#画图-3d
ax=plt.figure().add_subplot(111,projection='3d')
#散点图
ax.scatter(x[:,0],x[:,1],y,c='r',marker='o',s=100)
x1=x[:,0]
x2=x[:,1]
#生成网格矩阵
x1,x2=np.meshgrid(x1,x2)
z=model.intercept_+x1*model.coef_[0]+x2*model.coef_[1]
#画3d图
ax.plot_surface(x1,x2,z)
#设置坐标轴
ax.set_xlabel('x1')
ax.set_ylabel('x2')
ax.set_zlabel('y')
#图像显示
plt.show()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1010095.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号