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[,]) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| X | Float | feature matrix (rows=samples, cols=features) |
| y | Float | target matrix (0.0 or 1.0 values, rows=samples, cols=1) |
Return
| Type | Description |
|---|---|
| Bool | true if fitting succeeded |
GetBias #
Gets the learned bias term.
method : public : GetBias() ~ FloatReturn
| Type | Description |
|---|---|
| Float | bias |
GetEpochsRun #
Gets the number of training epochs actually run (early-stops after an epoch with no mistakes).
method : public : GetEpochsRun() ~ IntReturn
| Type | Description |
|---|---|
| Int | epochs run |
GetWeights #
Gets the learned weights.
method : public : GetWeights() ~ Float[]Return
| Type | Description |
|---|---|
| Float | weight array, or Nil if not fitted |
IsFitted #
Whether the model has been fitted.
method : public : IsFitted() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if fitted |
Load # function
Loads a fitted model from a file.
function : Load(filename:String) ~ PerceptronParameters
| Name | Type | Description |
|---|---|---|
| filename | String | file to load from |
Return
| Type | Description |
|---|---|
| Perceptron | fitted model, or Nil on failure |
New # constructor
Constructor
New(learning_rate:Float, iterations:Int)Parameters
| Name | Type | Description |
|---|---|---|
| learning_rate | Float | update step size |
| iterations | Int | maximum training epochs |
Predict #
Computes raw decision values (w*x + b) for input data.
method : public : Predict(X:Float[,]) ~ Float[]Parameters
| Name | Type | Description |
|---|---|---|
| X | Float | feature matrix |
Return
| Type | Description |
|---|---|
| Float | decision value array, or Nil if not fitted |
PredictClass #
Predicts class labels (decision value >= 0).
method : public : PredictClass(X:Float[,]) ~ Bool[]Parameters
| Name | Type | Description |
|---|---|---|
| X | Float | feature matrix |
Return
| Type | Description |
|---|---|
| Bool | boolean class predictions, or Nil if not fitted |