前十大功能SVC与rbf内核

时间:2017-08-06 09:12:06

标签: python scikit-learn svm feature-selection

我正在努力为具有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个功能吗?或者另一种可视化最佳功能的方法?

1 个答案:

答案 0 :(得分:1)

我不确定您所询问的RBF内核是否可能与您显示的示例类似(正如您的错误所示,这只适用于线性内核)。

但是,您可以随时尝试feature ablation;逐个删除每个功能并测试它对性能的影响。影响性能的10个功能是您的前10个功能"。

显然,只有在(1)你的功能相对较少和/或(2)训练和测试你的模型不需要很长时间的情况下,这是可能的。

相关问题