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[,]) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| X | Float | feature matrix (rows=samples, cols=features) |
| y | Float | target array (0.0 or 1.0 values, rows=samples, cols=1) |
Return
| Type | Description |
|---|---|
| Bool | true 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() ~ FloatReturn
| Type | Description |
|---|---|
| Float | bias |
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() ~ FloatReturn
| Type | Description |
|---|---|
| Float | training loss |
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) ~ LogisticRegressionParameters
| Name | Type | Description |
|---|---|---|
| filename | String | file to load from |
Return
| Type | Description |
|---|---|
| LogisticRegression | fitted 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) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| x | Float | input value |
Return
| Type | Description |
|---|---|
| Float | sigmoid(x) in (0.0, 1.0) |
New # constructor
Constructor
New(learning_rate:Float, iterations:Int)Parameters
| Name | Type | Description |
|---|---|---|
| learning_rate | Float | step size for gradient descent |
| iterations | Int | number 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
| Name | Type | Description |
|---|---|---|
| learning_rate | Float | step size for gradient descent |
| iterations | Int | number of training iterations |
| l2 | Float | L2 regularization strength (0.0 disables it) |
Predict #
Predicts probabilities for input data.
method : public : Predict(X:Float[,]) ~ Float[]Parameters
| Name | Type | Description |
|---|---|---|
| X | Float | feature matrix |
Return
| Type | Description |
|---|---|
| Float | probability 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
| Name | Type | Description |
|---|---|---|
| X | Float | feature matrix |
Return
| Type | Description |
|---|---|
| Bool | boolean class predictions |
Example
labels := model->PredictClass([[5.0, 6.0], [1.0, 0.5]]);
each(label in labels) {
label->PrintLine();
};