使用LinearRegression对数据进行预测

  • 1.预测两个点
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt

# # 预测两个点
# # 两个点的横坐标
X = [[1], [6]]
# # 两个点的纵坐标
y = [4, 6]

lrs = LinearRegression().fit(X, y)
plt.scatter(X, y, s=80)
z = np.linspace(0, 5, 20)
plt.xlim(0, 7)
plt.ylim(3, 7)
plt.plot(z, lrs.predict(z.reshape(-1, 1)))
# print("打印直线方程", "y=", '{:.3f}'.format(lrs.coef_[0]), "x", "+", '{:.3f}'.format(lrs.intercept_))
print("打印直线方程:y={:.3f}x+{:.3f}".format(lrs.coef_[0], lrs.intercept_))
plt.show()

拟合情况:
*

在这里插入图片描述

直线方程为:在这里插入图片描述

  • 2.预测三个点的情况。
# 预测三个点
X = [[1], [4], [3]]
y = [3, 5, 3]
lrs = LinearRegression().fit(X, y)
plt.scatter(X, y, c='orange')
z = np.linspace(0, 5, 20)
plt.plot(z, lrs.predict(z.reshape(-1, 1)), c='red')
print("打印方程:y={:.3f}x+{:.3f}".format(lrs.coef_[0], lrs.intercept_))
plt.show()

拟合情况:
*

在这里插入图片描述
直线方程: 在这里插入图片描述

  • 3.预测多个点是:
# 预测多个点
from sklearn.datasets import make_regression

X, y = make_regression(n_samples=50, n_features=1, n_informative=1, noise=50, random_state=1)
lrs = LinearRegression().fit(X, y)
z = np.linspace(-3, 3, 500)
print("打印方程:y={:.3f}x+{:.3f}".format(lrs.coef_[0], lrs.intercept_))

plt.scatter(X, y, c='orange')
plt.plot(z, lrs.predict(z.reshape(-1, 1)))
plt.show()

拟合情况:*

在这里插入图片描述
直线方程: 在这里插入图片描述

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值