【发布时间】:2013-03-14 10:31:15
【问题描述】:
我正在试用NaiveBayes Python 库(python 2.7)
我想知道为什么运行这段代码会给我一个ZeroDivisionError。
#!/usr/bin/env python
import NaiveBayes
model = NaiveBayes.NaiveBayes()
model.set_real(['Height'])
model.set_real(['Weight'])
model.add_instances({'attributes':
{'Height': 239,
'Weight': 231,
},
'cases': 32,
'label': 'Sex=M'})
model.add_instances({'attributes':
{'Height': 190,
'Weight': 152
},
'cases': 58,
'label': 'Sex=F'
})
model.train()
result = model.predict({'attributes': {'Height': 212, 'Weight': 200}})
print("The result is %s" % (result))
这是输出:
Traceback (most recent call last):
File "/tmp/py4127eDT", line 24, in <module>
result = model.predict({'attributes': {'Height': 212, 'Weight': 200}})
File "/usr/local/lib/python2.7/dist-packages/NaiveBayes.py", line 152, in predict
scores[label] /= sumPx
ZeroDivisionError: float division by zero
我是贝叶斯分类器的新手,所以我的输入有问题(即:数字的分布,还是没有足够的样本?)
【问题讨论】:
-
(一枪进入黑暗)如果你强制高度和重量是浮动的,问题会持续吗?例如
239.而不是239等? -
我试过了,它适用于一些输入,但不是全部。我不知道为什么。
标签: python python-2.7 bayesian