class Cadmium::Classifier::Tabular::LogisticRegression

Overview

Logistic Regression classifier for binary classification.

Uses gradient descent to learn weights that minimize binary cross-entropy loss. Suitable for binary classification tasks with numerical features.

Features

Example

classifier = Cadmium::Classifier::Tabular::LogisticRegression.new(learning_rate: 0.01, max_iterations: 1000)

features = [
  [1.0, 2.0, 3.0],
  [1.1, 2.1, 3.1],
  [5.0, 6.0, 7.0],
]
labels = ["class_a", "class_a", "class_b"]

classifier.train(features, labels)

result = classifier.classify([1.05, 2.05, 3.05])
# => "class_a"

probs = classifier.classify_probabilities([1.05, 2.05, 3.05])
# => {"class_a" => 0.85, "class_b" => 0.15}

Defined in:

cadmium/classifier/tabular/logistic_regression.cr

Constructors

Instance Method Summary

Constructor Detail

def self.load_model(path : String) : self #

Load a trained model from a file.

classifier = Cadmium::Classifier::Tabular::LogisticRegression.load_model("logistic_regression_model.msgpack")

[View source]
def self.new(learning_rate : Float64 = 0.01, max_iterations : Int32 = 1000) #

[View source]

Instance Method Detail

def bias : Float64 #

Get the learned bias term.


[View source]
def classify(features : Array(Float64)) : String #

Classify a new sample and return the predicted label.

classifier.classify([1.0, 2.0, 3.0]) # => "class_a"

[View source]
def classify_batch(features_batch : Array(Array(Float64))) : Array(String) #

Classify multiple samples at once.

results = classifier.classify_batch([[1.0, 2.0], [3.0, 4.0]])
# => ["class_a", "class_b"]

[View source]
def classify_probabilities(features : Array(Float64)) : Hash(String, Float64) #

Classify a new sample and return probability scores for both classes.

probs = classifier.classify_probabilities([1.0, 2.0, 3.0])
# => {"class_a" => 0.85, "class_b" => 0.15}

[View source]
def learning_rate : Float64 #

Learning rate for gradient descent


[View source]
def max_iterations : Int32 #

Maximum number of training iterations


[View source]
def save_model(path : String) : Nil #

Save the trained model to a file.

classifier.save_model("logistic_regression_model.msgpack")

[View source]
def train(features : Array(Array(Float64)), labels : Array(String)) : self #

Train the classifier using gradient descent.

features = [[1.0, 2.0], [3.0, 4.0]]
labels = ["a", "b"]
classifier.train(features, labels)

[View source]
def weights : Array(Float64) #

Get the learned weights (for inspection/debugging).


[View source]