Learn the Rust programming language through implementing classic machine learning algorithms. This project is self-completed without relying on any third-party libraries, serving as a bootstrap machine learning library.
❗❗❗:Actively seeking code reviews and welcome suggestions on fixing bugs or code refactoring. Please feel free to share your ideas. Happy to accept advice!
broadcast
, matrix operations
, permute
and etc. in arbitrary dimension. SIMD is used in matrix multiplication thanks to auto vectorizing by Rust.normalize
, shuffle
and Dataloader
. Several popular dataset pre-processing recipes are available.gini
or entropy
are provided.Lasso
, Ridge
and L-inf
)linear(MLP)
and some activation
functions which could be freely stacked and optimized by gradient back propagations.KdTree
and vanilla BruteForceSearch
.Let's use KNN algorithm to solve a classification task. More examples can be found in examples
directory.
create some synthetic data for tests
```rust use std::collections::HashMap;
let features = vec![ vec![0.6, 0.7, 0.8], vec![0.7, 0.8, 0.9], vec![0.1, 0.2, 0.3], ]; let labels = vec![0, 0, 1]; // so it is a binary classifiction task, 0 is for the large label, 1 is for the small label let mut labelmap = HashMap::new(); labelmap.insert(0, "large".tostring()); labelmap.insert(1, "small".to_string()); ```
convert the data to the dataset
```rust use mlinrust::dataset::Dataset;
let dataset = Dataset::new(features, labels, Some(label_map)); ```
split the dataset into train
and valid
sets and normalize them by Standard normalization
```rust let mut temp = dataset.splitdataset(vec![2.0, 1.0], 0); // [2.0, 1.0] is the split fraction, 0 is the seed let (mut traindataset, mut valid_dataset) = (temp.remove(0), temp.remove(0));
use mlinrust::dataset::utils::{normalize_dataset, ScalerType};
normalizedataset(&mut traindataset, ScalerType::Standard); normalizedataset(&mut validdataset, ScalerType::Standard); ```
build and train our KNN model using KdTree
```rust use mlinrust::model::knn::{KNNAlg, KNNModel, KNNWeighting};
// KdTree is one implementation of KNN; 1 defines the k of neighbours; Weighting decides the way of ensemble prediction; traindataset is for training KNN; Some(2) is the param of minkowski distance let model = KNNModel::new(KNNAlg::KdTree, 1, Some(KNNWeighting::Distance), traindataset, Some(2)); ```
evaluate the model
```rust use mlinrust::utils::evaluate;
let (correct, acc) = evaluate(&validdataset, &model); println!("evaluate results\ncorrect {correct} / total {}, acc = {acc:.5}", testdataset.len()); ```
The rust community. I received many help from rust-lang Discord.
Under GPL-v3 license. And commercial use is strictly prohibited.