【发布时间】:2012-05-18 14:06:01
【问题描述】:
我正在尝试使用 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']
【问题讨论】:
标签: python classification scikit-learn