使用scikit-learn中的rbf内核对SVM使用递归特征消除的ValueError

时间:2014-05-21 22:57:45

标签: python scikit-learn rfe

我尝试在scikit-learn中使用递归功能消除(RFE)功能,但不断收到错误ValueError: coef_ is only available when using a linear kernel。我正在尝试使用rbf内核为支持向量分类器(SVC)执行功能选择。网站上的这个例子执行得很好:

print(__doc__)

from sklearn.svm import SVC
from sklearn.cross_validation import StratifiedKFold
from sklearn.feature_selection import RFECV
from sklearn.datasets import make_classification
from sklearn.metrics import zero_one_loss

# Build a classification task using 3 informative features
X, y = make_classification(n_samples=1000, n_features=25, n_informative=3,
                       n_redundant=2, n_repeated=0, n_classes=8,
                       n_clusters_per_class=1, random_state=0)

# Create the RFE object and compute a cross-validated score.
svc = SVC(kernel="linear")
rfecv = RFECV(estimator=svc, step=1, cv=StratifiedKFold(y, 2),
          scoring='accuracy')
rfecv.fit(X, y)

print("Optimal number of features : %d" % rfecv.n_features_)

# Plot number of features VS. cross-validation scores
import pylab as pl
pl.figure()
pl.xlabel("Number of features selected")
pl.ylabel("Cross validation score (nb of misclassifications)")
pl.plot(range(1, len(rfecv.grid_scores_) + 1), rfecv.grid_scores_)
pl.show()

但是,只需将内核类型从线性更改为rbf,如下所示,就会产生错误:

print(__doc__)

from sklearn.svm import SVC
from sklearn.cross_validation import StratifiedKFold
from sklearn.feature_selection import RFECV
from sklearn.datasets import make_classification
from sklearn.metrics import zero_one_loss

# Build a classification task using 3 informative features
X, y = make_classification(n_samples=1000, n_features=25, n_informative=3,
                       n_redundant=2, n_repeated=0, n_classes=8,
                       n_clusters_per_class=1, random_state=0)

# Create the RFE object and compute a cross-validated score.
svc = SVC(kernel="rbf")
rfecv = RFECV(estimator=svc, step=1, cv=StratifiedKFold(y, 2),
          scoring='accuracy')
rfecv.fit(X, y)

print("Optimal number of features : %d" % rfecv.n_features_)

# Plot number of features VS. cross-validation scores
import pylab as pl
pl.figure()
pl.xlabel("Number of features selected")
pl.ylabel("Cross validation score (nb of misclassifications)")
pl.plot(range(1, len(rfecv.grid_scores_) + 1), rfecv.grid_scores_)
pl.show()

这似乎可能是一个错误,但如果有人能够发现我做错的事情那会很棒。另外,我使用scikit-learn版本0.14.1运行python 2.7.6。

感谢您的帮助!

1 个答案:

答案 0 :(得分:9)

这似乎是预期的结果。 RFECV要求估算工具有coef_,表示要素重要性:

  

estimator:object

     

带有拟合方法的监督学习估算器,该方法更新保存拟合参数的coef_属性。重要特征必须与coef_数组中的高绝对值相对应。

根据文档,通过将内核更改为RBF,SVC不再是线性的,coef_属性也不可用:

  

COEF _

     

array,shape = [n_class-1,n_features]

     

符合特征的权重(原始问题中的系数)。这仅适用于线性内核。

当内核不是线性时,当RFECV尝试访问coef_时,SVC (source)会引发错误。