我正在做一些多类文本分类,它可以很好地满足我的需求:
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 – >;“没有什么能改变我的关注那个新闻的类别“).我应该如何获得该类别的确定性值/预测值得分?
解决方法:
如果您需要类别概率之类的东西,则必须使用分类器的predict_proba()方法.
标签:python,machine-learning,classification,scikit-learn
来源: https://codeday.me/bug/20190628/1312466.html