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.

Perceptron

Classic Rosenblatt perceptron: a mistake-driven linear binary classifier. Deterministic (samples are visited in order each epoch) and guaranteed to converge on linearly separable data; training stops early after an epoch with no mistakes.

Example

X := [[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]];
y := [[0.0], [1.0], [1.0], [1.0]];   # OR
model := Perceptron->New(0.1, 100);
model->Fit(X, y);
labels := model->PredictClass(X);

Operations

Fit #

Fits the perceptron with the classic mistake-driven update rule.

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

Parameters

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

Return

TypeDescription
Booltrue if fitting succeeded

GetBias #

Gets the learned bias term.

method : public : GetBias() ~ Float

Return

TypeDescription
Floatbias

GetEpochsRun #

Gets the number of training epochs actually run (early-stops after an epoch with no mistakes).

method : public : GetEpochsRun() ~ Int

Return

TypeDescription
Intepochs run

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

Parameters

NameTypeDescription
filenameStringfile to load from

Return

TypeDescription
Perceptronfitted model, or Nil on failure

New # constructor

Constructor

New(learning_rate:Float, iterations:Int)

Parameters

NameTypeDescription
learning_rateFloatupdate step size
iterationsIntmaximum training epochs

Predict #

Computes raw decision values (w*x + b) for input data.

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

Parameters

NameTypeDescription
XFloatfeature matrix

Return

TypeDescription
Floatdecision value array, or Nil if not fitted

PredictClass #

Predicts class labels (decision value >= 0).

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

Parameters

NameTypeDescription
XFloatfeature matrix

Return

TypeDescription
Boolboolean class predictions, 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 labels (0.0/1.0, 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