Skip to content

Commit db3cade

Browse files
aswin-1111relf
andauthored
[Feature] Add Least Angle Regression (#421)
* Implement Least Angle Regression * cargo fmt * cargo clippy * cargo clippy 2 * improve documentation * remove linnerud dependency * add example * cargo clippy in doc test * Apply suggestions from code review Co-authored-by: Rémi Lafage <[email protected]> * close bracket * remove unwanted stride handling * correct doc example --------- Co-authored-by: Rémi Lafage <[email protected]>
1 parent 5afd26c commit db3cade

File tree

7 files changed

+965
-0
lines changed

7 files changed

+965
-0
lines changed

algorithms/linfa-lars/Cargo.toml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[package]
2+
name = "linfa-lars"
3+
version = "0.8.0"
4+
authors = ["Aswin <[email protected]>"]
5+
description = "Least Angle Regression methods"
6+
edition = "2024"
7+
license = "MIT OR Apache-2.0"
8+
9+
repository = "https://github.com/rust-ml/linfa"
10+
readme = "README.md"
11+
12+
keywords = ["machine-learning", "linfa", "ai", "ml", "linear"]
13+
categories = ["algorithms", "mathematics", "science"]
14+
15+
[features]
16+
default = []
17+
serde = ["serde_crate", "ndarray/serde", "linfa/serde"]
18+
blas = ["ndarray-linalg", "linfa/ndarray-linalg"]
19+
20+
[dependencies.serde_crate]
21+
package = "serde"
22+
optional = true
23+
version = "1.0"
24+
default-features = false
25+
features = ["std", "derive"]
26+
27+
[dependencies]
28+
linfa = { version = "0.8.0", path = "../.." }
29+
linfa-linalg = { version = "0.2", default-features = false }
30+
linfa-preprocessing = { version = "0.8.0" , path = "../linfa-preprocessing"}
31+
ndarray = { version = "0.16", features = ["approx"] }
32+
ndarray-linalg = { version = "0.17", optional = true }
33+
ndarray-stats = "0.6"
34+
thiserror = "2.0"
35+
36+
[dev-dependencies]
37+
linfa-datasets = { version = "0.8.0", path = "../../datasets", features = [
38+
"diabetes",
39+
] }
40+
approx = "0.5"
41+
ndarray-rand = "0.15"
42+
rand_xoshiro = "0.6"

algorithms/linfa-lars/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Least Angle Regression
2+
3+
`linfa-lars` aims to provide pure Rust implementation of least-angle regression algorithm.
4+
5+
## The Big Picture
6+
7+
`linfa-lars` is a crate in the [`linfa`](https://crates.io/crates/linfa) ecosystem, an effort to create a toolkit for classical Machine Learning implemented in pure Rust, akin to Python's `scikit-learn`.
8+
9+
## Current state
10+
11+
The `linfa-lars` crate currently provides an implementation of the Least-angle regression (LARS) algorithm
12+
13+
See also:
14+
* [Wikipedia on Least Angle Regression](https://en.wikipedia.org/wiki/Least-angle_regression)
15+
16+
17+
## BLAS/Lapack backend
18+
19+
See [this section](../../README.md#blaslapack-backend) to enable an external BLAS/LAPACK backend.
20+
21+
## Examples
22+
23+
There is an usage example in the `examples/` directory. To run, use:
24+
25+
```bash
26+
$ cargo run --example lars
27+
```
28+
29+
## License
30+
Dual-licensed to be compatible with the Rust project.
31+
32+
Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <http://opensource.org/licenses/MIT>, at your option. This file may not be copied, modified, or distributed except according to those terms.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use linfa::prelude::*;
2+
use linfa_lars::Lars;
3+
4+
fn main() {
5+
// load Diabetes dataset
6+
let (train, valid) = linfa_datasets::diabetes().split_with_ratio(0.90);
7+
8+
let model = Lars::params()
9+
.fit_intercept(true)
10+
.verbose(true)
11+
.fit(&train)
12+
.unwrap();
13+
14+
println!("hyperplane: {}", model.hyperplane());
15+
println!("intercept: {}", model.intercept());
16+
17+
// validate
18+
let y_est = model.predict(&valid);
19+
println!("predicted variance: {}", valid.r2(&y_est).unwrap());
20+
}

0 commit comments

Comments
 (0)