我正在尝试为推文训练分类器。然而,问题在于它说分类器具有 100% 的准确度,而信息量最大的特征列表没有显示任何内容。有谁知道我做错了什么?我相信我对分类器的所有输入都是正确的,所以我不知道哪里出了问题。
这是我正在使用的数据集:http: //thinknook.com/wp-content/uploads/2012/09/Sentiment-Analysis-Dataset.zip
这是我的代码:
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)