我费了很大劲但还是没能弄明白如何在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]
来使其工作。