如何在Python的sklearn中获取不同管道中的特征名称

我使用以下代码(来源)来连接多个特征提取方法。

from sklearn.pipeline import Pipeline, FeatureUnionfrom sklearn.model_selection import GridSearchCVfrom sklearn.svm import SVCfrom sklearn.datasets import load_irisfrom sklearn.decomposition import PCAfrom sklearn.feature_selection import SelectKBestiris = load_iris()X, y = iris.data, iris.targetpca = PCA(n_components=2)selection = SelectKBest(k=1)# Build estimator from PCA and Univariate selection:combined_features = FeatureUnion([("pca", pca), ("univ_select", selection)])# Use combined features to transform dataset:X_features = combined_features.fit(X, y).transform(X)print("Combined space has", X_features.shape[1], "features")svm = SVC(kernel="linear")# Do grid search over k, n_components and C:pipeline = Pipeline([("features", combined_features), ("svm", svm)])param_grid = dict(features__pca__n_components=[1, 2, 3],                  features__univ_select__k=[1, 2],                  svm__C=[0.1, 1, 10])grid_search = GridSearchCV(pipeline, param_grid=param_grid, cv=5, verbose=10)grid_search.fit(X, y)print(grid_search.best_estimator_)

我想从上述代码中获取所选特征的名称。

为此,我使用了grid_search.best_estimator_.support_。然而,这返回了一个错误,显示:

AttributeError: 'Pipeline' object has no attribute 'support_'

在Python的sklearn中,有没有办法获取上述代码中显示的选定特征名称

如果需要,我很乐意提供更多详情。


回答:

这是我了解best_estimator_使用的最终特征的方法

>>> features = grid_search.best_estimator_.named_steps['features']# number of components chosen from pca>>> pca=features.transformer_list[0][1]>>> pca.n_components3# features chosen by selectKbest>>> select_k_best=features.transformer_list[1][1]>>> select_k_best.get_support()array([False, False,  True, False])

Related Posts

在使用k近邻算法时,有没有办法获取被使用的“邻居”?

我想找到一种方法来确定在我的knn算法中实际使用了哪些…

Theano在Google Colab上无法启用GPU支持

我在尝试使用Theano库训练一个模型。由于我的电脑内…

准确性评分似乎有误

这里是代码: from sklearn.metrics…

Keras Functional API: “错误检查输入时:期望input_1具有4个维度,但得到形状为(X, Y)的数组”

我在尝试使用Keras的fit_generator来训…

如何使用sklearn.datasets.make_classification在指定范围内生成合成数据?

我想为分类问题创建合成数据。我使用了sklearn.d…

如何处理预测时不在训练集中的标签

已关闭。 此问题与编程或软件开发无关。目前不接受回答。…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注