| Crates.io | rust-ml |
| lib.rs | rust-ml |
| version | 0.1.5 |
| created_at | 2025-04-08 22:36:56.983512+00 |
| updated_at | 2025-04-18 17:01:38.314537+00 |
| description | A collection of machine learning algorithms implemented in pure Rust (personal project for practice). |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1626063 |
| size | 3,438,376 |
Rust-ML is a personal, and educational project that aims to combine Rust programming skills with machine learning concepts. The project leverages Rust's performance characteristics, memory safety guarantees, and expressive type system to build reliable machine learning tools while providing an opportunity to deepen understanding of both Rust and machine learning algorithms.
This is a personal project, and I will be working on it when I have time. If you find it useful, feel free to use it, but please keep in mind that it's not a production-ready library. This project is not intended to be a full-fledged machine learning library like TensorFlow or PyTorch, but rather a learning tool for me to explore the intersection of Rust and machine learning.
The project is organized into several key modules:
src/core/)Contains fundamental data structures and utilities:
types.rs: Defines common types used throughout the library like
ModelParamsparam_storage.rs: Provides storage mechanisms for model parametersparam_manager.rs: Manages parameter operations and transformationserror.rs: Defines error types and handling for the core modulesrc/model/)Houses machine learning model implementations:
core/: Contains base traits and interfaces for models:
base.rs: Defines base model traitsoptimizable_model.rs: Trait for models that can be optimizedregression_model.rs: Trait for regression modelsclassification_model.rs: Trait for classification modelslinear_regression.rs: Implementation of linear regression model
Other model implementations
src/optimization/)Provides optimization algorithms for training models:
core/:
optimizer.rs: Defines the Optimizer trait and related interfacesgradient_descent.rs: Implementation of gradient descent optimization
src/bench/)Tools for profiling and evaluating model performance:
core/:
profiler.rs: Base trait for profiling model training and evaluationerror.rs: Error handling for profiling operationstrain_metrics.rs: Defines metrics for training evaluationregression_profiler.rs: Profiler for regression models
regression_metrics.rs: Metrics specific to regression models
src/builders/)Factory patterns for creating and configuring models:
builder.rs: Defines builder interfaceslinear_regression.rs: Builder for linear regression modelssrc/bin/)Executable examples demonstrating library usage:
linear_regression.rs: Example usage of linear regressionsl_classifier.rs: Example of supervised learning classifierThe library employs a robust error handling strategy using Rust's Result type
and the thiserror crate:
From trait? operator is used for ergonomic error propagationThis approach ensures clear error messages and type-safe error handling throughout the codebase.
Add Rust-ML to your project by including it in your Cargo.toml:
[dependencies]
rust-ml = "0.1.0"
Or clone the repository to contribute or run examples:
git clone https://github.com/username/rust-ml.git
cd rust-ml
cargo build
cargo test
cargo bench
use rust_ml::model::linear_regression::LinearRegression;
use rust_ml::model::ml_model::RegressionModel;
use rust_ml::optimization::gradient_descent::GradientDescent;
use rust_ml::optimization::optimizer::Optimizer;
use ndarray::{Array1, Array2};
fn main() {
// Create sample data
let x_train = Array2::from_shape_vec((3, 2), vec![1.0, 2.0, 2.0, 3.0, 3.0, 4.0]).unwrap();
let y_train = Array1::from_vec(vec![6.0, 9.0, 12.0]);
// Initialize model and optimizer
let mut model = LinearRegression::new(2); // 2 features
let mut optimizer = GradientDescent::new(0.01, 1000); // learning rate and iterations
// Train model
optimizer.fit(&mut model, &x_train, &y_train).expect("Training failed");
// Make predictions
let x_test = Array2::from_shape_vec((1, 2), vec![4.0, 5.0]).unwrap();
let predictions = model.predict(&x_test);
println!("Prediction: {:?}", predictions);
}
The library provides profiling capabilities to measure model performance:
use rust_ml::bench::regression_profiler::RegressionProfiler;
use rust_ml::bench::profiler::Profiler;
use rust_ml::model::linear_regression::LinearRegression;
use rust_ml::optimization::gradient_descent::GradientDescent;
fn main() {
// Initialize data, model and optimizer
// [...]
// Create profiler
let profiler = RegressionProfiler::<LinearRegression, GradientDescent, _, _>::new();
// Profile training
let (train_metrics, eval_metrics) = profiler
.profile_training(&mut model, &mut optimizer, &x_train, &y_train)
.expect("Profiling failed");
println!("Training metrics: {:?}", train_metrics);
println!("Evaluation metrics: {:?}", eval_metrics);
}
The project relies on several high-quality Rust crates:
thiserror: For ergonomic error handlingndarray: For efficient numerical computationsndarray-rand: For random matrix generationpolars: For data manipulationrand: For random number generationapprox: For approximate floating-point comparisonsContributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)