可用自动特征选择的R包.
FSinR包包含执行特征选择过程的函数。更具体地说,它包含大量在文献中广泛使用的过滤器和包装器方法,这些方法与搜索算法相结合以获得最佳的特征子集。
如上所述,特征选择过程旨在获得最优的特征子集。为此,将搜索算法与过滤器/包装器方法相结合。搜索算法根据评估子集的过滤器/包装方法返回的结果来指导特征空间中的过程。在此过程中获得的最佳特征子集对于生成更简单的模型非常有用,这些模型需要更少的计算资源并呈现出更好的最终性能。
特征选择过程是用featureSelection函数完成的。参数包括:
包中的搜索功能如下:
searchAlgorithm允许选择要在特征选择过程中使用的搜索算法.
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 参数替换。 该函数主要返回选定的特征子集,以及对这些特征中的每一个的单独评价值。
包中存在的直接搜索功能如下
来看一个例子,主要变化是在这种情况下必须生成直接搜索算法。此外,用于执行特征选择过程的函数也更改为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