前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >机器学习-R-特征选择

机器学习-R-特征选择

作者头像
机器学习AI算法工程
发布2018-03-13 15:35:51
2K0
发布2018-03-13 15:35:51
举报

特征选择是实用机器学习的重要一步,一般数据集都带有太多的特征用于模型构建,如何找出有用特征是值得关注的内容。

1. Feature selection: All-relevant selection with the Boruta package

特征选择两种方法用于分析:

(1)最少最优特征选择(minimal-optimal feature selection)识别少量特征集合(理想状况最少)给出尽可能优的分类结果;

(2)所有相关特征选择(all-relevant feature selection)识别所有与分类有关的所有特征。

本文使用Boruta包,它使用随机森林分类算法,测量每个特征的重要行(z score)。

2. 使用caret包

使用递归特征消除法,rfe参数

x,预测变量的矩阵或数据框

y,输出结果向量(数值型或因子型)

sizes,用于测试的特定子集大小的整型向量

rfeControl,用于指定预测模型和方法的一系列选项

一些列函数可以用于rfeControl$functions,包括:线性回归(lmFuncs),随机森林(rfFuncs),朴素贝叶斯(nbFuncs),bagged trees(treebagFuncs)和可以用于caret的train函数的函数(caretFuncs)。

1)移除冗余特征

移除高度关联的特征。

Caret R包提供findCorrelation函数,分析特征的关联矩阵,移除冗余特征

[python] view plain copy

  1. set.seed(7)
  2. # load the library
  3. library(mlbench)
  4. library(caret)
  5. # load the data
  6. data(PimaIndiansDiabetes)
  7. #P calculate correlation matrix
  8. correlationMatrix <- cor(PimaIndiansDiabetes[,1:8])
  9. # summarize the correlation matrix
  10. print(correlationMatrix)
  11. # find attributes that are highly corrected (ideally >0.75)
  12. highlyCorrelated <- findCorrelation(correlationMatrix, cutoff=0.5)
  13. # print indexes of highly correlated attributes
  14. print(highlyCorrelated)

2) 根据重要性进行特征排序

特征重要性可以通过构建模型获取。一些模型,诸如决策树,内建有特征重要性的获取机制。另一些模型,每个特征重要性利用ROC曲线分析获取。

下例加载Pima Indians Diabetes数据集,构建一个Learning Vector Quantization(LVQ)模型。varImp用于获取特征重要性。从图中可以看出glucose, mass和age是前三个最重要的特征,insulin是最不重要的特征。

  1. # ensure results are repeatable
  2. set.seed(7)
  3. # load the library
  4. library(mlbench)
  5. library(caret)
  6. # load the dataset
  7. data(PimaIndiansDiabetes)
  8. # prepare training scheme
  9. control <- trainControl(method="repeatedcv", number=10, repeats=3)
  10. # train the model
  11. model <- train(diabetes~., data=PimaIndiansDiabetes, method="lvq", preProcess="scale", trControl=control)
  12. # estimate variable importance
  13. importance <- varImp(model, scale=FALSE)
  14. # summarize importance
  15. print(importance)
  16. # plot importance
  17. plot(importance)

3)特征选择

自动特征选择用于构建不同子集的许多模型,识别哪些特征有助于构建准确模型,哪些特征没什么帮助。

特征选择的一个流行的自动方法称为 递归特征消除(Recursive Feature Elimination)或RFE。

下例在Pima Indians Diabetes数据集上提供RFE方法例子。随机森林算法用于每一轮迭代中评估模型的方法。该算法用于探索所有可能的特征子集。从图中可以看出当使用4个特征时即可获取与最高性能相差无几的结果。

  1. # ensure the results are repeatable
  2. set.seed(7)
  3. # load the library
  4. library(mlbench)
  5. library(caret)
  6. # load the data
  7. data(PimaIndiansDiabetes)
  8. # define the control using a random forest selection function
  9. control <- rfeControl(functions=rfFuncs, method="cv", number=10)
  10. # run the RFE algorithm
  11. results <- rfe(PimaIndiansDiabetes[,1:8], PimaIndiansDiabetes[,9], sizes=c(1:8), rfeControl=control)
  12. # summarize the results
  13. print(results)
  14. # list the chosen features
  15. predictors(results)
  16. # plot the results
  17. plot(results, type=c("g", "o"))

原文:http://blog.csdn.net/python_learn/article/details/45008073

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

本文分享自 大数据挖掘DT数据分析 微信公众号,前往查看

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

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

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