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[,]) ~ 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 |
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) ~ SVMParameters
| Name | Type | Description |
|---|---|---|
| filename | String | file to load from |
Return
| Type | Description |
|---|---|
| SVM | fitted model, or Nil on failure |
New # constructor
Constructor
New(lambda:Float, learning_rate:Float, iterations:Int)Parameters
| Name | Type | Description |
|---|---|---|
| lambda | Float | L2 regularization strength (> 0.0) |
| learning_rate | Float | subgradient step size |
| iterations | Int | training 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
| 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 |