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.

SVM

Linear support vector machine trained by deterministic full-batch subgradient descent on the L2-regularized hinge loss: lambda/2*||w||^2 + (1/n)*sum(max(0, 1 - y*(w*x + b))), with labels mapped internally from {0,1} to {-1,+1}. The bias is not regularized.

Example

model := SVM->New(0.01, 0.1, 500);
model->Fit(X, y);
labels := model->PredictClass(X);

Operations

Fit #

Fits the SVM by full-batch subgradient descent on the hinge loss.

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

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

Parameters

NameTypeDescription
filenameStringfile to load from

Return

TypeDescription
SVMfitted model, or Nil on failure

New # constructor

Constructor

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

Parameters

NameTypeDescription
lambdaFloatL2 regularization strength (> 0.0)
learning_rateFloatsubgradient step size
iterationsInttraining epochs

Predict #

Computes raw decision values (w*x + b) for input data. The sign gives the class; the magnitude reflects distance from the separating plane.

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