前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >特征工程(一):前向逐步回归(R语言)

特征工程(一):前向逐步回归(R语言)

作者头像
三猫
发布2018-04-10 11:36:06
1.4K0
发布2018-04-10 11:36:06
举报
文章被收录于专栏:机器学习养成记

建模过程中,选择合适的特征集合,可以帮助控制模型复杂度,防止过拟合等问题。为了选取最佳的特征集合,可以遍历所有的列组合,找出效果最佳的集合,但这样需要大量的计算。本文介绍的前向逐步回归法是针对最小二乘法的修改。相对于要将所有组合情况遍历一遍,前向逐步回归可以大大节省计算量,选择最优的特征集合,从而解决过拟合问题。

  • 前向逐步回归

前向逐步回归的过程是:遍历属性的一列子集,选择使模型效果最好的那一列属性。接着寻找与其组合效果最好的第二列属性,而不是遍历所有的两列子集。以此类推,每次遍历时,子集都包含上一次遍历得到的最优子集。这样,每次遍历都会选择一个新的属性添加到特征集合中,直至特征集合中特征个数不能再增加。

  • 实例代码

1、数据导入并分组。导入数据,将数据集抽取70%作为训练集,剩下30%作为测试集。特征与标签分开存放。

target.url <- "http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv" data <- read.csv(target.url,header = T,sep=";") #divide data into training and test sets index <- which((1:nrow(data))%%3==0) train <- data[-index,] test <- data[index,] #arrange date into list and label sets trainlist <- train[,1:11] testlist <- test[,1:11] trainlabel <- train[,12] testlabel <- test[,12]

2、前向逐步回归构建输出特征集合。通过for循环,从属性的一个子集开始进行遍历。第一次遍历时,该子集为空。每一个属性被加入子集后,通过线性回归来拟合模型,并计算在测试集上的误差,每次遍历选择得到误差最小的一列加入输出特征集合中。最终得到输出特征集合的关联索引和属性名称。

#build list of attributes one-at-a-time, starting with empty attributeList<-as.numeric() index<-1:ncol(trainlist) indexSet<-as.numeric() oosError<-as.numeric() for(i in index){ #attributes not in list already attTry<-setdiff(index,attributeList) #try each attribute not in set to see which one gives least oos error errorList<-as.numeric() attTemp<-as.numeric() for(ii in attTry){ attTemp<-append(attTemp,attributeList) attTemp<-append(attTemp,ii) xTrainTemp<-as.data.frame(trainlist[,attTemp]) xTestTemp<-as.data.frame(testlist[,attTemp]) names(xTrainTemp)<-names(trainlist[attTemp]) names(xTestTemp)<-names(testlist[attTemp]) lm.mod <- lm(trainlabel~.,data=xTrainTemp) rmsError<-rmse(testlabel,predict(lm.mod,(xTestTemp))) errorList<-append(errorList,rmsError) attTemp<-as.numeric() } iBest<-which.min(errorList) attributeList<-append(attributeList,attTry[iBest]) oosError<-append(oosError,errorList[iBest]) } cat("Best attribute indices: ", attributeList, "\n","Best attribute names: \n",names(trainlist[attributeList]))

索引与名称如下:

Best attribute indices: 11 2 10 7 6 9 1 8 4 3 5 Best attribute names: alcohol volatile.acidity sulphates total.sulfur.dioxide free.sulfur.dioxide pH fixed.acidity density residual.sugar citric.acid chlorides

属性名列表的顺序也是属性的重要性排序,了解属性重要性,可以增加模型的解释性。

3、模型效果评估。分别画出RMSE与属性个数之间的关系,前向逐步预测算法对数据预测对错误直方图,和真实标签与预测标签散点图。

plot(oosError,type = "l",xlab = "Number of Attributes",ylab = "ERMS",main = "error versus number of attributes") finaltrain<-trainlist[,attributeList[1:which.min(oosError)]] finaltest<-testlist[,attributeList[1:which.min(oosError)]] lm.finalmol<-lm(trainlabel~.,data = finaltrain) finalpre<-predict(lm.finalmol,finaltest) errorVector<-testlabel-finalpre hist(errorVector) plot(predict(lm.finalmol,finaltest),testlabel,xlab = "Predicted Taste Score",ylab = "Actual Taste Score")

从图上可以看出,使用前9个属性,误差值一直在降低,加入第十个属性后,误差值开始增加。因此,我们选取输出特征集合的前9项,作为最终的最优特征集合。从散点图上看,得分在5、6时,预测情况非常好,因为区域的颜色深度可以反映点的堆积程度,一般情况下,机器学习算法对边缘数据预测效果不好。由于真正的标签是整数,所以散点图呈水平状分布。后两张图,均可通过分析图像形态,指出模型性能提升途径。


本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-12-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 机器学习养成记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档