Python 天真的eBayes : Why am I getting a ZeroDivisionError

标签 python python-2.7 bayesian

我正在尝试 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

我是贝叶斯分类器的新手,所以我的输入是否存在问题(即:数字的分布,或者是否没有足够的样本?)

最佳答案

有两个问题:

首先,您使用的是 python 2.7,并且 NaiveBayes需要 python 3。使用 python 2,它使用的除法会变成整数除法并返回零。

其次,每个标签的每个属性只有一个实例,因此西格玛为零。

为您的真实属性添加更多变化:

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': 233,
                          'Weight': 234,
                          },
                     'cases': 32,
                     'label':  'Sex=M'})
model.add_instances({'attributes':
                         {'Height': 190,
                          'Weight': 152
                          },
                     'cases': 58,
                     'label': 'Sex=F'
                     })
model.add_instances({'attributes':
                         {'Height': 191,
                          'Weight': 153
                          },
                     'cases': 58,
                     'label': 'Sex=F'
                     })

model.train()
result = model.predict({'attributes': {'Height': 212, 'Weight': 200}})

print ("The result is %s" % (result))

并使用 python3:

$ python3 bayes.py
The result is {'Sex=M': 1.0, 'Sex=F': 0.0}

关于Python 天真的eBayes : Why am I getting a ZeroDivisionError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15406737/

相关文章:

python - 我如何遍历页面并使用 selenium 从每个页面获取数据?

python - Spyder 终端内的 3D matplotlib 图

python - Python 中作为函数指针的类字段

python - 向字典添加键和值

python - 使用 Hydrogen,我如何保存 python 脚本并像 jupyter notebook 一样包含输出?

machine-learning - 贝叶斯集和寻找顶级集

r - 在 rstan 中转换变量(贝叶斯分析)

python - 连接操作后如何避免重复列?

javascript - 使用 Python 自动登录我的本地互联网页面

python - 找不到 numpyro.render_model