我目前使用朴素贝叶斯来分类一堆文本。我有多个类别。现在我只输出后验概率和类别,但我想要做的是根据后验概率对类别进行排序,并使用第二,第三类别作为“备份”类别。使用具有NLTK的朴素贝叶斯将文本字符串分类为多个类
下面是一个例子:
df = pandas.DataFrame({ 'text' : pandas.Categorical(["I have wings","Metal wings","Feathers","Airport"]), 'true_cat' : pandas.Categorical(["bird","plane","bird","plane"])})
text true_cat
-----------------------
I have wings bird
Metal wings plane
Feathers bird
Airport plane
我在做什么:
new_cat = classifier.classify(features(text))
prob_cat = classifier.prob_classify(features(text))
- 最终输出:
new_cat prob_cat text true_cat
bird 0.67 I have wings bird
bird 0.6 Feathers bird
bird 0.51 Metal wings plane
plane 0.8 Airport plane
我已经找到了几个例子使用classify_many和prob_classify_many但由于我是新来的Python我有麻烦翻译它到我的问题。我没有看到它在任何地方都与熊猫一起使用。
我希望它看起来像这样:
df_new = pandas.DataFrame({'text': pandas.Categorical(["I have wings","Metal wings","Feathers","Airport"]),'true_cat': pandas.Categorical(["bird","plane","bird","plane"]), 'new_cat1': pandas.Categorical(["bird","bird","bird","plane"]), 'new_cat2': pandas.Categorical(["plane","plane","plane","bird"]), 'prob_cat1': pandas.Categorical(["0.67","0.51","0.6","0.8"]), 'prob_cat2': pandas.Categorical(["0.33","0.49","0.4","0.2"])})
new_cat1 new_cat2 prob_cat1 prob_cat2 text true_cat
-----------------------------------------------------------------------
bird plane 0.67 0.33 I have wings bird
bird plane 0.51 0.49 Metal wings plane
bird plane 0.6 0.4 Feathers bird
plane bird 0.8 0.2 Airport plane
任何帮助,将不胜感激。
完美,谢谢! –