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

sklearn线性回归,函数拟合,加噪声扰动

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

sklearn线性回归,函数拟合,加噪声扰动

#2021.10.16 HIT ATCI LZH
#sklearn线性回归,函数拟合,加噪声扰动
from sklearn import linear_model
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.python.ops.variables import local_variables
#生成数据集
x = np.linspace(-1, 1, 50) #x,y 此时是list列表
y = 2*x + 1
y = y + np.random.rand(50)
#Sklearn中线性回归算法的fit方法需要传进去的x,y是两组矩阵,而不能是List,因此需要把list转变为矩阵
x = [[i] for i in x]
y = [[i] for i in y]
#训练线性回归模型
model = linear_model.LinearRegression()
model.fit(x, y)
#进行预测
w_ = model.coef_  #预测的权值
b_ = model.intercept_ #预测的截距
x_ = [[1], [3]]  #测试值
y_ = model.predict(x_)
print('y_ = ', y_)
print('w_ = ', w_)
print('b_ ', b_)

#数据集绘图
plt.figure(1)
plt.scatter(x, y, label = 'Real Date')#b代表蓝色, o代表圆点
plt.plot(x,x*w_ + b_, 'r', label = 'Predict Data')#r代表红色
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.title('Real : y = 2*x + 1  Predict: y = {0}*x + {1}'.format(w_, b_))
plt.show()

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

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

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