2017-08-06 33 views
0

我试图获得带RBF内核的支持向量机分类器前10名最具信息性(最好)的特征。由于我是编程初学者,我尝试了一些我在网上找到的代码。不幸的是,没有工作。我总是得到错误:ValueError: coef_ is only available when using a linear kernel前10名特征rbf内核的SVC

这是我测试的最后代码:

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)) 

是否有人没有办法得到一个分类的前10功能与RBF内核?或者以另一种方式可视化最佳功能?

回答

1

我不确定你所要求的是否有可能以类似的方式显示RBF内核(这是你的错误提示,只能用于线性内核)。

但是,您可以随时尝试feature ablation;逐个删除每个功能并测试它如何影响性能。对性能影响最大的10个功能是您的“前10名功能”。 (1)你的功能相对较少和/或(2)训练和测试你的模型不需要很长时间,这是唯一可能的。