Python NaiveBayes:为什么我会得到一个零错误

Python NaiveBayes:为什么我会得到一个零错误,python,python-2.7,bayesian,Python,Python 2.7,Bayesian,我正在试用NaiveBayesPython库(python2.7) 我想知道为什么运行此代码会给我一个zero错误 #!/usr/bin/env python import NaiveBayes model = NaiveBayes.NaiveBayes() model.set_real(['Height']) model.set_real(['Weight']) model.add_instances({'attributes': {'He

我正在试用
NaiveBayes
Python库(python2.7)

我想知道为什么运行此代码会给我一个
zero错误

#!/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
回溯(最近一次呼叫最后一次):
文件“/tmp/py4127eDT”,第24行,在
结果=模型。预测({'attributes':{'Height':212,'Weight':200})
文件“/usr/local/lib/python2.7/dist packages/NaiveBayes.py”,第152行,在predict中
分数[标签]/=sumPx
ZeroDivisionError:浮点除以零

我不熟悉贝叶斯分类器,所以我的输入有问题吗(即:数字的分布,还是样本不足?)

有两个问题:

首先,您使用的是Python2.7,需要Python3。在Python2中,它使用的除法转换为整数除法并返回零

其次,每个标签只有一个属性实例,因此sigma为零

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

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}

(向黑暗中射击)如果你强迫高度和重量浮动,问题还会持续吗?e、 g.
239.
而不是
239
等等?我试过了,它对一些输入有效,但不是所有输入都有效。我不知道为什么。