【问题标题】:How to get feature names from output of gridSearchCV如何从 gridSearchCV 的输出中获取特征名称
【发布时间】:2016-08-18 05:06:10
【问题描述】:

我使用 sklearn 使用朴素贝叶斯实现了 PCA,并使用 GridSearchCV 优化了 PCA 的组件数量。

我试图找出最佳估计器的特征名称,但我无法做到。这是我尝试过的代码。

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

【问题讨论】:

    标签: python machine-learning scikit-learn


    【解决方案1】:

    您似乎混淆了降维特征选择。 PCA 是降维技术,它不选择特征,它寻找更低维度的线性投影。你得到的特征不是你原来的特征——它们是这些特征的线性组合。因此,如果您的原始特征在 PCA 变暗 2 之后是“宽度”、“高度”和“年龄”,那么您最终会得到“0.4 * 宽度 + 0.1 * 高度 - 0.05 * 年龄”和“0.3 * 高度 - 0.2 * 宽度”之类的特征”。

    【讨论】:

    • 我想这就是它没有按我预期的方式工作的原因。
    猜你喜欢
    • 2019-06-30
    • 2018-06-30
    • 2017-11-21
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 2022-06-20
    • 2020-12-10
    • 2018-06-05
    相关资源
    最近更新 更多