class
Cadmium::Classifier::Tabular::LogisticRegression
- Cadmium::Classifier::Tabular::LogisticRegression
- Reference
- Object
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
- Fast prediction after training (O(1))
- Probabilistic output
- Works well with large datasets
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.crConstructors
-
.load_model(path : String) : self
Load a trained model from a file.
- .new(learning_rate : Float64 = 0.01, max_iterations : Int32 = 1000)
Instance Method Summary
-
#bias : Float64
Get the learned bias term.
-
#classify(features : Array(Float64)) : String
Classify a new sample and return the predicted label.
-
#classify_batch(features_batch : Array(Array(Float64))) : Array(String)
Classify multiple samples at once.
-
#classify_probabilities(features : Array(Float64)) : Hash(String, Float64)
Classify a new sample and return probability scores for both classes.
-
#learning_rate : Float64
Learning rate for gradient descent
-
#max_iterations : Int32
Maximum number of training iterations
-
#save_model(path : String) : Nil
Save the trained model to a file.
-
#train(features : Array(Array(Float64)), labels : Array(String)) : self
Train the classifier using gradient descent.
-
#weights : Array(Float64)
Get the learned weights (for inspection/debugging).
Constructor Detail
Load a trained model from a file.
classifier = Cadmium::Classifier::Tabular::LogisticRegression.load_model("logistic_regression_model.msgpack")
Instance Method Detail
Classify a new sample and return the predicted label.
classifier.classify([1.0, 2.0, 3.0]) # => "class_a"
Classify multiple samples at once.
results = classifier.classify_batch([[1.0, 2.0], [3.0, 4.0]])
# => ["class_a", "class_b"]
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}
Save the trained model to a file.
classifier.save_model("logistic_regression_model.msgpack")
Train the classifier using gradient descent.
features = [[1.0, 2.0], [3.0, 4.0]]
labels = ["a", "b"]
classifier.train(features, labels)