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

3

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

from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
from sklearn.feature_selection import SelectKBest

iris = load_iris()

X, y = iris.data, iris.target

pca = 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中获取如上代码所示的选定特征名称

如需更多详细信息,请告知。


这可能是以下SO的重复问题 https://stackoverflow.com/questions/36829875/how-to-get-feature-names-from-output-of-gridsearchcv - Ali Azam
1个回答

1

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

>>> features = grid_search.best_estimator_.named_steps['features']

# number of components chosen from pca
>>> pca=features.transformer_list[0][1]

>>> pca.n_components
3

# features chosen by selectKbest
>>> select_k_best=features.transformer_list[1][1]

>>> select_k_best.get_support()
array([False, False,  True, False])

1
非常感谢。如果您知道这个问题的答案,请告诉我:https://stackoverflow.com/questions/55671530/why-do-i-get-different-values-with-pipline-and-without-pipline-in-sklearn-in-pyt 非常感谢 :) - EmJ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接