Crate knn_classifier
source ·Expand description
This is a library for solving classification problems using the k-NN algorithm. Due to the simplicity of the algorithm, it is lightweight and well-suited for easily solving classification problems.
§Simple Example
The following sample is a program that determines if a person is of normal weight or Obesity, based on their height(cm) and weight(kg).
use knn_classifier::KnnClassifier;
fn main() {
// Create the classifier
let mut clf = KnnClassifier::new(3);
// Learn from data
clf.fit(
&[&[170., 60.], &[166., 58.], &[152., 99.], &[163., 95.], &[150., 90.]],
&["Normal", "Normal", "Obesity", "Obesity", "Obesity"]);
// Predict
let labels = clf.predict(&[&[159., 85.], &[165., 55.]]);
println!("{:?}", labels); // ["Obesity", "Normal"]
assert_eq!(labels, ["Obesity", "Normal"]);
}
§Support CSV format
The classifier can be converted to and from CSV format.
let s = clf.to_csv(',');
println!("{}", s);
// Convert from CSV (Label columns is 0)
clf.from_csv(&s, ',', 0);
// Predict one
let label = clf.predict_one(&[150., 80.]);
assert_eq!(label, "Obesity");