python – 在scikit-learn中使用rbf内核为SVM使用递归特征消除的ValueError
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 在scikit-learn中使用rbf内核为SVM使用递归特征消除的ValueError,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2853字,纯文字阅读大概需要5分钟。
内容图文
![python – 在scikit-learn中使用rbf内核为SVM使用递归特征消除的ValueError](/upload/InfoBanner/zyjiaocheng/749/b3e6868bc09941139904a02a500266e9.jpg)
我试图在scikit-learn中使用递归特征消除(RFE)函数,但不断得到错误ValueError:coef_仅在使用线性内核时可用.我正在尝试使用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.
谢谢您的帮助!
解决方法:
这似乎是预期的结果. RFECV要求估算器具有表示要素重要性的coef_:
estimator : object
A supervised learning estimator with a fit method that updates a coef_ attribute that holds the fitted parameters. Important features must correspond to high absolute values in the coef_ array.
根据文档,通过将内核更改为RBF,SVC不再是线性的,并且coef_属性变得不可用.
coef_
array, shape = [n_class-1, n_features]
Weights asigned to the features (coefficients in the primal problem). This is only available in the case of linear kernel.
当内核不是线性时,当RFECV试图访问coef_时,SVC (source)会引发错误.
内容总结
以上是互联网集市为您收集整理的python – 在scikit-learn中使用rbf内核为SVM使用递归特征消除的ValueError全部内容,希望文章能够帮你解决python – 在scikit-learn中使用rbf内核为SVM使用递归特征消除的ValueError所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。