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
| Name | Type | Description |
|---|---|---|
| left | BayesGroup | left group |
| right | BayesGroup | right group |
Query #
Query to Naive Bayes
method : public : Query(terms:String[]) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| terms | String | list of term to calculate |
Return
| Type | Description |
|---|---|
| Int | 0 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) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| terms | String | observed terms |
| group | BayesGroup | class group to score against |
Return
| Type | Description |
|---|---|
| Float | log-probability score (higher is more likely) |