Cadmium::Classifier::Tabular::LogisticRegression
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}
Constructors
Instance methods
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)