可用自动特征选择的R包.

  1. caret
  2. Boruta
  3. fs
  4. FSinR

FSinR

FSinR包包含执行特征选择过程的函数。更具体地说,它包含大量在文献中广泛使用的过滤器和包装器方法,这些方法与搜索算法相结合以获得最佳的特征子集。

如上所述,特征选择过程旨在获得最优的特征子集。为此,将搜索算法与过滤器/包装器方法相结合。搜索算法根据评估子集的过滤器/包装方法返回的结果来指导特征空间中的过程。在此过程中获得的最佳特征子集对于生成更简单的模型非常有用,这些模型需要更少的计算资源并呈现出更好的最终性能。

特征选择过程是用featureSelection函数完成的。参数包括:

  1. data: 矩阵或者数据框数据
  2. class: 因变量的名称
  3. searcher: 搜索算法
  4. evaluator: 评估方法(过滤器或包装器方法)

搜索算法

包中的搜索功能如下:

  1. Sequential forward selection 顺序向前选择
  2. Sequential floating forward selection 顺序浮动向前选择
  3. Sequential backward selection 顺序向后选择
  4. Sequential floating backward selection 顺序浮动向后选择
  5. Breadth first search 广度优先搜索
  6. Deep first search 深度优先搜索
  7. Genetic algorithm 遗传算法
  8. Whale optimization algorithm 鲸鱼优化算法
  9. Ant colony optimization 蚁群优化
  10. Simulated annealing 模拟退火
  11. Tabu search 禁忌搜索
  12. Hill-Climbing 爬山
  13. Las Vegas wrapper 拉斯维加斯包

searchAlgorithm允许选择要在特征选择过程中使用的搜索算法.

过滤方法

  1. Chi-squared 卡方
  2. Cramer
  3. F-score
  4. Relief
  5. Rough Sets consistency
  6. Binary consistency
  7. Inconsistent Examples consistency
  8. Inconsistent Examples Pairs consistency
  9. Determination Coefficient
  10. Mutual information 互信息
  11. Gain ratio 增益比
  12. Symmetrical uncertain 对称不确定
  13. Gini index 基尼系数
  14. Jd
  15. MDLC
  16. ReliefFeatureSetMeasure

FSinR 包允许使用 caret 包中可用的 238 个模型作为包装方法。这里查看可用模型:https://topepo.github.io/caret/available-models.html

例子

library(caret)
## Loading required package: ggplot2
## Loading required package: lattice
library(FSinR)

data(iris)

搜索+包装

首先,必须生成包装器方法。为此,必须调用该wrapperEvaluator函数并确定要将哪个模型用作包装器方法。由于鸢尾花问题是一个分类问题,在例子中我们使用了knn模型。

evaluator <- wrapperEvaluator("knn")

下一步是生成搜索算法。这是通过调用searchAlgorithm函数并指定要使用的算法来完成的。

searcher <- searchAlgorithm('sequentialForwardSelection') # searchAlgorithm 可以查看可选

一旦我们生成了搜索算法和包装器方法,我们就会调用执行特征选择过程的主函数。

results <- featureSelection(iris, 'Species', searcher, evaluator)

结果显示找到的最佳特征子集及其评估。

results
## $bestFeatures
##      Sepal.Length Sepal.Width Petal.Length Petal.Width
## [1,]            1           1            1           1
## 
## $bestValue
## [1] 0.9604503
## 
## $evaluationType
## [1] "Wrapper"
## 
## $evaluationMethod
## [1] "knn"
## 
## $measureType
## [1] "Set measure"
## 
## $searchMethod
## [1] "Sequential Forward Selection"
## 
## $target
## [1] "unspecified"
## 
## $numFeatures
## [1] 4
## 
## $xNames
## [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width" 
## 
## $yName
## [1] "Species"
## 
## $time
##    user  system elapsed 
##   5.148   0.110   5.361

搜索+过滤

这个例子与上一个非常相似。主要区别在于生成过滤器方法作为评估器而不是包装器方法。这是通过函数完成的filterEvaluator。

evaluator <- filterEvaluator('MDLC')

然后生成搜索算法:

searcher <- searchAlgorithm('sequentialForwardSelection')

最后执行特征选择过程

results <- featureSelection(iris, 'Species', searcher, evaluator)

查看结果

results
## $bestFeatures
##      Sepal.Length Sepal.Width Petal.Length Petal.Width
## [1,]            0           1            0           0
## 
## $bestValue
## [1] 98.96749
## 
## $evaluationType
## [1] "Filter"
## 
## $evaluationMethod
## [1] "MDLC"
## 
## $measureType
## [1] "Set measure"
## 
## $searchMethod
## [1] "Sequential Forward Selection"
## 
## $target
## [1] "maximize"
## 
## $numFeatures
## [1] 4
## 
## $xNames
## [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width" 
## 
## $yName
## [1] "Species"
## 
## $time
##    user  system elapsed 
##   0.505   0.023   0.545

直接特征选择过程

使用directFeatureSelection函数执行直接特征选择过程。此函数包含与函数相同的参数featureSelection,不同之处在于 searcher 参数被 directSearcher 参数替换。 该函数主要返回选定的特征子集,以及对这些特征中的每一个的单独评价值。

包中存在的直接搜索功能如下

  1. Select k best 选择 k 最佳
  2. Select percentile 选择百分位数
  3. Select threshold 选择阈值
  4. Select threshold range 选择阈值范围
  5. Select difference 选择差异
  6. Select slope: 选择坡度

来看一个例子,主要变化是在这种情况下必须生成直接搜索算法。此外,用于执行特征选择过程的函数也更改为directFeatureSelection

library(caret)
library(FSinR)

data(mtcars)


evaluator <- filterEvaluator('determinationCoefficient')

directSearcher <- directSearchAlgorithm('selectKBest', list(k=3))

results <- directFeatureSelection(mtcars, 'mpg', directSearcher, evaluator)

results
## $bestFeatures
##      cyl disp hp drat wt qsec vs am gear carb
## [1,]   1    1  0    0  1    0  0  0    0    0
## 
## $featuresSelected
## [1] "wt"   "cyl"  "disp"
## 
## $valuePerFeature
## [1] 0.7528328 0.7261800 0.7183433
## 
## $evaluationType
## [1] "Filter"
## 
## $evaluationMethod
## [1] "Determination Coefficient"
## 
## $searchMethod
## [1] "Select K Best"
## 
## $target
## [1] "maximize"
## 
## $numFeatures
## [1] 10
## 
## $xNames
##  [1] "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear" "carb"
## 
## $yName
## [1] "mpg"
## 
## $time
##    user  system elapsed 
##   0.012   0.000   0.013