This provides code for a general Naive Bayes learner.
Naive Bayes is a supervised classification algorithm that uses Bayes
rule to compute the fit between a new observation and some previously
observed data. The observations are discrete feature vectors, with
the Bayes assumption that the features are independent. Although this
is hardly ever true, the classifier works well enough in practice.
Glossary:
observation A feature vector of discrete data.
class A possible classification for an observation.
Classes:
NaiveBayes Holds information for a naive Bayes classifier.
Functions:
train Train a new naive Bayes classifier.
calculate Calculate the probabilities of each class, given an observation.
classify Classify an observation into a class.
Imported modules
|
|
from Bio.Tools import mathfns, listfns
|
Functions
|
|
calculate
classify
train
|
|
calculate
|
calculate (
nb,
observation,
scale=0,
)
calculate(nb, observation[, scale]) -> probability dict
Calculate log P(class|observation) for each class. nb is a NaiveBayes
classifier that has been trained. observation is a list representing
the observed data. scale is whether the probability should be
scaled by P(observation). By default, no scaling is done. The return
value is a dictionary where the keys is the class and the value is the
log probability of the class.
Exceptions
|
|
ValueError, "observation in %d dimension, but classifier in %d" %( len( observation ), nb.dimensionality )
|
|
|
classify
|
classify ( nb, observation )
classify(nb, observation) -> class
Classify an observation into a class.
|
|
train
|
train (
training_set,
results,
priors=None,
typecode=None,
)
train(training_set, results[, priors]) -> NaiveBayes
Train a naive bayes classifier on a training set. training_set is a
list of observations. results is a list of the class assignments
for each observation. Thus, training_set and results must be the same
length. priors is an optional dictionary specifying the prior
probabilities for each type of result. If not specified, the priors
will be estimated from the training results.
Exceptions
|
|
ValueError, "No data in the training set."
ValueError, "observations have different dimensionality"
ValueError, "training_set and results should be parallel lists."
|
|
Classes
|
|
NaiveBayes |
Holds information for a NaiveBayes classifier.
|
|
|