简体   繁体   English

使用FeatureUnion在scikit学习管道中的单词袋中添加额外功能

[英]Adding extra features to bag of words in scikit-learn pipeline with FeatureUnion

I've struggled so much but still couldn't figure out how to use extra features alongside text features with FeatureUnion in the scikit-learn pipeline. 我已经尽了很多努力,但是仍然无法弄清楚如何在scikit-learn管道中的FeatureUnion同时使用文本功能和FeatureUnion功能。
I have a list of sentences and their labels to train a model and a list of sentences as test data. 我有一个句子列表及其标签以训练模型,还有一个句子列表作为测试数据。 Then I try to add an extra feature (like the length of each sentence) to the bag words. 然后,我尝试为袋式单词添加一个额外的功能(如每个句子的长度)。 For this I wrote a custom LengthTransformer which returns a list of lengths and has same number of elements as my train list. 为此,我编写了一个自定义的LengthTransformer ,它返回一个长度列表,并且元素数量与我的火车列表相同。
Then I combine that with the TfidfVectorizer using FeatureUnion but it just doesn't work. 然后,我将其与使用FeatureUnionTfidfVectorizer结合使用,但它不起作用。

What I've came up with so far is this: 到目前为止,我想到的是:

from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import LinearSVC
from sklearn.multiclass import OneVsRestClassifier
from sklearn import preprocessing

class LengthTransformer(BaseEstimator, TransformerMixin):

    def fit(self, X, y=None):
        return self

    def transform(self, X):
        return [len(x) for x in X]


X_train = ["new york is a hell of a town",
            "new york was originally dutch",
            "the big apple is great",
            "new york is also called the big apple",
            "nyc is nice",
            "people abbreviate new york city as nyc",
            "the capital of great britain is london",
            "london is in the uk",
            "london is in england",
            "london is in great britain",
            "it rains a lot in london",
            "london hosts the british museum",
            "new york is great and so is london",
            "i like london better than new york"]
y_train_text = [["new york"], ["new york"], ["new york"], ["new york"], ["new york"],
                ["new york"], ["london"], ["london"], ["london"], ["london"],
                ["london"], ["london"], ["london", "new york"], ["new york", "london"]]

X_test = ['nice day in nyc',
            'welcome to london',
            'london is rainy',
            'it is raining in britian',
            'it is raining in britian and the big apple',
            'it is raining in britian and nyc',
            'hello welcome to new york. enjoy it here and london too']

lb = preprocessing.MultiLabelBinarizer()
Y = lb.fit_transform(y_train_text)

classifier = Pipeline([
    ('feats', FeatureUnion([
       ('tfidf', TfidfVectorizer()),
       ('len', LengthTransformer())
    ])),
    ('clf', OneVsRestClassifier(LinearSVC()))
])

classifier.fit(X_train, Y)
predicted = classifier.predict(X_test)
all_labels = lb.inverse_transform(predicted)

for item, labels in zip(X_test, all_labels):
    print('{} => {}'.format(item, ', '.join(labels)))

LengthTransformer.transform return shape is wrong - it returns a scalar per input document, while transformers should return a feature vector per document. LengthTransformer.transform返回形状是错误的-它为每个输入文档返回一个标量,而转换器应为每个文档返回一个特征向量。 You can make it work by changing [len(x) for x in X] to [[len(x)] for x in X] in LengthTransformer.transform. 您可以通过在LengthTransformer.transform中将[[len(x)] for x in X] [len(x) for x in X]更改[len(x) for x in X] [[len(x)] for x in X]使其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM