2016-12-05 11 views
0

현재 Naive Bayes를 사용하여 많은 텍스트를 분류하고 있습니다. 여러 카테고리가 있습니다. 지금 방금 후 확률과 범주를 출력하지만, 내가하고자하는 것은 사후 확률에 따라 범주의 순위를 매기고 두 번째, 세 번째 범주를 "백업"범주로 사용하는 것입니다.NLTK를 사용하여 Naive Bayes를 사용하여 텍스트 문자열을 여러 클래스로 분류

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)) 

에 생길 출력 : 내가 classify_many를 사용하여 몇 가지 예제를 발견

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 

prob_classify_many하지만 파이썬에 익숙하지 않아 문제를 해결하는 데 어려움이 있습니다. 팬더와 함께 사용되는 것을 보지 못했습니다.

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

귀하의 질문에 귀하의 자답을 다루고 있습니다.

다음
prob_cat.prob("bird") 

prob_cat는 NLTK 확률 분포 (ProbDist)이다 : 아마도 당신은 다음과 같이 분류 bird의 확률을 얻었다. 이 같은 이산 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) 

지금 나는 범주 이름을 모두에주지 않고 나에게 그들 모두를 제공해야하는 내가 좋은 방법 일하고 있어요 종류의 수십을 가지고 있기 때문에,하지만 그건 아주 간단해야한다.