我想用sklearn分类一些句子。句子存储在Pandas DataFrame中。FeatureUnion具有不同的特征尺寸
首先,我想用一句话的长度,它的TF-IDF向量作为特征,所以我创造了这个管道:
pipeline = Pipeline([
('features', FeatureUnion([
('meta', Pipeline([
('length', LengthAnalyzer())
])),
('bag-of-words', Pipeline([
('tfidf', TfidfVectorizer())
]))
])),
('model', LogisticRegression())
其中LengthAnalyzer是一个自定义TransformerMixin
有:
def transform(self, documents):
for document in documents:
yield len(document)
所以,LengthAnalyzer返回一个数字(1维),而TfidfVectorizer返回一个n维列表。
当我尝试运行此,我得到
ValueError: blocks[0,:] has incompatible row dimensions. Got blocks[0,1].shape[0] == 494, expected 1.
了什么工作要做,以使此功能结合工作?
将该数字转换为形状的二维数组[1,1] –
像np.array(len(document))。reshape(-1,1)?同样的错误 – Mirco