十大特色SVC与rbf内核

2024-04-19 00:31:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图得到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个特征吗?或者用另一种方法来形象化最好的功能?在


Tags: 代码names分类器with特征featurefitfn
1条回答
网友
1楼 · 发布于 2024-04-19 00:31:45

我不确定您所要求的对于RBF核函数是否有可能以与您展示的示例类似的方式(正如您的错误所示,它只适用于线性核)。在

但是,您可以尝试feature ablation;逐个删除每个功能,并测试这对性能的影响。对性能影响最大的10个特性是“前10个特性”。在

显然,这只有在(1)特性相对较少和/或(2)模型的培训和测试不需要很长时间的情况下才可能实现。在

相关问题 更多 >