ml_naive_bayes

ml_naive_bayes(x, y, newdata=None)

Gaussian naive Bayes classification.

Mirrors the C# NaiveBayes class: assumes each feature is normally distributed given the class and independent of the other features, then assigns each new observation the class with the highest posterior probability.

classes follows the order the class labels FIRST APPEAR in y, not sorted order, and every per-class row of means, standard_deviations and priors is indexed to match. A class with a single member gets a standard deviation of 1e-6 rather than 0.

Parameters

Name Type Description Default
x array_like Training predictors, one row per observation. required
y array_like The training class labels, one per row of x. required
newdata array_like Predictors to classify, with the same number of columns as x. None (the default) trains without predicting. None

Returns

Name Type Description
dict classes, means and standard_deviations (both one row per class and one column per feature), priors, and – when newdata is supplied – prediction.

Examples

>>> from corehydropy import ml_naive_bayes
>>> x = [1, 1.1, 0.9, 1.2, 1.05, 5, 5.1, 4.9, 5.2, 5.05]
>>> y = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
>>> ml_naive_bayes(x, y, newdata=[1.0, 5.0])["prediction"].tolist()
[0.0, 1.0]