【发布时间】:2024-01-03 21:19:01
【问题描述】:
我正在尝试为具有 RBF 内核的 SVM 分类器获取前 10 个信息量最大(最佳)的特征。由于我是编程初学者,所以我尝试了一些我在网上找到的代码。不幸的是,没有一个工作。我总是收到错误:ValueError: coef_ is only available when using a linear kernel。
这是我测试的最后一个代码:
scaler = StandardScaler(with_mean=False)
enc = LabelEncoder()
y = enc.fit_transform(labels)
vec = DictVectorizer()
feat_sel = SelectKBest(mutual_info_classif, k=200)
# Pipeline for SVM classifier
clf = SVC()
pipe = Pipeline([('vectorizer', vec),
('scaler', StandardScaler(with_mean=False)),
('mutual_info', feat_sel),
('svc', clf)])
y_pred = model_selection.cross_val_predict(pipe, instances, y, cv=10)
# Now fit the pipeline using your data
pipe.fit(instances, y)
def show_most_informative_features(vec, clf, n=10):
feature_names = vec.get_feature_names()
coefs_with_fns = sorted(zip(clf.coef_[0], feature_names))
top = zip(coefs_with_fns[:n], coefs_with_fns[:-(n + 1):-1])
for (coef_1, fn_1), (coef_2, fn_2) in top:
return ('\t%.4f\t%-15s\t\t%.4f\t%-15s' % (coef_1, fn_1, coef_2, fn_2))
print(show_most_informative_features(vec, clf))
有人没有办法从带有 RBF 内核的分类器中获得前 10 个特征吗?或者以其他方式可视化最佳特征?
【问题讨论】:
标签: python scikit-learn svm feature-selection