scikit-learn确定所选类别

时间:2016-03-04 10:35:02

标签: python machine-learning scikit-learn classification

我正在进行一些多类文本分类,它可以很好地满足我的需求:

classifier = Pipeline([
    ('vect', CountVectorizer(tokenizer=my_tokenizer, stop_words=stopWords, ngram_range=(1, 2), min_df=2)),
    ('tfidf', TfidfTransformer(norm='l2', use_idf=True, smooth_idf=True, sublinear_tf=False)),
    ('clf', MultinomialNB(alpha=0.01, fit_prior=True))])

categories = [list of my possible categories]

# Learning

news = [list of news already categorized]
news_cat = [the category of the corresponding news]

news_target_cat = numpy.searchsorted(categories, news_cat)

classifier = classifier.fit(news, news_target_cat)

# Categorizing

news = [list of news not yet categorized]

predicted = classifier.predict(news)

for i, pred_cat in enumerate(predicted):
    print(news[i])
    print(categories[pred_cat])

现在,我想要预测的类别是预测变量的'确定性'(例如:0.0 - >“我已经掷骰子选择一个类别”最多1.0 - >;“没有什么会改变我对新闻类别的看法“)。我该如何获得该确定性值/该类别预测变量的得分?

1 个答案:

答案 0 :(得分:3)

如果您需要类别probability之类的内容,则必须使用分类器的predict_proba()方法。

Docs