class
Cadmium::Classifier::Tabular::KNN
- Cadmium::Classifier::Tabular::KNN
- Reference
- Object
Overview
K-Nearest Neighbors classifier for multi-feature tabular data.
This classifier stores all training data and makes predictions by finding the k most similar training examples and taking a majority vote.
Features
- Handles numerical features of any dimension
- Supports multiple distance metrics (Euclidean, Manhattan, Cosine, etc.)
- No training phase - just data storage
- Suitable for small to medium datasets
Example
classifier = Cadmium::Classifier::Tabular::KNN.new(k: 3)
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)
# Predict new sample
result = classifier.classify([1.05, 2.05, 3.05])
# => "class_a"
# Get detailed results with vote counts
details = classifier.classify_details([1.05, 2.05, 3.05])
# => {"class_a" => 3, "class_b" => 0}
Defined in:
cadmium/classifier/tabular/knn.crConstructors
-
.load_model(path : String) : self
Load a trained model from a file.
- .new(k : Int32 = 5, distance_metric : DistanceMetric = DistanceMetric::Euclidean)
Instance Method Summary
-
#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_details(features : Array(Float64)) : Hash(String, Int32)
Classify a new sample and return detailed vote counts.
-
#distance_metric : DistanceMetric
Distance metric to use for finding nearest neighbors
-
#k : Int32
Number of neighbors to consider
-
#save_model(path : String) : Nil
Save the trained model to a file.
-
#train(features : Array(Array(Float64)), labels : Array(String)) : self
Train the classifier by storing feature vectors and labels.
-
#train(features : Array(Float64), label : String) : self
Train with a single sample.
Constructor Detail
Load a trained model from a file.
classifier = Cadmium::Classifier::Tabular::KNN.load_model("knn_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 detailed vote counts.
details = classifier.classify_details([1.0, 2.0, 3.0])
# => {"class_a" => 3, "class_b" => 2}
Save the trained model to a file.
classifier.save_model("knn_model.msgpack")
Train the classifier by storing feature vectors and labels.
features = [[1.0, 2.0], [3.0, 4.0]]
labels = ["a", "b"]
classifier.train(features, labels)
Train with a single sample.
classifier.train([1.0, 2.0, 3.0], "class_a")