我想用滑雪板分类一些句子。句子存储在Pandas DataFrame中。
首先,我想使用这个句子的长度,它的TF-以色列国防军向量作为一个特性,所以我创建了这个管道:
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返回一个数字(一维),而TfidfVectorizer返回一个n维列表.
当我尝试运行这个时,我得到
ValueError: blocks[0,:] has incompatible row dimensions. Got blocks[0,1].shape[0] == 494, expected 1.要使这种功能组合工作,必须做些什么?
发布于 2017-10-13 14:02:07
这个问题似乎起源于transform()中使用的yield。可能由于yield,向scipy hstack方法报告的行数是1,而不是documents中的实际样本数。
您的数据中应该有494行(示例),这些数据来自TfidfVectorizer,但是LengthAnalyzer只报告了一行。因此出现了错误。
如果你能把它改为
return np.array([len(document) for document in documents]).reshape(-1,1)
然后,该管道成功地适应。
注意:我试图在scikit学习github上找到任何相关的问题,但都没有成功。您可以在那里发布这个问题,以获得一些关于使用的真实反馈。
https://stackoverflow.com/questions/46728016
复制相似问题