by maciejkula

maciejkula /rustlearn

Machine learning crate for Rust

431 Stars 37 Forks Last release: about 2 years ago (v0.5.0) Apache License 2.0 76 Commits 6 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:


Circle CI

A machine learning package for Rust.

For full usage details, see the API documentation.


This crate contains reasonably effective implementations of a number of common machine learning algorithms.

At the moment,

uses its own basic dense and sparse array types, but I will be happy to use something more robust once a clear winner in that space emerges.


Matrix primitives


All the models support fitting and prediction on both dense and sparse data, and the implementations should be roughly competitive with Python

implementations, both in accuracy and performance.




A number of models support both parallel model fitting and prediction.

Model serialization

Model serialization is supported via



Usage should be straightforward.

  • import the prelude for all the linear algebra primitives and common traits:
use rustlearn::prelude::*;
  • import individual models and utilities from submodules:
use rustlearn::prelude::*;

use rustlearn::linear_models::sgdclassifier::Hyperparameters; // more imports


Logistic regression

use rustlearn::prelude::*;
use rustlearn::datasets::iris;
use rustlearn::cross_validation::CrossValidation;
use rustlearn::linear_models::sgdclassifier::Hyperparameters;
use rustlearn::metrics::accuracy_score;

let (X, y) = iris::load_data();

let num_splits = 10; let num_epochs = 5;

let mut accuracy = 0.0;

for (train_idx, test_idx) in CrossValidation::new(X.rows(), num_splits) {

let X_train = X.get_rows(&train_idx);
let y_train = y.get_rows(&train_idx);
let X_test = X.get_rows(&test_idx);
let y_test = y.get_rows(&test_idx);

let mut model = Hyperparameters::new(X.cols())

for _ in 0..num_epochs {, &y_train).unwrap();

let prediction = model.predict(&X_test).unwrap();
accuracy += accuracy_score(&y_test, &prediction);


accuracy /= num_splits as f32;

Random forest

use rustlearn::prelude::*;

use rustlearn::ensemble::random_forest::Hyperparameters; use rustlearn::datasets::iris; use rustlearn::trees::decision_tree;

let (data, target) = iris::load_data();

let mut tree_params = decision_tree::Hyperparameters::new(data.cols()); tree_params.min_samples_split(10) .max_features(4);

let mut model = Hyperparameters::new(tree_params, 10) .one_vs_rest();, &target).unwrap();

// Optionally serialize and deserialize the model

// let encoded = bincode::serialize(&model).unwrap(); // let decoded: OneVsRestWrapper = bincode::deserialize(&encoded).unwrap();

let prediction = model.predict(&data).unwrap();


Pull requests are welcome.

To run basic tests, run

cargo test


cargo test --features "all_tests" --release
runs all tests, including generated and slow tests. Running
cargo bench --features bench
(only on the nightly branch) runs benchmarks.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.