rustlearn

Machine learning crate for Rust

  • 所有者: maciejkula/rustlearn
  • 平台:
  • 許可證: Apache License 2.0
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

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 alll 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.

主要指標

概覽
名稱與所有者maciejkula/rustlearn
主編程語言Rust
編程語言Rust (語言數: 3)
平台
許可證Apache License 2.0
所有者活动
創建於2015-12-03 21:48:17
推送於2021-06-07 09:09:59
最后一次提交2020-06-20 19:01:58
發布數6
最新版本名稱v0.5.0 (發布於 )
第一版名稱0.2.0 (發布於 2015-12-14 19:36:18)
用户参与
星數637
關注者數22
派生數54
提交數76
已啟用問題?
問題數17
打開的問題數11
拉請求數33
打開的拉請求數2
關閉的拉請求數0
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?