当前位置:网站首页>机器学习之线性回归简单实例
机器学习之线性回归简单实例
2022-06-11 21:52:00 【CHRN晨】
线性回归
简介:线性回归目标是提取输入变量和输出变量的关联线性模型。线性回归属于有监督学习,有监督学习的基本架构和框架如下:
1.准备训练数据,可以是文本数据、图像数据和音频数据
2.然后抽取所需要的特征形成特征向量。
3.把这些特征向量已经对应的目标一起导入机器学习算法模型中,训练出一个预测模型。
4.采用同样的特征抽取方法作用与新数据,得到用于测试的特征向量。
5.最后使用预测模型对这些待测试的特征向量进行预测得到结果。
线性回归也不例外,下面运用一个简单的实例来了解线性回归。
(1)导入数据
#线性回归:最小二乘法
import numpy as np
x=[]
y=[]
with open('filename','r') as f:
for line in f.readlines():
xt,yt=[float(i) for i in line.split(',')]
x.append(xt)
y.append(yt)
(2)在建立机器学习模型时要用一种方法来验证模型,检查模型是否达到一定的满意度,我们可以把数据分成两组,一组用来训练,一组用来测试。
训练集用来建立模型,测试集用来验证模型对未知数据的预测效果。
本例用80%的数据训练,20%的数据用来测试。
#切割训练和测试数据
num_training=int(0.8*len(x))
num_test=len(x)-num_training
#reshape改变数组形状reshape((num_training,1)
#本例 生成num_training行,1列的数组
#训练数据
x_train=np.array(x[:num_training]).reshape((num_training,1))
y_train=np.array(y[:num_training])
#测试数据
x_test=np.array(x[:num_training]).reshape((num_test,1))
y_test=np.array(y[:num_training])
(3)训练数据
#创建线性回归对象
from sklearn import linear_model
linear_regressor=linear_model.LinearRegession()
#用训练数据训练模型
linear_regressor.fit(x_train,y_train)
(4)预测数据
#用训练好的模型对测试机上的数据预测
y_test_pred=linear_regressor.predict(x_test)
(5)可视化分析
import matplotlib.pyplot as plt
plt.scatter(x_test,y_test,color='green')
plt.plot(x_test,y_test,color='green',linewidth=4)
plt.show()
(6)对模型进行评价
评价指标主要有:
平均绝对误差(Mean absolute error):给定数据集的所有数据点的误差和的平均值

均方误差(Mean squared error):各数据偏离真实值差值的平方和的平均数
中位数绝对误差(Median absolute error):给定数据集的所有数据点的的误差的中位数
解释方差得分(Explain variance score):衡量模型对数据集波动的解释能力,若为1表示模型完美
R方得分(R2 score):指确定性相关系数,用来衡量模型对位置样本的预测效果,最好得分为1。
import sklearn.metrics as sm
#round()方法:将数字四舍五入为仅有两个小数
print("Mean absolute error=",
round(sm_mean_absolute_error(y_test,y_test_pred),2))
print("Mean squared error=",
round(sm_mean_squared_error(y_test,y_test_pred),2))
print("Median absolute error=",
round(sm_median_absolute_error(y_test,y_test_pred),2))
print("Explain variance score=",
round(sm_explain_variance_score(y_test,y_test_pred),2))
print("R2 score =",
round(sm_r2_score(y_test,y_test_pred),2))
边栏推荐
- 多态的所有特征
- 高考结束,人生才刚刚开始,10年职场老鸟给的建议
- Example of using zypper command
- Conception du Processeur superscalaire Yao yongbin chapitre 2 cache - - sous - section 2.4 extrait
- 超标量处理器设计 姚永斌 第2章 Cache --2.2 小节摘录
- Three methods of quick sorting
- Classes and objects (3)
- Look for leap years and see how many leap years I have had since I was born (I have had five)
- 快速排序的三种方法
- In the future, cloud expansion technology is expected to be selected as a specialized, special and new enterprise in Shanghai
猜你喜欢

Explain asynchronous tasks in detail: the task of function calculation triggers de duplication

Tkinter学习笔记(二)

《物联网开发实战》18 场景联动:智能电灯如何感知光线?(上)(学习笔记)

详解异步任务:函数计算的任务触发去重

In the post epidemic era, how can enterprise CIOs improve enterprise production efficiency through distance

R语言相关文章、文献整理合集(持续更新)

R语言书籍学习03 《深入浅出R语言数据分析》-第七章 线性回归模型

Regular execution of shell scripts in crontab

科普 | NFT的类型有哪些(上)

R语言书籍学习03 《深入浅出R语言数据分析》-第八章 逻辑回归模型 第九章 聚类模型
随机推荐
Players must read starfish NFT advanced introduction
Unity3D getLaunchIntentForPackage 获取包返回null问题
类和对象(4)
3.3 测试模块的命名规则
189. rotation array
Static PVC with CEPH CSI
Custom implementation offsetof
2022-02-28(2)
图书管理系统
联调这夜,我把同事打了...
超标量处理器设计 姚永斌 第2章 Cache --2.2 小节摘录
Nmap进行主机探测出现网段IP全部存活情况分析
Example of using zypper command
Add anti debugging function to game or code (application level)
Top - k问题
超标量处理器设计 姚永斌 第2章 Cache --2.4 小节摘录
C语言实现迷宫问题
Introduction to MySQL transactions
Tkinter学习笔记(二)
Huawei equipment configuration h-vpn