线性回归算法例子(Linear Regression)|机器学习

在这里插入图片描述

#线性回归算法
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

#模拟数据

#通过指定开始值、终值和步长创建一维等差数组,但其数组中不包含终值
x = np.linspace(0,10,50)
#uniform() 方法将随机生成下一个实数,它在 [x, y] 范围内
#size是生成随机数的数量
#uniform模块不能直接访问,必须导入random模块
noise = np.random.uniform(-2,2,size=50)

y = 5 * x + 6 + noise

#创建模型
liner = LinearRegression()

#拟合模型
#如果等于-1的话,那么Numpy会根据剩下的维度计算出数组的另外一个shape属性值
#numpy.reshape(a, newshape, order=‘C’)[source]
#50行一列(50*1)
liner.fit(np.reshape(x,(-1,1)),np.reshape(y,(-1,1)))
print(liner)

#预测
y_pred = liner.predict(np.reshape(x,(-1,1)))
#figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)
# num:图像编号或名称,数字为编号 ,字符串为名称
# figsize:指定figure的宽和高,单位为英寸;
# dpi参数指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80      1英寸等于2.5cm,A4纸是 21*30cm的纸张
# facecolor:背景颜色
# edgecolor:边框颜色
# frameon:是否显示边框
plt.figure(figsize=(5,5))
plt.scatter(x,y)
plt.plot(x,y_pred,color='r')
plt.show()
#corf_斜率
#intercept_截距
print(liner.coef_)
print(liner.intercept_)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值