我运行的代码,
http://scikit-learn.org/stable/auto_examples/feature_stacker.html#example -feature-stacker-py
并使用以下代码,
# 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)
将数据放入FeatureUnion,我想知道选择了哪个功能。 在 FeatureUnion 的文档中,有一个函数get_feature_names()
可以从所有转换器中获取所有名称。 所以只需调用这个函数并得到这样的错误,
AttributeError: Transformer pca does not provide get_feature_names.
实际上,我知道 pca 没有这样的功能。 但是为什么FeatureUnion提供这个功能!?
我同意应该有一种方法来查看哪些功能属于哪个
组件,我早就提出了这个,但我不认为它是
目前可能。
2016 年 1 月 6 日 18:23,genliu777 [email protected]写道:
我运行的代码,
http://scikit-learnorg/stable/auto_examples/feature_stackerhtml#example -feature-stacker-py
并使用以下代码,从 PCA 和单变量选择构建估计器:
combine_features = FeatureUnion([("pca", pca), ("univ_select", selection)])
使用组合特征转换数据集:
X_features = combine_featuresfit(X, y)transform(X)
将数据放入FeatureUnion,我想知道选择了哪个功能
在 FeatureUnion 的文档中,有一个函数 get_feature_names()
从所有转换器中获取所有名称,因此只需调用此函数
并得到这样的错误,
AttributeError:Transformer pca 不提供 get_feature_names
实际上,我知道 pca 没有这样的功能,但为什么 FeatureUnion
提供这个功能!?—
直接回复此邮件或在 GitHub 上查看
https://github.com/scikit-learn/scikit-learn/issues/6122。
为什么目前不可能!? 你知道,FeatureUnion 提供了函数get_feature_names()
,它也应该可以工作!
比如,可能所有的模型,sklearn 中的模型,都有fit
和transform
的功能,它应该使所有可以放入 FeatureUnion 并运行良好的模型,提供属性作为源get_feature_names()
的代码调用if not hasattr(trans, 'get_feature_names'):
。 否则,FeatureUnion 不一定提供get_feature_names()
的功能!
这可能无法直接解决您与 PCA 相关的特定问题,但如果我正确阅读了您的问题,您通常会想知道如何通过自定义管道渗透属性。
派对迟到了,但无论多么复杂,您都可以访问管道中的元素,方法是遍历管道结构,找到适当的步骤(即使在 featureunion 中),然后使用适当的属性。 这是我刚刚运行的示例:
pipeline = Pipeline([
('union', FeatureUnion([
('categoric', Pipeline([
('f_cat', feature_type_split(type = 'categoric')), #returns categoric in array for vect
('vect', vect),
])),
('numeric', Pipeline([
('f_num', feature_type_split(type = 'numeric')),
])),
])),
('select', ff),
('tree_clf', clf),
])
通过 print(pipeline) 显示管道对象本身给了我一个参考点:
Pipeline(steps=[('union', FeatureUnion(n_jobs=1,
transformer_list=[('categoric', Pipeline(steps=[('f_cat', feature_type_split(type='categoric')), ('vect', DictVectorizer(dtype=<type 'numpy.float64'>, separator='=', sort=True,
sparse=True))])), ('numeric', Pipeline(steps=[('f_num', feature_type...it=2, min_weight_fraction_leaf=0.0,
presort=False, random_state=None, splitter='best'))])
所以我通过以下方式走到联合步骤:
pipeline.named_steps['union']
然后通过以下方式进入下一个级别,即transformer_list(或分类管道):
pipeline.named_steps['union'].transformer_list[0]
然后通过以下方式进入下一个级别,这是类别管道中的步骤:
pipeline.named_steps['union'].transformer_list[0][1]
上面输出了一个典型的管道结构,我们现在可以在其中使用 named_steps:
print pipeline.named_steps['union'].transformer_list[0][1].named_steps['vect']
因此通过以下方式访问我们需要的属性:
print pipeline.named_steps['union'].transformer_list[0][1].named_steps['vect'].get_feature_names()
TLDR;
使用您的自定义管道逐段浏览管道结构,然后像往常一样访问该转换/估计器部分的属性。
请尝试eli5
的transform_feature_names
,它可以在 scikit-learn 的get_feature_names
不起作用的情况下工作。
最有用的评论
这可能无法直接解决您与 PCA 相关的特定问题,但如果我正确阅读了您的问题,您通常会想知道如何通过自定义管道渗透属性。
派对迟到了,但无论多么复杂,您都可以访问管道中的元素,方法是遍历管道结构,找到适当的步骤(即使在 featureunion 中),然后使用适当的属性。 这是我刚刚运行的示例:
pipeline = Pipeline([ ('union', FeatureUnion([ ('categoric', Pipeline([ ('f_cat', feature_type_split(type = 'categoric')), #returns categoric in array for vect ('vect', vect), ])), ('numeric', Pipeline([ ('f_num', feature_type_split(type = 'numeric')), ])), ])), ('select', ff), ('tree_clf', clf), ])
通过 print(pipeline) 显示管道对象本身给了我一个参考点:
Pipeline(steps=[('union', FeatureUnion(n_jobs=1, transformer_list=[('categoric', Pipeline(steps=[('f_cat', feature_type_split(type='categoric')), ('vect', DictVectorizer(dtype=<type 'numpy.float64'>, separator='=', sort=True, sparse=True))])), ('numeric', Pipeline(steps=[('f_num', feature_type...it=2, min_weight_fraction_leaf=0.0, presort=False, random_state=None, splitter='best'))])
所以我通过以下方式走到联合步骤:
pipeline.named_steps['union']
然后通过以下方式进入下一个级别,即transformer_list(或分类管道):
pipeline.named_steps['union'].transformer_list[0]
然后通过以下方式进入下一个级别,这是类别管道中的步骤:
pipeline.named_steps['union'].transformer_list[0][1]
上面输出了一个典型的管道结构,我们现在可以在其中使用 named_steps:
print pipeline.named_steps['union'].transformer_list[0][1].named_steps['vect']
因此通过以下方式访问我们需要的属性:
print pipeline.named_steps['union'].transformer_list[0][1].named_steps['vect'].get_feature_names()
TLDR;
使用您的自定义管道逐段浏览管道结构,然后像往常一样访问该转换/估计器部分的属性。