众所周知,glmnet
可以作为一种工具用于特征选择。以下是一个玩具示例:
library(glmnet)
# Binomial dataset, the number of classes is 2
data(BinomialExample)
# data truncation to 10 columns, just to make the example dataset smaller
x <- BinomialExample$x[,1:10]
y <- BinomialExample$y
cvfit = cv.glmnet(x, y, family = "binomial")
coefs <- coef(cvfit)
coefs
变量显示已选择的特征(在此示例中除了 V1
和 V7
之外的所有特征)。这个结果清晰易懂。> coefs
11 x 1 sparse Matrix of class "dgCMatrix"
s1
(Intercept) 0.1048257
V1 .
V2 0.5901863
V3 -0.4060696
V4 -0.9627180
V5 -0.1067188
V6 -0.7813739
V7 .
V8 -0.4106554
V9 0.5733065
V10 -1.0492793
问题在于如何解释输出结果,如果分类数量超过两个。以下是一个玩具例子:
# Multinomial, the number of classes is 3
data(MultinomialExample)
x <- MultinomialExample$x[,1:10]
y <- MultinomialExample$y
cvfit = cv.glmnet(x, y, family = "multinomial")
coefs <- coef(cvfit)
现在,coefs
存储了三个要选择的特征集合。
问题:应该使用哪个集合作为 最佳特征集合?
换句话说:当我们有超过两类时,是否可以将 glmnet
用作特征选择工具?
> coefs
$`1`
11 x 1 sparse Matrix of class "dgCMatrix"
1
(Intercept) -0.03279324
V1 .
V2 -0.08585827
V3 0.40882396
V4 -0.08639670
V5 -0.15763031
V6 0.22513768
V7 .
V8 0.17657623
V9 .
V10 .
$`2`
11 x 1 sparse Matrix of class "dgCMatrix"
1
(Intercept) 0.01255996
V1 -0.21913800
V2 .
V3 .
V4 .
V5 0.41329881
V6 .
V7 .
V8 .
V9 -0.57131512
V10 0.52214739
$`3`
11 x 1 sparse Matrix of class "dgCMatrix"
1
(Intercept) 0.02023328
V1 0.09163282
V2 0.42655929
V3 .
V4 0.29403632
V5 .
V6 -0.12306560
V7 .
V8 -0.44815059
V9 0.88580234
V10 -0.20920812