机器学习算法基础——sklearn求二元线性回归

直接调用sklearn里面封装好的函数即可

import numpy as np 
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D # 可以用来画3D图
from sklearn import linear_model
from numpy import genfromtxt

# 导入数据
data = genfromtxt(r"C:\\ML\\chapter-1\\Delivery.csv",delimiter=",")
print(data)

# 切分数据
x_data = data[:,:-1] # 行,列
y_data = data[:,-1] # 行,列
print(x_data)
print(y_data)

# 创建模型
model = linear_model.LinearRegression()
model.fit(x_data,y_data)

# 系数
print("coefficients:",model.coef_)
# 截距
print("intercept:",model.intercept_)

# 测试
x_test = [[102,4]]
predict = model.predict(x_test)
print("predict:",predict)

ax = plt.figure().add_subplot(111,projection = "3d")
ax.scatter(x_data[:,0],x_data[:,1],y_data,c = 'r',marker = 'o',s = 100) # 点为红色三角形 s代表点的大小
x0 = x_data[:,0]
x1 = x_data[:,1]
# 生成网络矩阵
x0,x1 = np.meshgrid(x0,x1)
z = model.intercept_ + x0*model.coef_[0] + x1*model.coef_[1]
# 画3D图
ax.plot_surface(x0,x1,z)
# 设置坐标轴
ax.set_xlabel('Miles')
ax.set_ylabel("Num of Deliveries")
ax.set_zlabel('Time')

# 显示图像
plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiao黄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值