2017-04-04 1 views
0

트윗에 대한 분류자를 훈련시키고 있습니다. 그러나 분류기의 정확도는 100 %이며 가장 유익한 기능 목록에는 아무 것도 표시되지 않는다는 것이 문제입니다. 아무도 내가 뭘 잘못하고 있는지 알아? 나는 분류 자에 대한 나의 모든 입력이 정확하다고 믿습니다. 그래서 그것이 잘못 될지 전혀 모르겠습니다.NLTK Naive Bayes 분류 자 ​​훈련 문제

feature_set = [(find_features (all_words :

import nltk 
import random 

file = open('Train/train.txt', 'r') 


documents = [] 
all_words = []   #TODO remove punctuation? 
INPUT_TWEETS = 3000 

print("Preprocessing...") 
for line in (file): 

    # Tokenize Tweet content 
    tweet_words = nltk.word_tokenize(line[2:]) 

    sentiment = "" 
    if line[0] == 0: 
     sentiment = "negative" 
    else: 
     sentiment = "positive" 
    documents.append((tweet_words, sentiment)) 

    for word in tweet_words: 
     all_words.append(word.lower()) 

    INPUT_TWEETS = INPUT_TWEETS - 1 
    if INPUT_TWEETS == 0: 
     break 

random.shuffle(documents) 


all_words = nltk.FreqDist(all_words) 

word_features = list(all_words.keys())[:3000] #top 3000 words 

def find_features(document): 
    words = set(document) 
    features = {} 
    for w in word_features: 
     features[w] = (w in words) 

    return features 

#Categorize as positive or Negative 
feature_set = [(find_features(all_words), sentiment) for (all_words, sentment) in documents] 


training_set = feature_set[:1000] 
testing_set = feature_set[1000:] 

print("Training...") 
classifier = nltk.NaiveBayesClassifier.train(training_set) 

print("Naive Bayes Accuracy:", (nltk.classify.accuracy(classifier,testing_set))*100) 
classifier.show_most_informative_features(15) 
+1

문제 같은데 [0]'은'int''0'으로 라인 '에있는 문자를 비교입니다. 귀하의 의견이 실제로 부정적인 정서를 나타 내기 위해 null 바이트를 사용하는지 의심 스럽습니다. – alexis

답변

1

코드에 오타가있다 : 이것은 내 코드입니다 http://thinknook.com/wp-content/uploads/2012/09/Sentiment-Analysis-Dataset.zip

:

내가 사용하고 데이터 세트입니다), 감정)에 대한 (all_words, 문장)

이 ca sentiment을 사용하여 전처리 단계의 마지막 짹짹 값을 항상 동일하게 유지하므로 교육이 무의미하며 모든 기능이 무의미합니다.

수정을 당신이 얻을 것이다 :

('Naive Bayes Accuracy:', 66.75) 
Most Informative Features 
        -- = True   positi : negati =  6.9 : 1.0 
       these = True   positi : negati =  5.6 : 1.0 
       face = True   positi : negati =  5.6 : 1.0 
       saw = True   positi : negati =  5.6 : 1.0 
        ] = True   positi : negati =  4.4 : 1.0 
       later = True   positi : negati =  4.4 : 1.0 
       love = True   positi : negati =  4.1 : 1.0 
        ta = True   positi : negati =  4.0 : 1.0 
       quite = True   positi : negati =  4.0 : 1.0 
       trying = True   positi : negati =  4.0 : 1.0 
       small = True   positi : negati =  4.0 : 1.0 
       thx = True   positi : negati =  4.0 : 1.0 
       music = True   positi : negati =  4.0 : 1.0 
        p = True   positi : negati =  4.0 : 1.0 
      husband = True   positi : negati =  4.0 : 1.0 
+0

오타가 변경되었지만 출력이 100 %로 변경되지 않고 기능이 표시되지 않습니다 –

+0

train.txt가 손상되었거나 불완전 할 수 있습니까? 나는 원래의 데이터를'df = pd.read_csv ('Sentiment Analysis Dataset.csv', error_bad_lines = False, encoding = 'utf-8')'을 사용하여 DataFrame으로 읽었고'df.iterrows()'를 사용하여 행을 반복했다. 위에 붙여 넣기 출력. – acidtobi

+0

.csv를 읽을 수있는 전체 코드를 보여줄 수 있습니까? –

관련 문제