本文介绍了如何在python的sklearn中的不同管道中获取功能名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码(源)来连接多种特征提取方法.
I am using the following code (source) to concatenate multiple feature extraction methods.
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_)
我想从上面的代码中获取所选功能的名称.
I want to get the names of the selected features from the above code.
为此,我使用了 grid_search.best_estimator_.support _
.但是,这返回了一条错误消息:
For that, I used, grid_search.best_estimator_.support_
. However, this returned an error saying:
AttributeError: 'Pipeline' object has no attribute 'support_'
是否有一种方法来获取选定的功能名称,如上面python sklearn中的代码所示?
Is there a way to get the selected feature names as shown in the above code in sklearn in python?
如果需要,我很乐意提供更多详细信息.
I am happy to provide more details if needed.
推荐答案
这是我了解 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])
这篇关于如何在python的sklearn中的不同管道中获取功能名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!