DJG
DJG

Reputation: 6543

Python NaiveBayes: Why am I getting a ZeroDivisionError

I am trying out the NaiveBayes Python library (python 2.7)

I am wondering why running this code is giving me a 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))

And here is the output:

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

I am new to Bayesian Classifiers, so is there a problem with my input (ie: the distributions of the numbers, or are there not enough samples?)

Upvotes: 2

Views: 592

Answers (1)

Pavel Anossov
Pavel Anossov

Reputation: 62898

There are two problems:

First, you are using python 2.7, and NaiveBayes requires python 3. With python 2 the divisions it uses turn to integer divisions and return zeroes.

Second, there are only a single instance of each attribute per label, so sigmas are zero.

Add more variation to your real attributes:

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))

And use python3:

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

Upvotes: 3

Related Questions