Skip to main content
 首页 » 编程设计

python之NLTK NaiveBayesClassifier 输入格式

2025年05月04日14findumars

我完全被这个问题难住了。我对 python 和 NLTK 比较陌生。我正在尝试制作一个朴素贝叶斯分类器,但我不确定输入应该是一个元组列表,还是一个字典或一个包含两个列表的元组的列表。

以下返回错误 AttributeError: 'str' object has no attribute 'items'

[('maggie: just a push button. and the electric car uses sensors to drive itself. \n', 'notending')] 

下面的格式返回以下错误 AttributeError: 'list' object has no attribute 'items'

[([['the', 'fire', 'chief', 'says', 'someone', 'started', 'the', 'blaze', 'on', 'purpose', 'as', 'a', 'controlled', 'burn', ',', 'but', 'it', 'quickly', 'got', 'out', 'of', 'hand', '.']], 'notending')] 

如果我使用字典,我会收到以下错误 ValueError: too many values to unpack

{'everyone: bye!': 'ending'} 

我将朴素贝叶斯分类器称为 classifier = nltk.NaiveBayesClassifier.train(d_train)

我不确定这里有什么问题。非常感谢您的帮助。谢谢。

请您参考如下方法:

from nltk.classify import NaiveBayesClassifier 
from nltk.corpus import stopwords 
stopset = list(set(stopwords.words('english'))) 
 
def word_feats(words): 
    return dict([(word, True) for word in words.split() if word not in stopset]) 
 
posids = ['I love this sandwich.', 'I feel very good about these beers.'] 
negids = ['I hate this sandwich.', 'I feel worst about these beers.'] 
pos_feats = [(word_feats(f), 'positive') for f in posids ] 
neg_feats = [(word_feats(f), 'negative') for f in negids ] 
print pos_feats 
print neg_feats 
trainfeats = pos_feats + neg_feats 
classifier = NaiveBayesClassifier.train(trainfeats) 

看看正面和负面的壮举

[({'I': True, 'love': True, 'sandwich.': True}, 'positive'), ({'I': True, 'feel': True, 'good': True, 'beers.': True}, 'positive')] 
[({'I': True, 'hate': True, 'sandwich.': True}, 'negative'), ({'I': True, 'feel': True, 'beers.': True, 'worst': True}, 'negative')] 

所以,如果你给'I hate everything'这句话来分类

print classifier.classify(word_feats('I hate everything')) 

您将得到“否定”的结果。