python-3.x - 从FeatureUnion +管道中获取特征名称

标签 python-3.x scikit-learn nlp feature-extraction

我正在使用FeatureUnion来加入从事件的标题和描述中找到的特征:

union = FeatureUnion(
    transformer_list=[
    # Pipeline for pulling features from the event's title
        ('title', Pipeline([
            ('selector', TextSelector(key='title')),
            ('count', CountVectorizer(stop_words='english')),
        ])),

        # Pipeline for standard bag-of-words model for description
        ('description', Pipeline([
            ('selector', TextSelector(key='description_snippet')),
            ('count', TfidfVectorizer(stop_words='english')),
        ])),
    ],

    transformer_weights ={
            'title': 1.0,
            'description': 0.2
    },
)


但是,调用union.get_feature_names()会给我一个错误:“变压器标题(管道类型)不提供get_feature_names。”我想看看由我的不同Vectorizer生成的一些功能。我该怎么做呢?

最佳答案

这是因为您正在使用名为TextSelector的自定义transfomer。您是否在get_feature_names中实现了TextSelector

如果您希望此方法有效,则必须在自定义转换中实现此方法。

这是为您提供的具体示例:

from sklearn.datasets import load_boston
from sklearn.pipeline import FeatureUnion, Pipeline
from sklearn.base import TransformerMixin
import pandas as pd

dat = load_boston()
X = pd.DataFrame(dat['data'], columns=dat['feature_names'])
y = dat['target']

# define first custom transformer
class first_transform(TransformerMixin):
    def transform(self, df):
        return df

    def get_feature_names(self):
        return df.columns.tolist()


class second_transform(TransformerMixin):
    def transform(self, df):
        return df

    def get_feature_names(self):
        return df.columns.tolist()



pipe = Pipeline([
       ('features', FeatureUnion([
                    ('custom_transform_first', first_transform()),
                    ('custom_transform_second', second_transform())
                ])
        )])

>>> pipe.named_steps['features']_.get_feature_names()
['custom_transform_first__CRIM',
 'custom_transform_first__ZN',
 'custom_transform_first__INDUS',
 'custom_transform_first__CHAS',
 'custom_transform_first__NOX',
 'custom_transform_first__RM',
 'custom_transform_first__AGE',
 'custom_transform_first__DIS',
 'custom_transform_first__RAD',
 'custom_transform_first__TAX',
 'custom_transform_first__PTRATIO',
 'custom_transform_first__B',
 'custom_transform_first__LSTAT',
 'custom_transform_second__CRIM',
 'custom_transform_second__ZN',
 'custom_transform_second__INDUS',
 'custom_transform_second__CHAS',
 'custom_transform_second__NOX',
 'custom_transform_second__RM',
 'custom_transform_second__AGE',
 'custom_transform_second__DIS',
 'custom_transform_second__RAD',
 'custom_transform_second__TAX',
 'custom_transform_second__PTRATIO',
 'custom_transform_second__B',
 'custom_transform_second__LSTAT']


请记住,Feature Union将连接每个变压器从相应get_feature_names发出的两个列表。这就是为什么当一个或多个变压器不具有此方法时出现错误的原因。

但是,我看到仅凭此一项并不能解决您的问题,因为Pipeline对象中没有get_feature_names方法,并且您具有嵌套的管道(Feature Union中的管道)。因此,您有两种选择:


子类化Pipeline并自己添加get_feature_names方法,该方法从链中的最后一个转换器获取特征名称。
从每个转换器中自己提取功能名称,这将需要您自己从管道中取出这些转换器并在其上调用get_feature_names


另外,请记住,许多内置的sklearn转换器不会在DataFrame上运行,而是传递numpy数组,因此,如果要将很多转换器链接在一起,请小心。但是我认为这为您提供了足够的信息,使您可以了解正在发生的事情。

再看一遍sklearn-pandas。我自己没有使用过,但它可能为您提供解决方案。

关于python-3.x - 从FeatureUnion +管道中获取特征名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42479370/

相关文章:

python-3.x - 使用Keras后端实现裁剪功能

python - 找不到模块错误 : No module named 'src'

python-3.x - 如何将 bool 数据类型为 True 的 numpy 零数组索引?

python - 在 virtualenv : installed sklearn module not available 中运行 Jupyter notebook

python - 比较 2 个 ML 模型的性能准确性之间的差异是否具有统计显着性

java - ClassNotFoundException : edu. stanford.nlp.tagger.maxent.ExtractorNonAlphanumeric

python - Tensorflow Keras实现多实例学习问题

python-3.x - python numba指纹错误

python - 使用 Scikit-learn 标记预处理文本

python - 标记 HTML 文档