Python中类对象的属性错误-朴素贝叶斯
我正在尝试从头制作一个朴素的贝叶斯分类器,用于Python中的垃圾邮件过滤。当我试着训练全班同学时,我总是遇到这样的错误:Python中类对象的属性错误-朴素贝叶斯,python,class,attributeerror,naivebayes,Python,Class,Attributeerror,Naivebayes,我正在尝试从头制作一个朴素的贝叶斯分类器,用于Python中的垃圾邮件过滤。当我试着训练全班同学时,我总是遇到这样的错误: Traceback (most recent call last): File "/Users/francescacape/Desktop/testnb.py", line 97, in <module> nb.train(traindata) File "/Users/francescacape/Desktop/t
Traceback (most recent call last):
File "/Users/francescacape/Desktop/testnb.py", line 97, in <module>
nb.train(traindata)
File "/Users/francescacape/Desktop/testnb.py", line 36, in train
self._hamdocs += 1
AttributeError: 'NLPNaiveBayes' object has no attribute '_hamdocs'
如果这是您的代码的精确副本,那么我认为您的init方法的名称不正确-它应该
\uuuu init\uuuu
-前后两个下划线。您似乎只有一个下划线在前面,两个下划线在后面(\u init\u
)
由于名称错误,这意味着在构造实例时不会执行该名称
from collections import defaultdict
import math
from nltk.tokenize import word_tokenize
import glob
class NLPNaiveBayes:
def _init__(self):
self._spamdocs = 0
self._hamdocs = 0
self.totaldocs = self._spamdocs + self._hamdocs
self.spamwordcount = defaultdict(int)
self.hamwordcount = defaultdict(int)
self.spamwords = {}
self.hamwords = {}
self.totalwords = set()
self.totalspamwords = []
self.totalhamwords = []
self.priorlogham = math.log(self._hamdocs / self.totaldocs)
self.priorlogspam = math.log(self._spamdocs / self.totaldocs)
@staticmethod
def preprocessing(message):
toks = list(word_tokenize(message))
words = [word.islower() for word in toks if word.isalnum()]
return set(words)
def train(self, data):
for message, cat in data:
if is_spam:
self._spamdocs += 1
self.totalspamwords.append(message)
else:
self._hamdocs += 1
self.totalhamwords.append(message)
words = self.preprocessing(message)
for word in words:
if is_spam:
self.totalwords.update(word)
self.spamwords.update(word)
self.spamwordcount[word] += 1
else:
self.totalwords.update(word)
self.hamwords.update(word)
self.hamwordcount[word] += 1
for word, count in self.totalspamwords.item():
self.spamwords[word] = math.log((int(count) + 1)) / (self._spamdocs + self.totaldocs)
for word, count in self.totalhamwords.item():
self.hamwords[word] = math.log((int(count) + 1)) / (self._hamdocs + self.totaldocs)
nb = NLPNaiveBayes()
nb.train(traindata)