2017-08-06 35 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)訓練和測試你的模型不需要很長時間,這是唯一可能的。