• Stars
    star
    598
  • Rank 71,996 (Top 2 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Machine learning crate for Rust

rustlearn

Circle CI Crates.io

A machine learning package for Rust.

For full usage details, see the API documentation.

Introduction

This crate contains reasonably effective implementations of a number of common machine learning 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.

Features

Matrix primitives

Models

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.

Cross-validation

Metrics

Parallelization

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

Model serialization

Model serialization is supported via serde.

Using rustlearn

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

Examples

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())
                                    .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;

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();

model.fit(&data, &target).unwrap();

// Optionally serialize and deserialize the model

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

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

Contributing

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.

More Repositories

1

spotlight

Deep recommender models using PyTorch.
Python
2,857
star
2

glove-python

Toy Python implementation of http://www-nlp.stanford.edu/projects/glove/
Python
1,232
star
3

triplet_recommendations_keras

An example of doing MovieLens recommendations using triplet loss in Keras
Jupyter Notebook
417
star
4

wyrm

Autodifferentiation package in Rust.
Rust
169
star
5

sbr-go

Recommender systems for Go
Go
169
star
6

sbr-rs

Deep recommender systems for Rust
Rust
115
star
7

netrex

Neural network recommendation models in PyTorch
Python
57
star
8

recommender_datasets

A common format and repository for various recommender datasets.
Python
38
star
9

mixture

TeX
26
star
10

python-rustlearn

Calling rustlearn from Python
Python
26
star
11

explicit-vs-implicit

An experiment on explicit vs implicit feedback recommenders
Jupyter Notebook
26
star
12

binge

Recommendation models that use binary rather than floating point operations at prediction time.
TeX
21
star
13

fizzbuzz

A FizzBuzz solver using a Rust autodifferentiation library.
Rust
20
star
14

dictionarylearning

Online learning of sparse dictionaries
Java
13
star
15

dynarray

Dynamic Numpy arrays
Python
13
star
16

hugo-blog

Rust
8
star
17

BayesianTestJS

Rudimentary Bayesian Beta-Bernoulli A/B testing inference and visualization code.
JavaScript
6
star
18

BetaJS

Beta distribution probability density function (PDF) for JavaScript
JavaScript
3
star
19

spotlight-recipe

Conda recipes for Spotlight
Shell
2
star
20

sbr-sys

Rust
2
star
21

blas_cython

Python
1
star
22

lightfm_datasets

Datasets for the LightFM package
Python
1
star
23

sketchy

Python
1
star
24

wheedle

Rust
1
star
25

fictional-octo-chainsaw

1
star