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內核?或者以另一種方式可視化最佳功能?