sklearn速度复习-多元线性回归

import numpy as np
from numpy import genfromtxt
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

#导入数据并切分成XY
data=np.genfromtxt("data.csv",delimiter=',')
x_data=data[:,:-1]#读出第零列到倒数第二列
y_data=data[:,-1]#读出倒数第一列
print(x_data)
print(y_data)

#参数
lr=0.0001#学习率learning rate
theta0=0#系数
theta1=0#系数
theta2=0#截距
epochs=1000#最大迭代次数

#最小二乘法
def compute_error(theta0,theta1,theta2,x_data,y_data):
    totalError=0
    for i in range(0,len(x_data)):
        totalError+=(y_data[i]-(theta1*x_data[i,0]+theta2*x_data[i,1]+theta0))**2
    return totalError/float(len(x_data))
#梯度下降法
def gradient_descent_runner(x_data,y_data,theta0,theta1,theta2,lr,epochs):
    #计算总数据量
    m=float(len(x_data))
    #循环epochs次
    for i in range(epochs):
        theta0_grad=0
        theta1_grad=0
        theta2_grad=0
        #计算梯度的总和再求平均
        for j in range(0,len(x_data)):
            theta0_grad+=-(1/m)*(y_data[j]-(theta1*x_data[j,0]+theta2*x_data[j,1]+theta0))
            theta1_grad+=-(1/m)*x_data[j,0]*(y_data[j]-(theta1*x_data[j,0]+theta2*x_data[j,1]+theta0))
            theta2_grad+=-(1/m)*x_data[j,1]*(y_data[j]-(theta1*x_data[j,0]+theta2*x_data[j,1]+theta0))
        #更新b和k
        theta0=theta0-(lr*theta0_grad)
        theta1=theta1-(lr*theta1_grad)
        theta2=theta2-(lr*theta2_grad)
    return theta0,theta1,theta2
#开始训练
theta0,theta1,theta2=gradient_descent_runner(x_data,y_data,theta0,theta1,theta2,lr,epochs)
#下面是绘图
ax=plt.figure().add_subplot(111,projection='3d')
ax.scatter(x_data[:,0],x_data[:,1],y_data,c='r',marker='o',s=100)#点为红色三角形
x0=x_data[:,0]
x1=x_data[:,1]
#生成网络矩阵
x0,x1=np.meshgrid(x0,x1)
z=theta0+x0*theta1+x1*theta2
#画3D图
ax.plot_surface(x0,x1,z)
#设置坐标轴
ax.set_xlabel('Miles')
ax.set_ylabel('Num of Deliveries')
ax.set_zlabel('Time')
#显示图像
plt.show()

100	4	9.3
50	3	4.8
100	4	8.9
100	2	6.5
50	2	4.2
80	2	6.2
75	3	7.4
65	4	8
90	3	7.6
90	2	6.1
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
#导入数据
data=np.genfromtxt("data.csv",delimiter=',')
x_data=data[1:,1,np.newaxis]
y_data=data[1:,2,np.newaxis]
'''
#线性拟合
model=LinearRegression()
model.fit(x_data,y_data)
plt.plot(x_data,y_data,'b.')
plt.plot(x_data,model.predict(x_data),'r')
plt.show()
'''
#多项式拟合
poly_reg=PolynomialFeatures(degree=4)#设置多少次方
x_poly=poly_reg.fit_transform(x_data)#自变量升维(特征)
print(x_poly)#更改上面的degre值为2,就会有1,X,X^2的特征
lin_reg=LinearRegression()#注意!依然是线性回归!因为X由一个特征变成了多个特征
lin_reg.fit(x_poly,y_data)#训练模型
plt.plot(x_data,y_data,'b.')
plt.plot(x_data,lin_reg.predict(poly_reg.fit_transform(x_data)),c='r')
plt.show()

Position	Level	Salary
AAA	1	45000
BBB	2	50000
CCC	3	60000
DDD	4	80000
EEE	5	110000
FFF	6	150000
GGG	7	200000
HHH	8	300000
III	9	500000
JJJ	10	1000000


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值