2016-12-05 49 views
0

我目前使用樸素貝葉斯來分類一堆文本。我有多個類別。現在我只輸出後驗概率和類別,但我想要做的是根據後驗概率對類別進行排序,並使用第二,第三類別作爲「備份」類別。使用具有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_manyprob_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 

任何幫助,將不勝感激。

回答

1

我把你的自我回答當作你的問題的一部分。想必你得到了分類bird像這樣的可能性:

prob_cat.prob("bird") 

這裏,prob_cat是NLTK概率分佈(ProbDist)。你可以得到所有類別在這樣的離散ProbDist及其概率:

probs = list((x, prob_cat.prob(x)) for x in prob_cat.samples()) 

既然你已經知道你訓練類別,你可以使用預定義的列表,而不是prob_cat.samples()。最後,您可以從相同的表達式中將它們從最多可能排序到最不可能:

mycategories = ["bird", "plane"] 
probs = sorted(((x, prob_cat.prob(x)) for x in mycategories), key=lambda tup: -tup[1]) 
+0

完美,謝謝! –

0

我現在開始到那裏去了。

#This gives me the probability it's a bird. 
prob_cat.prob(bird) 

#This gives me the probability it's a plane. 
prob_cat.prob(plane) 

現在,因爲我有幾十個類別我工作的一個很好的辦法把它給我所有的人都沒有把所有的類別名稱,但應該是非常簡單的。