我想用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