3

다음 코드에서, naivebayes 분류기가 trainset1에서 올바르게 작동하지만 trainset2에서 작동하지 않는 이유는 무엇인지 정확하게 알고 있습니다. TextBlob과 nltk의 두 분류 자에 대해서도 시도해 보았습니다.nltk naivebayes 텍스트 분류를위한 분류 자 ​​

from textblob.classifiers import NaiveBayesClassifier 
from textblob import TextBlob 
from nltk.tokenize import word_tokenize 
import nltk 

trainset1 = [('I love this sandwich.', 'pos'), 
('This is an amazing place!', 'pos'), 
('I feel very good about these beers.', 'pos'), 
('This is my best work.', 'pos'), 
("What an awesome view", 'pos'), 
('I do not like this restaurant', 'neg'), 
('I am tired of this stuff.', 'neg'), 
("I can't deal with this", 'neg'), 
('He is my sworn enemy!', 'neg'), 
('My boss is horrible.', 'neg')] 

trainset2 = [('hide all brazil and everything plan limps to anniversary inflation plan initiallyis limping its first anniversary amid soaring prices', 'class1'), 
     ('hello i was there and no one came', 'class2'), 
     ('all negative terms like sad angry etc', 'class2')] 

def nltk_naivebayes(trainset, test_sentence): 
    all_words = set(word.lower() for passage in trainset for word in word_tokenize(passage[0])) 
    t = [({word: (word in word_tokenize(x[0])) for word in all_words}, x[1]) for x in trainset] 
    classifier = nltk.NaiveBayesClassifier.train(t) 
    test_sent_features = {word.lower(): (word in word_tokenize(test_sentence.lower())) for word in all_words} 
    return classifier.classify(test_sent_features) 

def textblob_naivebayes(trainset, test_sentence): 
    cl = NaiveBayesClassifier(trainset) 
    blob = TextBlob(test_sentence,classifier=cl) 
    return blob.classify() 

test_sentence1 = "he is my horrible enemy" 
test_sentence2 = "inflation soaring limps to anniversary" 

print nltk_naivebayes(trainset1, test_sentence1) 
print nltk_naivebayes(trainset2, test_sentence2) 
print textblob_naivebayes(trainset1, test_sentence1) 
print textblob_naivebayes(trainset2, test_sentence2) 

출력 :

neg 
class2 
neg 
class2 

test_sentence2 명확하게 CLASS1에 속해 있지만.

답변

3

분류 자에게 3 가지 예제만으로 좋은 모델을 배우고이 특정 예에서 왜 그런지 이해하는 것이 더 바람직하다고 생각합니다.

그럴 가능성이있는 이유는 순진 베이 분류가 이전 클래스 확률을 사용한다는 것입니다. 즉, 텍스트와 상관없이 대결 가능성이 있습니다. 귀하의 경우, 2/3의 예는 부정적이며 따라서 사전은 neg의 경우 66 %이고 pos의 경우 33 %입니다. 당신의 긍정적 인 예에서 긍정적 인 말은 'anniversary'와 'soaring'입니다. 이것은 이전의 계급 확률을 보상하기에 충분하지 않을 것입니다.

특히, 단어 빈도 계산에는 log10 (용어 빈도)이 아닌 각 클래스의 log10 (용어 빈도 +1)과 같이 다양한 '평활화'기능이 필요합니다. 분류 결과, 0으로 나누는 등 너무 많은 영향을줍니다. 따라서 "기념일"과 "급상승"확률은 예상했던 것과 달리 neg가 0.0이고 pos가 1.0입니다.