A machine learning package for Rust.
For full usage details, see the API documentation.
This crate is mostly an excuse for me to learn Rust. Nevertheless, it contains reasonably effective implementations of a number of common machine learing algorithms.
At the moment, rustlearn
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.
libsvm
library,All the models support fitting and prediction on both dense and sparse data, and the implementations
should be roughly competitive with Python sklearn
implementations, both in accuracy and performance.
Model serialization is supported via rustc_serialize
. This will probably change to serde
once compiler plugins land in stable.
rustlearn
Usage should be straightforward.
rust
use rustlearn::prelude::*;
```rust use rustlearn::prelude::*;
use rustlearn::linear_models::sgdclassifier::Hyperparameters; // more imports ```
```rust use rustlearn::prelude::*; use rustlearn::datasets::iris; use rustlearn::crossvalidation::CrossValidation; use rustlearn::linearmodels::sgdclassifier::Hyperparameters; use rustlearn::metrics::accuracy_score;
let (X, y) = iris::load_data();
let numsplits = 10; let numepochs = 5;
let mut accuracy = 0.0;
for (trainidx, testidx) 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())
.learning_rate(0.5)
.l2_penalty(0.0)
.l1_penalty(0.0)
.one_vs_rest();
for _ in 0..num_epochs {
model.fit(&X_train, &y_train).unwrap();
}
let prediction = model.predict(&X_test).unwrap();
accuracy += accuracy_score(&y_test, &prediction);
}
accuracy /= num_splits as f32;
```
```rust use rustlearn::prelude::*;
use rustlearn::ensemble::randomforest::Hyperparameters; use rustlearn::datasets::iris; use rustlearn::trees::decisiontree;
let (data, target) = iris::load_data();
let mut treeparams = decisiontree::Hyperparameters::new(data.cols()); treeparams.minsamplessplit(10) .maxfeatures(4);
let mut model = Hyperparameters::new(treeparams, 10) .onevs_rest();
model.fit(&data, &target).unwrap();
// Optionally serialize and deserialize the model
// let encoded = bincode::rustcserialize::encode(&model,
// bincode::SizeLimit::Infinite).unwrap();
// let decoded: OneVsRestWrapper
let prediction = model.predict(&data).unwrap(); ```
Pull requests are welcome.
To run basic tests, run cargo test
.
Running 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.