我想使用scikit-learn中的监督学习方法将文本分类到一个或多个类别中。我尝试使用所有算法的预测函数,但只返回一个匹配项。
例如,我有一段文本:
例如,我有一段文本:
"Theaters in New York compared to those in London"
我已经训练好了算法,可以为我输入的每个文本片段选择一个地点。
在上面的例子中,我希望它返回New York
和London
,但它只返回New York
。
是否可能使用scikit-learn返回多个结果?甚至返回下一个最高概率的标签?
谢谢你的帮助。
---更新
我尝试使用OneVsRestClassifier
,但每个文本片段仍然只返回一个选项。以下是我使用的示例代码:
y_train = ('New York','London')
train_set = ("new york nyc big apple", "london uk great britain")
vocab = {'new york' :0,'nyc':1,'big apple':2,'london' : 3, 'uk': 4, 'great britain' : 5}
count = CountVectorizer(analyzer=WordNGramAnalyzer(min_n=1, max_n=2),vocabulary=vocab)
test_set = ('nice day in nyc','london town','hello welcome to the big apple. enjoy it here and london too')
X_vectorized = count.transform(train_set).todense()
smatrix2 = count.transform(test_set).todense()
base_clf = MultinomialNB(alpha=1)
clf = OneVsRestClassifier(base_clf).fit(X_vectorized, y_train)
Y_pred = clf.predict(smatrix2)
print Y_pred
结果: ['New York' 'London' 'London']
min_n
和max_n
的问题吗?我需要将它们改成ngram_range=(1,2)
才能使其正常工作。 - emmagras