在scikit-learn管道中使用FeatureUnion为词袋模型添加额外特征

我费了很大劲但还是没能弄明白如何在scikit-learn管道中使用FeatureUnion来同时使用文本特征和额外特征。
我有一组句子和它们的标签用于训练模型,还有一组句子作为测试数据。然后我尝试为词袋模型添加一个额外特征(例如每个句子的长度)。为此,我编写了一个自定义的LengthTransformer,它返回一个长度列表,并且元素数量与我的训练列表相同。
然后我尝试使用FeatureUnion将它与TfidfVectorizer结合,但就是行不通。

到目前为止,我想到的是这样做的:

from sklearn.base import BaseEstimator, TransformerMixinfrom sklearn.pipeline import Pipeline, FeatureUnionfrom sklearn.feature_extraction.text import TfidfVectorizerfrom sklearn.svm import LinearSVCfrom sklearn.multiclass import OneVsRestClassifierfrom sklearn import preprocessingclass 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返回的形状不对——它为每个输入文档返回一个标量,而变换器应该为每个文档返回一个特征向量。你可以通过将LengthTransformer.transform中的[len(x) for x in X]改为[[len(x)] for x in X]来使其工作。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注