mirror of
https://github.com/Magnus167/rustframe.git
synced 2025-11-19 14:06:11 +00:00
Compare commits
13 Commits
f4ebd78234
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| c53693fa7b | |||
| 109d39b248 | |||
|
|
18ad6c689a | ||
| 1fead78b69 | |||
|
|
6fb32e743c | ||
| 2cb4e46217 | |||
|
|
a53ba63f30 | ||
|
|
dae60ea1bd | ||
|
|
755dee58e7 | ||
|
|
9e6e22fc37 | ||
|
|
b687fd4e6b | ||
|
|
68a01ab528 | ||
|
|
23a01dab07 |
7
.github/htmldocs/index.html
vendored
7
.github/htmldocs/index.html
vendored
@@ -59,8 +59,12 @@
|
|||||||
<hr style="border: 1px solid #d4d4d4; margin: 20px 0;">
|
<hr style="border: 1px solid #d4d4d4; margin: 20px 0;">
|
||||||
<p>
|
<p>
|
||||||
|
|
||||||
|
🐙 <a href="https://github.com/Magnus167/rustframe">GitHub</a>
|
||||||
|
<br><br>
|
||||||
|
|
||||||
📖 <a href="https://magnus167.github.io/rustframe/user-guide">User Guide</a>
|
📖 <a href="https://magnus167.github.io/rustframe/user-guide">User Guide</a>
|
||||||
<br><br>
|
<br><br>
|
||||||
|
|
||||||
|
|
||||||
📚 <a href="https://magnus167.github.io/rustframe/docs">Docs</a> |
|
📚 <a href="https://magnus167.github.io/rustframe/docs">Docs</a> |
|
||||||
📊 <a href="https://magnus167.github.io/rustframe/benchmark-report/">Benchmarks</a>
|
📊 <a href="https://magnus167.github.io/rustframe/benchmark-report/">Benchmarks</a>
|
||||||
@@ -69,8 +73,7 @@
|
|||||||
🦀 <a href="https://crates.io/crates/rustframe">Crates.io</a> |
|
🦀 <a href="https://crates.io/crates/rustframe">Crates.io</a> |
|
||||||
🔖 <a href="https://docs.rs/rustframe/latest/rustframe/">docs.rs</a>
|
🔖 <a href="https://docs.rs/rustframe/latest/rustframe/">docs.rs</a>
|
||||||
<br><br>
|
<br><br>
|
||||||
🐙 <a href="https://github.com/Magnus167/rustframe">GitHub</a> |
|
<!-- 🌐 <a href="https://gitea.nulltech.uk/Magnus167/rustframe">Gitea mirror</a> -->
|
||||||
🌐 <a href="https://gitea.nulltech.uk/Magnus167/rustframe">Gitea mirror</a>
|
|
||||||
</p>
|
</p>
|
||||||
</main>
|
</main>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rustframe"
|
name = "rustframe"
|
||||||
authors = ["Palash Tyagi (https://github.com/Magnus167)"]
|
authors = ["Palash Tyagi (https://github.com/Magnus167)"]
|
||||||
version = "0.0.1-a.20250716"
|
version = "0.0.1-a.20250805"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "GPL-3.0-or-later"
|
license = "GPL-3.0-or-later"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
description = "A simple dataframe library"
|
description = "A simple dataframe and math toolkit"
|
||||||
|
documentation = "https://magnus167.github.io/rustframe/"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "rustframe"
|
name = "rustframe"
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
# rustframe
|
# rustframe
|
||||||
|
|
||||||
📚 [Docs](https://magnus167.github.io/rustframe/) | 🐙 [GitHub](https://github.com/Magnus167/rustframe) | 🌐 [Gitea mirror](https://gitea.nulltech.uk/Magnus167/rustframe) | 🦀 [Crates.io](https://crates.io/crates/rustframe) | 🔖 [docs.rs](https://docs.rs/rustframe/latest/rustframe/)
|
🐙 [GitHub](https://github.com/Magnus167/rustframe) | 📚 [Docs](https://magnus167.github.io/rustframe/) | 📖 [User Guide](https://magnus167.github.io/rustframe/user-guide/) | 🦀 [Crates.io](https://crates.io/crates/rustframe) | 🔖 [docs.rs](https://docs.rs/rustframe/latest/rustframe/)
|
||||||
|
|
||||||
<!-- [](https://github.com/Magnus167/rustframe) -->
|
<!-- [](https://github.com/Magnus167/rustframe) -->
|
||||||
|
|
||||||
[](https://codecov.io/gh/Magnus167/rustframe)
|
[](https://codecov.io/gh/Magnus167/rustframe)
|
||||||
[](https://magnus167.github.io/rustframe/docs/tarpaulin-report.html)
|
[](https://magnus167.github.io/rustframe/docs/tarpaulin-report.html)
|
||||||
|
[](https://gitea.nulltech.uk/Magnus167/rustframe)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -8,22 +8,55 @@ some basic inferential tests.
|
|||||||
|
|
||||||
```rust
|
```rust
|
||||||
# extern crate rustframe;
|
# extern crate rustframe;
|
||||||
use rustframe::compute::stats::{mean, mean_vertical, stddev, median};
|
use rustframe::compute::stats::{mean, mean_horizontal, mean_vertical, stddev, median, population_variance, percentile};
|
||||||
use rustframe::matrix::Matrix;
|
use rustframe::matrix::Matrix;
|
||||||
|
|
||||||
let m = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
let m = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||||
assert_eq!(mean(&m), 2.5);
|
assert_eq!(mean(&m), 2.5);
|
||||||
assert_eq!(stddev(&m), 1.118033988749895);
|
assert_eq!(stddev(&m), 1.118033988749895);
|
||||||
assert_eq!(median(&m), 2.5);
|
assert_eq!(median(&m), 2.5);
|
||||||
|
assert_eq!(population_variance(&m), 1.25);
|
||||||
|
assert_eq!(percentile(&m, 50.0), 3.0);
|
||||||
// column averages returned as 1 x n matrix
|
// column averages returned as 1 x n matrix
|
||||||
|
let row_means = mean_horizontal(&m);
|
||||||
|
assert_eq!(row_means.data(), &[2.0, 3.0]);
|
||||||
let col_means = mean_vertical(&m);
|
let col_means = mean_vertical(&m);
|
||||||
assert_eq!(col_means.data(), &[1.5, 3.5]);
|
assert_eq!(col_means.data(), & [1.5, 3.5]);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Axis-specific Operations
|
||||||
|
|
||||||
|
Operations can be applied along specific axes (rows or columns):
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# extern crate rustframe;
|
||||||
|
use rustframe::compute::stats::{mean_vertical, mean_horizontal, stddev_vertical, stddev_horizontal};
|
||||||
|
use rustframe::matrix::Matrix;
|
||||||
|
|
||||||
|
// 3x2 matrix
|
||||||
|
let m = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], 3, 2);
|
||||||
|
|
||||||
|
// Mean along columns (vertical) - returns 1 x cols matrix
|
||||||
|
let col_means = mean_vertical(&m);
|
||||||
|
assert_eq!(col_means.shape(), (1, 2));
|
||||||
|
assert_eq!(col_means.data(), &[3.0, 4.0]); // [(1+3+5)/3, (2+4+6)/3]
|
||||||
|
|
||||||
|
// Mean along rows (horizontal) - returns rows x 1 matrix
|
||||||
|
let row_means = mean_horizontal(&m);
|
||||||
|
assert_eq!(row_means.shape(), (3, 1));
|
||||||
|
assert_eq!(row_means.data(), &[1.5, 3.5, 5.5]); // [(1+2)/2, (3+4)/2, (5+6)/2]
|
||||||
|
|
||||||
|
// Standard deviation along columns
|
||||||
|
let col_stddev = stddev_vertical(&m);
|
||||||
|
assert_eq!(col_stddev.shape(), (1, 2));
|
||||||
|
|
||||||
|
// Standard deviation along rows
|
||||||
|
let row_stddev = stddev_horizontal(&m);
|
||||||
|
assert_eq!(row_stddev.shape(), (3, 1));
|
||||||
```
|
```
|
||||||
|
|
||||||
## Correlation
|
## Correlation
|
||||||
|
|
||||||
Correlation functions help measure linear relationships between datasets.
|
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
# extern crate rustframe;
|
# extern crate rustframe;
|
||||||
use rustframe::compute::stats::{pearson, covariance};
|
use rustframe::compute::stats::{pearson, covariance};
|
||||||
@@ -37,6 +70,77 @@ assert!((corr - 1.0).abs() < 1e-8);
|
|||||||
assert!((cov - 2.5).abs() < 1e-8);
|
assert!((cov - 2.5).abs() < 1e-8);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Covariance
|
||||||
|
|
||||||
|
### `covariance`
|
||||||
|
|
||||||
|
Computes the population covariance between two equally sized matrices by flattening
|
||||||
|
their values.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# extern crate rustframe;
|
||||||
|
use rustframe::compute::stats::covariance;
|
||||||
|
use rustframe::matrix::Matrix;
|
||||||
|
|
||||||
|
let x = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||||
|
let y = Matrix::from_vec(vec![2.0, 4.0, 6.0, 8.0], 2, 2);
|
||||||
|
let cov = covariance(&x, &y);
|
||||||
|
assert!((cov - 2.5).abs() < 1e-8);
|
||||||
|
```
|
||||||
|
|
||||||
|
### `covariance_vertical`
|
||||||
|
|
||||||
|
Evaluates covariance between columns (i.e. across rows) and returns a matrix of
|
||||||
|
column pair covariances.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# extern crate rustframe;
|
||||||
|
use rustframe::compute::stats::covariance_vertical;
|
||||||
|
use rustframe::matrix::Matrix;
|
||||||
|
|
||||||
|
let m = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||||
|
let cov = covariance_vertical(&m);
|
||||||
|
assert_eq!(cov.shape(), (2, 2));
|
||||||
|
assert!(cov.data().iter().all(|&v| (v - 1.0).abs() < 1e-8));
|
||||||
|
```
|
||||||
|
|
||||||
|
### `covariance_horizontal`
|
||||||
|
|
||||||
|
Computes covariance between rows (i.e. across columns) returning a matrix that
|
||||||
|
describes how each pair of rows varies together.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# extern crate rustframe;
|
||||||
|
use rustframe::compute::stats::covariance_horizontal;
|
||||||
|
use rustframe::matrix::Matrix;
|
||||||
|
|
||||||
|
let m = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||||
|
let cov = covariance_horizontal(&m);
|
||||||
|
assert_eq!(cov.shape(), (2, 2));
|
||||||
|
assert!(cov.data().iter().all(|&v| (v - 0.25).abs() < 1e-8));
|
||||||
|
```
|
||||||
|
|
||||||
|
### `covariance_matrix`
|
||||||
|
|
||||||
|
Builds a covariance matrix either between columns (`Axis::Col`) or rows
|
||||||
|
(`Axis::Row`). Each entry represents how two series co-vary.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# extern crate rustframe;
|
||||||
|
use rustframe::compute::stats::covariance_matrix;
|
||||||
|
use rustframe::matrix::{Axis, Matrix};
|
||||||
|
|
||||||
|
let data = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||||
|
|
||||||
|
// Covariance between columns
|
||||||
|
let cov_cols = covariance_matrix(&data, Axis::Col);
|
||||||
|
assert!((cov_cols.get(0, 0) - 2.0).abs() < 1e-8);
|
||||||
|
|
||||||
|
// Covariance between rows
|
||||||
|
let cov_rows = covariance_matrix(&data, Axis::Row);
|
||||||
|
assert!((cov_rows.get(0, 1) + 0.5).abs() < 1e-8);
|
||||||
|
```
|
||||||
|
|
||||||
## Distributions
|
## Distributions
|
||||||
|
|
||||||
Probability distribution helpers are available for common PDFs and CDFs.
|
Probability distribution helpers are available for common PDFs and CDFs.
|
||||||
@@ -51,5 +155,68 @@ let pdf = normal_pdf(x, 0.0, 1.0);
|
|||||||
assert_eq!(pdf.data().len(), 2);
|
assert_eq!(pdf.data().len(), 2);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Additional Distributions
|
||||||
|
|
||||||
|
Rustframe provides several other probability distributions:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# extern crate rustframe;
|
||||||
|
use rustframe::compute::stats::distributions::{normal_cdf, binomial_pmf, binomial_cdf, poisson_pmf};
|
||||||
|
use rustframe::matrix::Matrix;
|
||||||
|
|
||||||
|
// Normal distribution CDF
|
||||||
|
let x = Matrix::from_vec(vec![0.0, 1.0], 1, 2);
|
||||||
|
let cdf = normal_cdf(x, 0.0, 1.0);
|
||||||
|
assert_eq!(cdf.data().len(), 2);
|
||||||
|
|
||||||
|
// Binomial distribution PMF
|
||||||
|
// Probability of k successes in n trials with probability p
|
||||||
|
let k = Matrix::from_vec(vec![0_u64, 1, 2, 3], 1, 4);
|
||||||
|
let pmf = binomial_pmf(3, k.clone(), 0.5);
|
||||||
|
assert_eq!(pmf.data().len(), 4);
|
||||||
|
|
||||||
|
// Binomial distribution CDF
|
||||||
|
let cdf = binomial_cdf(3, k, 0.5);
|
||||||
|
assert_eq!(cdf.data().len(), 4);
|
||||||
|
|
||||||
|
// Poisson distribution PMF
|
||||||
|
// Probability of k events with rate parameter lambda
|
||||||
|
let k = Matrix::from_vec(vec![0_u64, 1, 2], 1, 3);
|
||||||
|
let pmf = poisson_pmf(2.0, k);
|
||||||
|
assert_eq!(pmf.data().len(), 3);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Inferential Statistics
|
||||||
|
|
||||||
|
Rustframe provides several inferential statistical tests:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# extern crate rustframe;
|
||||||
|
use rustframe::matrix::Matrix;
|
||||||
|
use rustframe::compute::stats::inferential::{t_test, chi2_test, anova};
|
||||||
|
|
||||||
|
// Two-sample t-test
|
||||||
|
let sample1 = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], 1, 5);
|
||||||
|
let sample2 = Matrix::from_vec(vec![6.0, 7.0, 8.0, 9.0, 10.0], 1, 5);
|
||||||
|
let (t_statistic, p_value) = t_test(&sample1, &sample2);
|
||||||
|
assert!((t_statistic + 5.0).abs() < 1e-5);
|
||||||
|
assert!(p_value > 0.0 && p_value < 1.0);
|
||||||
|
|
||||||
|
// Chi-square test of independence
|
||||||
|
let observed = Matrix::from_vec(vec![12.0, 5.0, 8.0, 10.0], 2, 2);
|
||||||
|
let (chi2_statistic, p_value) = chi2_test(&observed);
|
||||||
|
assert!(chi2_statistic > 0.0);
|
||||||
|
assert!(p_value > 0.0 && p_value < 1.0);
|
||||||
|
|
||||||
|
// One-way ANOVA
|
||||||
|
let group1 = Matrix::from_vec(vec![1.0, 2.0, 3.0], 1, 3);
|
||||||
|
let group2 = Matrix::from_vec(vec![2.0, 3.0, 4.0], 1, 3);
|
||||||
|
let group3 = Matrix::from_vec(vec![3.0, 4.0, 5.0], 1, 3);
|
||||||
|
let groups = vec![&group1, &group2, &group3];
|
||||||
|
let (f_statistic, p_value) = anova(groups);
|
||||||
|
assert!(f_statistic > 0.0);
|
||||||
|
assert!(p_value > 0.0 && p_value < 1.0);
|
||||||
|
```
|
||||||
|
|
||||||
With the basics covered, explore predictive models in the
|
With the basics covered, explore predictive models in the
|
||||||
[machine learning](./machine-learning.md) chapter.
|
[machine learning](./machine-learning.md) chapter.
|
||||||
|
|||||||
@@ -73,5 +73,85 @@ assert_eq!(frame.sum_vertical(), vec![3.0, 7.0]);
|
|||||||
assert_eq!(frame.sum_horizontal(), vec![4.0, 6.0]);
|
assert_eq!(frame.sum_horizontal(), vec![4.0, 6.0]);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Matrix Operations
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# extern crate rustframe;
|
||||||
|
use rustframe::matrix::Matrix;
|
||||||
|
|
||||||
|
let data1 = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||||
|
let data2 = Matrix::from_vec(vec![5.0, 6.0, 7.0, 8.0], 2, 2);
|
||||||
|
|
||||||
|
let sum = data1.clone() + data2.clone();
|
||||||
|
assert_eq!(sum.data(), vec![6.0, 8.0, 10.0, 12.0]);
|
||||||
|
|
||||||
|
let product = data1.clone() * data2.clone();
|
||||||
|
assert_eq!(product.data(), vec![5.0, 12.0, 21.0, 32.0]);
|
||||||
|
|
||||||
|
let scalar_product = data1.clone() * 2.0;
|
||||||
|
assert_eq!(scalar_product.data(), vec![2.0, 4.0, 6.0, 8.0]);
|
||||||
|
|
||||||
|
let equals = data1 == data1.clone();
|
||||||
|
assert_eq!(equals, true);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Advanced Matrix Operations
|
||||||
|
|
||||||
|
Matrices support a variety of advanced operations:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# extern crate rustframe;
|
||||||
|
use rustframe::matrix::{Matrix, SeriesOps};
|
||||||
|
|
||||||
|
// Matrix multiplication (dot product)
|
||||||
|
let a = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||||
|
let b = Matrix::from_vec(vec![5.0, 6.0, 7.0, 8.0], 2, 2);
|
||||||
|
let product = a.matrix_mul(&b);
|
||||||
|
assert_eq!(product.data(), vec![23.0, 34.0, 31.0, 46.0]);
|
||||||
|
|
||||||
|
// Transpose
|
||||||
|
let m = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||||
|
let transposed = m.transpose();
|
||||||
|
assert_eq!(transposed.data(), vec![1.0, 3.0, 2.0, 4.0]);
|
||||||
|
|
||||||
|
// Map function over all elements
|
||||||
|
let m = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||||
|
let squared = m.map(|x| x * x);
|
||||||
|
assert_eq!(squared.data(), vec![1.0, 4.0, 9.0, 16.0]);
|
||||||
|
|
||||||
|
// Zip two matrices with a function
|
||||||
|
let a = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0], 2, 2);
|
||||||
|
let b = Matrix::from_vec(vec![5.0, 6.0, 7.0, 8.0], 2, 2);
|
||||||
|
let zipped = a.zip(&b, |x, y| x + y);
|
||||||
|
assert_eq!(zipped.data(), vec![6.0, 8.0, 10.0, 12.0]);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Matrix Reductions
|
||||||
|
|
||||||
|
Matrices support various reduction operations:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# extern crate rustframe;
|
||||||
|
use rustframe::matrix::{Matrix, SeriesOps};
|
||||||
|
|
||||||
|
let m = Matrix::from_rows_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], 3, 2);
|
||||||
|
|
||||||
|
// Sum along columns (vertical)
|
||||||
|
let col_sums = m.sum_vertical();
|
||||||
|
assert_eq!(col_sums, vec![9.0, 12.0]); // [1+3+5, 2+4+6]
|
||||||
|
|
||||||
|
// Sum along rows (horizontal)
|
||||||
|
let row_sums = m.sum_horizontal();
|
||||||
|
assert_eq!(row_sums, vec![3.0, 7.0, 11.0]); // [1+2, 3+4, 5+6]
|
||||||
|
|
||||||
|
// Cumulative sum along columns
|
||||||
|
let col_cumsum = m.cumsum_vertical();
|
||||||
|
assert_eq!(col_cumsum.data(), vec![1.0, 4.0, 9.0, 2.0, 6.0, 12.0]);
|
||||||
|
|
||||||
|
// Cumulative sum along rows
|
||||||
|
let row_cumsum = m.cumsum_horizontal();
|
||||||
|
assert_eq!(row_cumsum.data(), vec![1.0, 3.0, 5.0, 3.0, 7.0, 11.0]);
|
||||||
|
```
|
||||||
|
|
||||||
With the basics covered, continue to the [compute features](./compute.md)
|
With the basics covered, continue to the [compute features](./compute.md)
|
||||||
chapter for statistics and analytics.
|
chapter for statistics and analytics.
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
# Introduction
|
# Introduction
|
||||||
|
|
||||||
|
🐙 [GitHub](https://github.com/Magnus167/rustframe) | 📚 [Docs](https://magnus167.github.io/rustframe/) | 📖 [User Guide](https://magnus167.github.io/rustframe/user-guide/) | 🦀 [Crates.io](https://crates.io/crates/rustframe) | 🔖 [docs.rs](https://docs.rs/rustframe/latest/rustframe/)
|
||||||
|
|
||||||
Welcome to the **Rustframe User Guide**. Rustframe is a lightweight dataframe
|
Welcome to the **Rustframe User Guide**. Rustframe is a lightweight dataframe
|
||||||
and math toolkit for Rust written in 100% safe Rust. It focuses on keeping the
|
and math toolkit for Rust written in 100% safe Rust. It focuses on keeping the
|
||||||
API approachable while offering handy features for small analytical or
|
API approachable while offering handy features for small analytical or
|
||||||
|
|||||||
@@ -41,9 +41,6 @@ let new_point = Matrix::from_vec(vec![0.0, 0.0], 1, 2);
|
|||||||
let cluster = model.predict(&new_point)[0];
|
let cluster = model.predict(&new_point)[0];
|
||||||
```
|
```
|
||||||
|
|
||||||
For helper functions and upcoming modules, visit the
|
|
||||||
[utilities](./utilities.md) section.
|
|
||||||
|
|
||||||
## Logistic Regression
|
## Logistic Regression
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
@@ -72,5 +69,214 @@ let transformed = pca.transform(&data);
|
|||||||
assert_eq!(transformed.cols(), 1);
|
assert_eq!(transformed.cols(), 1);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Gaussian Naive Bayes
|
||||||
|
|
||||||
|
Gaussian Naive Bayes classifier for continuous features:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# extern crate rustframe;
|
||||||
|
use rustframe::compute::models::gaussian_nb::GaussianNB;
|
||||||
|
use rustframe::matrix::Matrix;
|
||||||
|
|
||||||
|
// Training data with 2 features
|
||||||
|
let x = Matrix::from_rows_vec(vec![
|
||||||
|
1.0, 2.0,
|
||||||
|
2.0, 3.0,
|
||||||
|
3.0, 4.0,
|
||||||
|
4.0, 5.0
|
||||||
|
], 4, 2);
|
||||||
|
|
||||||
|
// Class labels (0 or 1)
|
||||||
|
let y = Matrix::from_vec(vec![0.0, 0.0, 1.0, 1.0], 4, 1);
|
||||||
|
|
||||||
|
// Train the model
|
||||||
|
let mut model = GaussianNB::new(1e-9, true);
|
||||||
|
model.fit(&x, &y);
|
||||||
|
|
||||||
|
// Make predictions
|
||||||
|
let predictions = model.predict(&x);
|
||||||
|
assert_eq!(predictions.rows(), 4);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Dense Neural Networks
|
||||||
|
|
||||||
|
Simple fully connected neural network:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# extern crate rustframe;
|
||||||
|
use rustframe::compute::models::dense_nn::{DenseNN, DenseNNConfig, ActivationKind, InitializerKind, LossKind};
|
||||||
|
use rustframe::matrix::Matrix;
|
||||||
|
|
||||||
|
// Training data with 2 features
|
||||||
|
let x = Matrix::from_rows_vec(vec![
|
||||||
|
0.0, 0.0,
|
||||||
|
0.0, 1.0,
|
||||||
|
1.0, 0.0,
|
||||||
|
1.0, 1.0
|
||||||
|
], 4, 2);
|
||||||
|
|
||||||
|
// XOR target outputs
|
||||||
|
let y = Matrix::from_vec(vec![0.0, 1.0, 1.0, 0.0], 4, 1);
|
||||||
|
|
||||||
|
// Create a neural network with 2 hidden layers
|
||||||
|
let config = DenseNNConfig {
|
||||||
|
input_size: 2,
|
||||||
|
hidden_layers: vec![4, 4],
|
||||||
|
output_size: 1,
|
||||||
|
activations: vec![ActivationKind::Sigmoid, ActivationKind::Sigmoid, ActivationKind::Sigmoid],
|
||||||
|
initializer: InitializerKind::Uniform(0.5),
|
||||||
|
loss: LossKind::MSE,
|
||||||
|
learning_rate: 0.1,
|
||||||
|
epochs: 1000,
|
||||||
|
};
|
||||||
|
let mut model = DenseNN::new(config);
|
||||||
|
|
||||||
|
// Train the model
|
||||||
|
model.train(&x, &y);
|
||||||
|
|
||||||
|
// Make predictions
|
||||||
|
let predictions = model.predict(&x);
|
||||||
|
assert_eq!(predictions.rows(), 4);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Real-world Examples
|
||||||
|
|
||||||
|
### Housing Price Prediction
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# extern crate rustframe;
|
||||||
|
use rustframe::compute::models::linreg::LinReg;
|
||||||
|
use rustframe::matrix::Matrix;
|
||||||
|
|
||||||
|
// Features: square feet and bedrooms
|
||||||
|
let features = Matrix::from_rows_vec(vec![
|
||||||
|
2100.0, 3.0,
|
||||||
|
1600.0, 2.0,
|
||||||
|
2400.0, 4.0,
|
||||||
|
1400.0, 2.0,
|
||||||
|
], 4, 2);
|
||||||
|
|
||||||
|
// Sale prices
|
||||||
|
let target = Matrix::from_vec(vec![400_000.0, 330_000.0, 369_000.0, 232_000.0], 4, 1);
|
||||||
|
|
||||||
|
let mut model = LinReg::new(2);
|
||||||
|
model.fit(&features, &target, 1e-8, 10_000);
|
||||||
|
|
||||||
|
// Predict price of a new home
|
||||||
|
let new_home = Matrix::from_vec(vec![2000.0, 3.0], 1, 2);
|
||||||
|
let predicted_price = model.predict(&new_home);
|
||||||
|
println!("Predicted price: ${}", predicted_price.data()[0]);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Spam Detection
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# extern crate rustframe;
|
||||||
|
use rustframe::compute::models::logreg::LogReg;
|
||||||
|
use rustframe::matrix::Matrix;
|
||||||
|
|
||||||
|
// 20 e-mails × 5 features = 100 numbers (row-major, spam first)
|
||||||
|
let x = Matrix::from_rows_vec(
|
||||||
|
vec![
|
||||||
|
// ─────────── spam examples ───────────
|
||||||
|
2.0, 1.0, 1.0, 1.0, 1.0, // "You win a FREE offer - click for money-back bonus!"
|
||||||
|
1.0, 0.0, 1.0, 1.0, 0.0, // "FREE offer! Click now!"
|
||||||
|
0.0, 2.0, 0.0, 1.0, 1.0, // "Win win win - money inside, click…"
|
||||||
|
1.0, 1.0, 0.0, 0.0, 1.0, // "Limited offer to win easy money…"
|
||||||
|
1.0, 0.0, 1.0, 0.0, 1.0, // ...
|
||||||
|
0.0, 1.0, 1.0, 1.0, 0.0, // ...
|
||||||
|
2.0, 0.0, 0.0, 1.0, 1.0, // ...
|
||||||
|
0.0, 1.0, 1.0, 0.0, 1.0, // ...
|
||||||
|
1.0, 1.0, 1.0, 1.0, 0.0, // ...
|
||||||
|
1.0, 0.0, 0.0, 1.0, 1.0, // ...
|
||||||
|
// ─────────── ham examples ───────────
|
||||||
|
0.0, 0.0, 0.0, 0.0, 0.0, // "See you at the meeting tomorrow."
|
||||||
|
0.0, 0.0, 0.0, 1.0, 0.0, // "Here's the Zoom click-link."
|
||||||
|
0.0, 0.0, 0.0, 0.0, 1.0, // "Expense report: money attached."
|
||||||
|
0.0, 0.0, 0.0, 1.0, 1.0, // ...
|
||||||
|
0.0, 1.0, 0.0, 0.0, 0.0, // "Did we win the bid?"
|
||||||
|
0.0, 0.0, 0.0, 0.0, 0.0, // ...
|
||||||
|
0.0, 0.0, 0.0, 1.0, 0.0, // ...
|
||||||
|
1.0, 0.0, 0.0, 0.0, 0.0, // "Special offer for staff lunch."
|
||||||
|
0.0, 0.0, 0.0, 0.0, 0.0, // ...
|
||||||
|
0.0, 0.0, 0.0, 1.0, 0.0,
|
||||||
|
],
|
||||||
|
20,
|
||||||
|
5,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Labels: 1 = spam, 0 = ham
|
||||||
|
let y = Matrix::from_vec(
|
||||||
|
vec![
|
||||||
|
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, // 10 spam
|
||||||
|
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, // 10 ham
|
||||||
|
],
|
||||||
|
20,
|
||||||
|
1,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Train
|
||||||
|
let mut model = LogReg::new(5);
|
||||||
|
model.fit(&x, &y, 0.01, 5000);
|
||||||
|
|
||||||
|
// Predict
|
||||||
|
// e.g. "free money offer"
|
||||||
|
let email_data = vec![1.0, 0.0, 1.0, 0.0, 1.0];
|
||||||
|
let email = Matrix::from_vec(email_data, 1, 5);
|
||||||
|
let prob_spam = model.predict_proba(&email);
|
||||||
|
println!("Probability of spam: {:.4}", prob_spam.data()[0]);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Iris Flower Classification
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# extern crate rustframe;
|
||||||
|
use rustframe::compute::models::gaussian_nb::GaussianNB;
|
||||||
|
use rustframe::matrix::Matrix;
|
||||||
|
|
||||||
|
// Features: sepal length and petal length
|
||||||
|
let x = Matrix::from_rows_vec(vec![
|
||||||
|
5.1, 1.4, // setosa
|
||||||
|
4.9, 1.4, // setosa
|
||||||
|
6.2, 4.5, // versicolor
|
||||||
|
5.9, 5.1, // virginica
|
||||||
|
], 4, 2);
|
||||||
|
|
||||||
|
let y = Matrix::from_vec(vec![0.0, 0.0, 1.0, 2.0], 4, 1);
|
||||||
|
let names = vec!["setosa", "versicolor", "virginica"];
|
||||||
|
|
||||||
|
let mut model = GaussianNB::new(1e-9, true);
|
||||||
|
model.fit(&x, &y);
|
||||||
|
|
||||||
|
let sample = Matrix::from_vec(vec![5.0, 1.5], 1, 2);
|
||||||
|
let predicted_class = model.predict(&sample);
|
||||||
|
let class_name = names[predicted_class.data()[0] as usize];
|
||||||
|
println!("Predicted class: {} ({:?})", class_name, predicted_class.data()[0]);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Customer Segmentation
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# extern crate rustframe;
|
||||||
|
use rustframe::compute::models::k_means::KMeans;
|
||||||
|
use rustframe::matrix::Matrix;
|
||||||
|
|
||||||
|
// Each row: [age, annual_income]
|
||||||
|
let customers = Matrix::from_rows_vec(
|
||||||
|
vec![
|
||||||
|
25.0, 40_000.0, 34.0, 52_000.0, 58.0, 95_000.0, 45.0, 70_000.0,
|
||||||
|
],
|
||||||
|
4,
|
||||||
|
2,
|
||||||
|
);
|
||||||
|
|
||||||
|
let (model, labels) = KMeans::fit(&customers, 2, 20, 1e-4);
|
||||||
|
|
||||||
|
let new_customer = Matrix::from_vec(vec![30.0, 50_000.0], 1, 2);
|
||||||
|
let cluster = model.predict(&new_customer)[0];
|
||||||
|
println!("New customer belongs to cluster: {}", cluster);
|
||||||
|
println!("Cluster labels: {:?}", labels);
|
||||||
|
```
|
||||||
|
|
||||||
For helper functions and upcoming modules, visit the
|
For helper functions and upcoming modules, visit the
|
||||||
[utilities](./utilities.md) section.
|
[utilities](./utilities.md) section.
|
||||||
|
|||||||
@@ -35,6 +35,25 @@ let v2 = rng.next_u64();
|
|||||||
assert_ne!(v1, v2);
|
assert_ne!(v1, v2);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Stats Functions
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# extern crate rustframe;
|
||||||
|
use rustframe::matrix::Matrix;
|
||||||
|
use rustframe::compute::stats::descriptive::{mean, median, stddev};
|
||||||
|
|
||||||
|
let data = Matrix::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0], 1, 5);
|
||||||
|
|
||||||
|
let mean_value = mean(&data);
|
||||||
|
assert_eq!(mean_value, 3.0);
|
||||||
|
|
||||||
|
let median_value = median(&data);
|
||||||
|
assert_eq!(median_value, 3.0);
|
||||||
|
|
||||||
|
let std_value = stddev(&data);
|
||||||
|
assert_eq!(std_value, 2.0_f64.sqrt());
|
||||||
|
```
|
||||||
|
|
||||||
Upcoming utilities will cover:
|
Upcoming utilities will cover:
|
||||||
|
|
||||||
- Data import/export helpers
|
- Data import/export helpers
|
||||||
|
|||||||
Reference in New Issue
Block a user