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.

LogisticRegression

Logistic regression using gradient descent with sigmoid activation. Binary classification only.

Example

X := [[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]];
y := [[0.0], [0.0], [0.0], [1.0]];
model := LogisticRegression->New(0.1, 1000);
model->Fit(X, y);
labels := model->PredictClass([[1.0, 1.0]]);
labels[0]->PrintLine();

Operations

Fit #

Fits the model to training data using gradient descent.

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

Parameters

NameTypeDescription
XFloatfeature matrix (rows=samples, cols=features)
yFloattarget array (0.0 or 1.0 values, rows=samples, cols=1)

Return

TypeDescription
Booltrue if fitting succeeded

Example

X := [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
y := [[0.0], [0.0], [1.0]];
model := LogisticRegression->New(0.1, 200);
model->Fit(X, y);

GetBias #

Gets the learned bias / intercept term.

method : public : GetBias() ~ Float

Return

TypeDescription
Floatbias

GetLoss #

Gets the final binary cross-entropy loss recorded at the end of training. Note: batch gradient descent assumes roughly comparable feature scales -- apply FeatureScaler (e.g. StandardScaler) to inputs for reliable convergence.

method : public : GetLoss() ~ Float

Return

TypeDescription
Floattraining loss

GetWeights #

Gets the learned weights.

method : public : GetWeights() ~ Float[]

Return

TypeDescription
Floatweight array, or Nil if not fitted

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) ~ LogisticRegression

Parameters

NameTypeDescription
filenameStringfile to load from

Return

TypeDescription
LogisticRegressionfitted model, or Nil on failure

LogSigmoid # function

Numerically stable logistic sigmoid. Branching on the sign of x avoids Float->Exp overflow for large-magnitude inputs.

function : LogSigmoid(x:Float) ~ Float

Parameters

NameTypeDescription
xFloatinput value

Return

TypeDescription
Floatsigmoid(x) in (0.0, 1.0)

New # constructor

Constructor

New(learning_rate:Float, iterations:Int)

Parameters

NameTypeDescription
learning_rateFloatstep size for gradient descent
iterationsIntnumber of training iterations

Example

model := LogisticRegression->New(0.01, 500);

New # constructor

Constructor with L2 (ridge) regularization.

New(learning_rate:Float, iterations:Int, l2:Float)

Parameters

NameTypeDescription
learning_rateFloatstep size for gradient descent
iterationsIntnumber of training iterations
l2FloatL2 regularization strength (0.0 disables it)

Predict #

Predicts probabilities for input data.

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

Parameters

NameTypeDescription
XFloatfeature matrix

Return

TypeDescription
Floatprobability array (values between 0 and 1)

Example

probs := model->Predict([[2.0, 3.0]]);
"Probability: {$probs[0]}"->PrintLine();

PredictClass #

Predicts class labels for input data (threshold 0.5).

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

Parameters

NameTypeDescription
XFloatfeature matrix

Return

TypeDescription
Boolboolean class predictions

Example

labels := model->PredictClass([[5.0, 6.0], [1.0, 0.5]]);
each(label in labels) {
  label->PrintLine();
};

Score #

Computes classification accuracy on the given data.

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

Parameters

NameTypeDescription
XFloatfeature matrix
yFloattrue labels (0.0/1.0, rows x 1)

Return

TypeDescription
Floatfraction of correctly classified samples

Store #

Saves the fitted model (weights, bias, hyperparameters and final loss) to a file.

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

Parameters

NameTypeDescription
filenameStringfile to store to

Return

TypeDescription
Booltrue if successful, false otherwise