python - 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)
解决方案
如果这是您的代码的精确副本,那么我认为您的 init 方法的名称不正确 - 它应该__init__
- 前后两个下划线。您似乎在 ( ) 之前和之后只有一个下划线和两个下划线_init__
。
因为名字不对,所以在构造实例的时候不会执行。
推荐阅读
- python - 单击电报中的菜单按钮后显示文本消息
- reactjs - 使用 gatsbyjs 将来自 graphql 查询的数据修改为反应组件在 gatsby 开发中有效,但在 gatsby 构建中中断
- python-3.x - 如何解决此未知标签类型错误?
- android - 如何从列、行获取索引
- apache-kafka - Kafka的Replica处理机制?
- css - flex中的角度项目在使它们成为组件后没有固定的高度
- android-studio - 在不重启 Android Studio 的情况下刷新自定义 template.xml
- python - 如何使用 OpenCV 用一些 Python 代码拼接这两个图像?
- python - 字符串与数据库中数据的比较
- android - 如何在按钮单击以打开另一个活动时输入密码?