2016-04-24 21 views
0

Sklearnを使用してNaive BayesでPCAを実装し、GridSearchCVを使用してコンポーネントのPCA数を最適化しました。gridSearchCVの出力からフィーチャ名を取得する方法

ベストエスティメータの機能名を見つけようとしましたが、できませんでした。ここで私が試したコードです。

from sklearn.cross_validation import train_test_split 
features_train, features_test, labels_train, labels_test = \ 
train_test_split(features, labels, test_size=0.3, random_state=42) 
### A Naive Bayes classifier combined with PCA is used and its accuracy is tested 

pca = decomposition.PCA() 
#clf = GaussianNB() 
clf = Pipeline(steps=[('pca', pca), ('gaussian_NB', GaussianNB())]) 
n_components = [3, 5, 7, 9] 
clf = GridSearchCV(clf, 
         dict(pca__n_components=n_components)) 

# from sklearn.tree import DecisionTreeClassifier 
#clf = DecisionTreeClassifier(random_state=0, min_samples_split=20) 
clf = clf.fit(features_train, labels_train) 
features_pred = clf.predict(features_test) 
print "The number of components of the best estimator is ", clf.best_estimator_.named_steps['pca'].n_components 
print "The best parameters:", clf.best_params_ 
#print "The best estimator", clf.best_estimator_.get_params(deep=True).gaussian_NB 
# best_est = RFE(clf.best_estimator_) 
# print "The best estimator:", best_est 
estimator = clf.best_estimator_ 
print "The features are:", estimator['features'].get_feature_names() 

答えて

1

あなたは選択ています次元削減を混乱しているようです。 PCAは次元削減技術であり、フィーチャを選択せず​​、より低い次元の線形投影を探す。結果のフィーチャはオリジナルのフィーチャではなく、フィーチャの線形結合です。したがって、PCAが2倍になった後、オリジナルのフィーチャが「幅」、「高さ」、「年齢」の場合、「0.4 *幅+ 0.1 *高さ - 0.05 *年」および「0.3 *高さ - 0.2 *幅"

+0

私はそれが私が期待するように動作していない理由だと思います。 –

0

this answerのように思われるかもしれません。それは本当に良い徹底的な例も含んでいます!

関連する問題