v2026.6.1
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.

GaussianNaiveBayes

Gaussian naive Bayes for continuous features and integer class labels. Per class and feature it fits a mean and variance, then classifies by maximum log-joint probability (log prior + sum of log Gaussian likelihoods). Variances are floored for numerical stability.

Example

X := [[1.0], [1.2], [8.0], [8.2]];
y := [[0.0], [0.0], [1.0], [1.0]];
gnb := GaussianNaiveBayes->New();
gnb->Fit(X, y);
classes := gnb->PredictClass([[1.1], [7.9]]);   # [0, 1]

Operations

Fit #

Fits per-class feature means/variances and class priors. Class labels are the integer values 0..C-1 in the target column; every class in the range must appear at least once.

method : public : Fit(X:Float[,], y:Float[,]) ~ Bool

Parameters

NameTypeDescription
XFloatfeature matrix (rows=samples, cols=features)
yFloatclass labels (integer values stored as Float, rows x 1)

Return

TypeDescription
Booltrue if fitting succeeded

GetNumClasses #

Gets the number of classes seen at fit time.

method : public : GetNumClasses() ~ Int

Return

TypeDescription
Intclass count

IsFitted #

Whether the model has been fitted.

method : public : IsFitted() ~ Bool

Return

TypeDescription
Booltrue if fitted

Load # function

Loads a fitted model from a file.

function : Load(filename:String) ~ GaussianNaiveBayes

Parameters

NameTypeDescription
filenameStringfile to load from

Return

TypeDescription
GaussianNaiveBayesfitted model, or Nil on failure

Predict #

Computes the log-joint probability of each class for every row.

method : public : Predict(X:Float[,]) ~ Float[,]

Parameters

NameTypeDescription
XFloatfeature matrix

Return

TypeDescription
Floatrows x classes matrix of log-joint scores, or Nil if not fitted

PredictClass #

Predicts the most likely class for every row.

method : public : PredictClass(X:Float[,]) ~ Int[]

Parameters

NameTypeDescription
XFloatfeature matrix

Return

TypeDescription
Intper-row class ids, or Nil if not fitted

Score #

Computes classification accuracy on the given data.

method : public : Score(X:Float[,], y:Float[,]) ~ Float

Parameters

NameTypeDescription
XFloatfeature matrix
yFloattrue class labels (integer values stored as Float, rows x 1)

Return

TypeDescription
Floatfraction of correctly classified samples

Store #

Saves the fitted model to a file.

method : public : Store(filename:String) ~ Bool

Parameters

NameTypeDescription
filenameStringfile to store to

Return

TypeDescription
Booltrue if successful, false otherwise