knn_classifier 0.1.2

This simple library is a classifier for the k-Nearest Neighbors (kNN/k-nn) algorithm.
Documentation
  • Coverage
  • 46.67%
    7 out of 15 items documented0 out of 10 items with examples
  • Size
  • Source code size: 13.73 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 640.05 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • kujirahand/rust-knn-classifier
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • kujirahand

k-nn classifier

This is a library for solving classification problems using the k-nearest neighbor (k-nn) algorithm. Due to the simplicity of the algorithm, it is lightweight and well-suited for easily solving classification problems.

Install

cargo add knn_classifier

Simple Example

The following sample is a program that determines if a person is of normal weight or fat, 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(&[vec![159., 85.], vec![165., 55.]]);
    println!("{:?}", labels); // ["Fat", "Normal"]
    assert_eq!(labels, ["Obesity", "Normal"]);
}

Support CSV format

The classifier can be converted to and from CSV format.

// Convert Data to CSV
let s = clf.to_csv(',');
println!("{}", s);

// Convert from CSV
clf.from_csv(&s, ',', 0, false);

// Predict one
let label = clf.predict_one(&[150., 80.]);
assert_eq!(label, "Obesity");

Samples

Reference