首页 > 解决方案 > Python中类对象的属性错误 - 朴素贝叶斯

问题描述

我正在尝试从头开始制作一个朴素贝叶斯分类器,用于 Python 中的垃圾邮件过滤。当我尝试训练班级时,我不断收到此错误:

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'

我是编程新手,这是我创建的第一个类。我无法弄清楚为什么会发生这种情况,因为该属性是在构造函数下定义的。抱歉,我的代码有点长。

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)

标签: pythonclassattributeerrornaivebayes

解决方案


如果这是您的代码的精确副本,那么我认为您的 init 方法的名称不正确 - 它应该__init__- 前后两个下划线。您似乎在 ( ) 之前和之后只有一个下划线和两个下划线_init__

因为名字不对,所以在构造实例的时候不会执行。


推荐阅读