Skip to main content

Crate libsvm_rs

Crate libsvm_rs 

Source
Expand description

§libsvm-rs

libsvm-rs is a pure Rust reimplementation of LIBSVM for training, prediction, cross-validation, and LIBSVM text model/problem I/O without an FFI boundary. The public API mirrors the original LIBSVM concepts while using owned Rust data and Result-based error handling; see the migration guide for a C-to-Rust mapping.

§LIBSVM parity

The crate targets numerical equivalence and model-file compatibility with upstream LIBSVM rather than bit-for-bit identity. Core counterparts include train::svm_train for svm_train, cross_validation::svm_cross_validation for svm_cross_validation, predict::predict_values / predict::predict / predict::predict_probability for the prediction APIs, and io::save_model / io::load_model for LIBSVM model persistence.

§Quick example

use libsvm_rs::predict::predict;
use libsvm_rs::train::svm_train;
use libsvm_rs::{KernelType, SvmNode, SvmParameterBuilder, SvmProblem, SvmType};

let problem = SvmProblem {
    labels: vec![-1.0, -1.0, 1.0, 1.0],
    instances: vec![
        vec![SvmNode { index: 1, value: -2.0 }],
        vec![SvmNode { index: 1, value: -1.0 }],
        vec![SvmNode { index: 1, value: 1.0 }],
        vec![SvmNode { index: 1, value: 2.0 }],
    ],
};
let param = SvmParameterBuilder::new()
    .svm_type(SvmType::CSvc)
    .kernel_type(KernelType::Linear)
    .build()?;

let model = svm_train(&problem, &param);
let label = predict(&model, &[SvmNode { index: 1, value: 1.5 }]);
assert_eq!(label, 1.0);

§Status

Training works for all 5 SVM types (C-SVC, ν-SVC, one-class, ε-SVR, ν-SVR). See train::svm_train for training, predict::predict for inference, and predict::predict_probability for probabilistic outputs. The project repository is https://github.com/ricardofrantz/libsvm-rs.

§Trust Boundary

Problem and model files are treated as untrusted text input by default. The io loaders apply LoadOptions caps, reject malformed sparse feature rows, and validate model-header consistency before allocating support-vector storage. These checks bound parsing work and memory use; they do not authenticate a model or prove that it is appropriate for a particular deployment.

§Feature Flags

  • rayon — Enable parallel cross-validation (off by default), including probability-calibration CV folds for binary SVC models. Fold assignment remains serial and deterministic, then each fold trains on an independent worker. Per-fold training diagnostics are suppressed while the parallel workers run so output cannot interleave; use the default serial path if you need fold-internal progress text. With k parallel folds, peak memory can include up to min(k, rayon_threads) simultaneous kernel caches of SvmParameter::cache_size each; the cache size is never divided implicitly.
  • serde — Enable Serialize/Deserialize for model and parameter types. SvmType and KernelType serialize as pinned LIBSVM integer codes (0..4). Deserializing SvmModel runs the same structural validation as the text model loader; LIBSVM text model files remain the C-compatible interchange format.

Re-exports§

pub use builder::SvmParameterBuilder;
pub use error::SvmError;
pub use io::LoadOptions;
pub use metrics::accuracy_percentage;
pub use metrics::regression_metrics;
pub use types::*;

Modules§

builder
Fluent builder for SvmParameter values. Fluent construction for SvmParameter.
cache
Kernel-row cache used by the SMO solver. LRU kernel cache matching the original LIBSVM.
cross_validation
Cross-validation entry point corresponding to LIBSVM’s svm_cross_validation. Cross-validation for SVM models.
error
Error types returned by fallible parsing, validation, and I/O APIs. Error types returned by libsvm-rs.
io
LIBSVM problem/model text-format loading and saving. I/O routines for LIBSVM problem and model files.
kernel
Kernel functions equivalent to LIBSVM’s linear, polynomial, RBF, sigmoid, and precomputed kernels. Kernel functions matching the original LIBSVM.
metrics
Helpers for classification accuracy and regression error summaries. Lightweight scoring helpers for CLI outputs and test checks.
predict
Prediction entry points corresponding to LIBSVM’s svm_predict* APIs. Prediction functions matching the original LIBSVM.
probability
Probability-estimation routines corresponding to LIBSVM’s Platt-scaling and related helpers. Probability estimation functions for SVM models.
qmatrix
Q-matrix implementations consumed by the SMO solver. Q matrix implementations for the SMO solver.
solver
Sequential Minimal Optimization solver internals corresponding to LIBSVM’s Solver and Solver_NU. SMO solver for the SVM dual problem.
train
Training entry points corresponding to LIBSVM’s svm_train. SVM training pipeline.
types
LIBSVM-compatible public data structures and C-style helper functions. Core LIBSVM-compatible data structures.
util
Utility routines shared by training, parsing, and parity-oriented algorithms. Shared internal utilities for libsvm-rs.

Functions§

set_quiet
Enable or disable quiet mode. When quiet, solver diagnostic messages are suppressed (equivalent to LIBSVM’s -q flag).