v2026.6.4
All Bundles
Bundle Core machine learning types: the seedable Random generator, Matrix2D linear algebra and activations, matrix reference holders, the NeuralNetwork and CSV-backed MatrixReader. Compile with -lib ml.

NaiveBayes

Naive Bayes ML algorithm

Example

normal := BayesGroup->New(8.0 / 12.0);
normal->AddEntry(BayesEntry->New("dear", 8));
normal->AddEntry(BayesEntry->New("friend", 5));
normal->AddEntry(BayesEntry->New("lunch", 3));
normal->AddEntry(BayesEntry->New("money", 1));

spam := BayesGroup->New(4.0 / 12.0);
spam->AddEntry(BayesEntry->New("dear", 2));
spam->AddEntry(BayesEntry->New("friend", 1));
spam->AddEntry(BayesEntry->New("lunch", 0));
spam->AddEntry(BayesEntry->New("money", 4));

bayes := NaiveBayes->New(normal, spam);
finding := bayes->Query(["lunch", "money", "money", "money", "money"]);

if(finding = 0) {
  "normal"->PrintLine();
}
else if(finding = 1) {
  "spam"->PrintLine();
}
else {
  "invalid"->PrintLine();
}

Operations

New # constructor

Constructor

New(left:BayesGroup, right:BayesGroup)

Parameters

NameTypeDescription
leftBayesGroupleft group
rightBayesGroupright group

Query #

Query to Naive Bayes

method : public : Query(terms:String[]) ~ Int

Parameters

NameTypeDescription
termsStringlist of term to calculate

Return

TypeDescription
Int0 if left, 1 if right, -1 otherwise

Query #

Computes the Laplace-smoothed log-probability that the given terms belong to the supplied group. Likelihoods are summed in log space to avoid floating-point underflow on long term lists, and the group's counts are never mutated (so queries are side-effect free and repeatable).

method : private : Query(terms:String[], group:BayesGroup) ~ Float

Parameters

NameTypeDescription
termsStringobserved terms
groupBayesGroupclass group to score against

Return

TypeDescription
Floatlog-probability score (higher is more likely)