Module: aprender::classification

Public module of the aprender-core crate.

Source

crates/aprender-core/src/classification.rs or directory.

Example

use aprender::classification::LogisticRegression;
// See `cargo doc -p aprender-core --open` for full API reference.

Module summary

aprender::classification collects the linear and instance-based classifiers that complement tree ensembles for tabular problems: logistic regression, the linear support vector machine, Gaussian Naive Bayes, and k-nearest neighbours. All four return usize class indices and follow the same fit / predict contract — predict here returns Vec<usize>, not a Vector<f32>, because the targets are discrete labels.

Key types

TypeDescription
LogisticRegressionMultinomial logistic regression with optional class weighting (ClassWeight) and gradient-descent or L-BFGS fit modes (FitMode).
LinearSVMHinge-loss SVM solved with subgradient descent; sparse and fast on high-dimensional inputs.
GaussianNBGaussian Naive Bayes with per-class mean / variance estimates; closed-form, no iterative training.
KNearestNeighborsK-nearest neighbours with selectable DistanceMetric (Euclidean, Manhattan, Cosine).
ClassWeightEnum that controls class re-weighting in logistic regression (Balanced / Custom).

Usage patterns

Pattern 1: Logistic regression on separable data

use aprender::metrics::classification::accuracy;
use aprender::prelude::*;

let x = Matrix::from_vec(6, 2, vec![
    1.0, 2.0, 2.0, 3.0, 3.0, 1.0,    // class 0
    6.0, 5.0, 7.0, 8.0, 8.0, 6.0,    // class 1
]).expect("valid 6x2 matrix");
let y: Vec<usize> = vec![0, 0, 0, 1, 1, 1];

let mut lr = LogisticRegression::new()
    .with_learning_rate(0.1)
    .with_max_iter(1000);
lr.fit(&x, &y).expect("logistic regression fit");

let preds = lr.predict(&x);
let acc = accuracy(&preds, &y);
assert!(acc >= 0.8, "training accuracy contract");
println!("accuracy: {:.2}", acc);

Pattern 2: KNN with custom distance metric

use aprender::classification::{DistanceMetric, KNearestNeighbors};
use aprender::primitives::Matrix;

let x = Matrix::from_vec(4, 2, vec![
    0.0, 0.0,
    0.0, 1.0,
    5.0, 5.0,
    5.0, 6.0,
]).expect("valid 4x2 matrix");
let y = vec![0_usize, 0, 1, 1];

let mut knn = KNearestNeighbors::new(3)
    .with_metric(DistanceMetric::Euclidean);
knn.fit(&x, &y).expect("knn fit");

let probe = Matrix::from_vec(1, 2, vec![0.1, 0.1]).expect("probe matrix");
let prediction = knn.predict(&probe);
assert_eq!(prediction[0], 0, "near origin → class 0");

See also

  • tree — decision tree / random forest classifiers
  • ensemble — boosted classifiers (MixtureOfExperts, etc.)
  • metricsaccuracy, precision, recall, F1 in metrics::classification
  • calibrationTemperatureScaling / PlattScaling to calibrate probabilistic outputs
  • model_selection — cross-validation for picking k, learning rate, or C

Full API

Run cargo doc -p aprender-core --open for the rendered rustdoc, or browse docs.rs/aprender for the published version.