90 Commits

Author SHA1 Message Date
Lorenzo Mec-iS
a62c293244 Add another pairwise distance algorithm 2025-01-28 00:30:57 +00:00
Lorenzo Mec-iS
39f87aa5c2 add tests to fastpair 2025-01-28 00:20:29 +00:00
Lorenzo Mec-iS
8cc02cdd48 fix test 2025-01-27 23:43:42 +00:00
Lorenzo Mec-iS
d60ba63862 Merge branch 'main' of github.com:smartcorelib/smartcore into march-2023-improvements 2025-01-27 23:34:45 +00:00
Lorenzo
5dd5c2f0d0 Merge branch 'development' into march-2023-improvements 2025-01-27 23:28:58 +00:00
Lorenzo
c8ec8fec00 Fix #245: return error for NaN in naive bayes (#246)
* Fix #245: return error for NaN in naive bayes
* Implement error handling for NaN values in NBayes predict:
* general behaviour has been kept unchanged according to original tests in `mod.rs`
* aka: error is returned only if all the predicted probabilities are NaN
* Add tests
* Add test with static values
* Add test for numerical stability with numpy
2025-01-27 23:17:55 +00:00
Lorenzo
3da433f757 Implement predict_proba for DecisionTreeClassifier (#287)
* Implement predict_proba for DecisionTreeClassifier
* Some automated fixes suggested by cargo clippy --fix
2025-01-20 18:50:00 +00:00
dependabot[bot]
4523ac73ff Update itertools requirement from 0.12.0 to 0.13.0 (#280)
Updates the requirements on [itertools](https://github.com/rust-itertools/itertools) to permit the latest version.
- [Changelog](https://github.com/rust-itertools/itertools/blob/master/CHANGELOG.md)
- [Commits](https://github.com/rust-itertools/itertools/compare/v0.12.0...v0.13.0)

---
updated-dependencies:
- dependency-name: itertools
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-11-25 11:47:23 -04:00
morenol
ba75f9ffad chore: fix clippy (#283)
* chore: fix clippy


Co-authored-by: Luis Moreno <morenol@users.noreply.github.com>
2024-11-25 11:34:29 -04:00
Lorenzo (Mec-iS)
074cfaf14f rustfmt 2023-03-24 12:06:54 +09:00
Lorenzo
393cf15534 Merge branch 'development' into march-2023-improvements 2023-03-24 12:05:06 +09:00
Lorenzo (Mec-iS)
80c406b37d Merge branch 'development' of github.com:smartcorelib/smartcore into march-2023-improvements 2023-03-21 17:38:35 +09:00
Lorenzo (Mec-iS)
0e1bf6ce7f Add ordered_pairs method to FastPair 2023-03-21 14:46:33 +09:00
Lorenzo (Mec-iS)
0c9c70f8d2 Merge 2022-11-09 12:05:17 +00:00
morenol
62de25b2ae Handle kernel serialization (#232)
* Handle kernel serialization
* Do not use typetag in WASM
* enable tests for serialization
* Update serde feature deps

Co-authored-by: Luis Moreno <morenol@users.noreply.github.com>
Co-authored-by: Lorenzo <tunedconsulting@gmail.com>
2022-11-08 11:29:56 -05:00
morenol
7d87451333 Fixes for release (#237)
* Fixes for release
* add new test
* Remove change applied in development branch
* Only add dependency for wasm32
* Update ci.yml

Co-authored-by: Luis Moreno <morenol@users.noreply.github.com>
Co-authored-by: Lorenzo <tunedconsulting@gmail.com>
2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
265fd558e7 make work cargo build --target wasm32-unknown-unknown 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
e25e2aea2b update CHANGELOG 2022-11-08 11:29:56 -05:00
Lorenzo
2f6dd1325e update comment 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
b0dece9476 use getrandom/js 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
c507d976be Update CHANGELOG 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
fa54d5ee86 Remove unused tests flags 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
459d558d48 minor fixes to doc 2022-11-08 11:29:56 -05:00
Lorenzo
1b7dda30a2 minor fix 2022-11-08 11:29:56 -05:00
Lorenzo
c1bd1df5f6 minor fix 2022-11-08 11:29:56 -05:00
Lorenzo
cf751f05aa minor fix 2022-11-08 11:29:56 -05:00
Lorenzo
63ed89aadd minor fix 2022-11-08 11:29:56 -05:00
Lorenzo
890e9d644c minor fix 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
af0a740394 Fix std_rand feature 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
616e38c282 cleanup 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
a449fdd4ea fmt 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
669f87f812 Use getrandom as default (for no-std feature) 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
6d529b34d2 Add static analyzer to doc 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
3ec9e4f0db Exclude datasets test for wasm/wasi 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
527477dea7 minor fixes 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
5b517c5048 minor fix 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
2df0795be9 Release 0.3 2022-11-08 11:29:56 -05:00
Lorenzo
0dc97a4e9b Create DEVELOPERS.md 2022-11-08 11:29:56 -05:00
Lorenzo
6c0fd37222 Update README.md 2022-11-08 11:29:56 -05:00
Lorenzo
d8d0fb6903 Update README.md 2022-11-08 11:29:56 -05:00
morenol
8d07efd921 Use Box in SVM and remove lifetimes (#228)
* Do not change external API
Authored-by: Luis Moreno <morenol@users.noreply.github.com>
2022-11-08 11:29:56 -05:00
morenol
ba27dd2a55 Fix CI (#227)
* Update ci.yml
Co-authored-by: Luis Moreno <morenol@users.noreply.github.com>
2022-11-08 11:29:56 -05:00
Lorenzo
ed9769f651 Implement CSV reader with new traits (#209) 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
b427e5d8b1 Improve options conditionals 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
fabe362755 Implement Display for NaiveBayes 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
ee6b6a53d6 cargo clippy 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
19f3a2fcc0 Fix signature of metrics tests 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
e09c4ba724 Add kernels' parameters to public interface 2022-11-08 11:29:56 -05:00
Lorenzo
6624732a65 Fix svr tests (#222) 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
1cbde3ba22 Refactor modules structure in src/svm 2022-11-08 11:29:56 -05:00
Lorenzo (Mec-iS)
551a6e34a5 clean up svm 2022-11-08 11:29:56 -05:00
Lorenzo
c45bab491a Support Wasi as target (#216)
* Improve features
* Add wasm32-wasi as a target
* Update .github/workflows/ci.yml
Co-authored-by: morenol <22335041+morenol@users.noreply.github.com>
2022-11-08 11:29:56 -05:00
Lorenzo
7f35dc54e4 Disambiguate distances. Implement Fastpair. (#220) 2022-11-08 11:29:56 -05:00
morenol
8f1a7dfd79 build: fix compilation without default features (#218)
* build: fix compilation with optional features
* Remove unused config from Cargo.toml
* Fix cache keys
Co-authored-by: Luis Moreno <morenol@users.noreply.github.com>
2022-11-08 11:29:56 -05:00
Lorenzo
712c478af6 Improve features (#215) 2022-11-08 11:29:56 -05:00
Lorenzo
4d36b7f34f Fix metrics::auc (#212)
* Fix metrics::auc
2022-11-08 11:29:56 -05:00
Lorenzo
a16927aa16 Port ensemble. Add Display to naive_bayes (#208) 2022-11-08 11:29:56 -05:00
Lorenzo
d91f4f7ce4 Update README.md 2022-11-08 11:29:56 -05:00
Lorenzo
a7fa0585eb Merge potential next release v0.4 (#187) Breaking Changes
* First draft of the new n-dimensional arrays + NB use case
* Improves default implementation of multiple Array methods
* Refactors tree methods
* Adds matrix decomposition routines
* Adds matrix decomposition methods to ndarray and nalgebra bindings
* Refactoring + linear regression now uses array2
* Ridge & Linear regression
* LBFGS optimizer & logistic regression
* LBFGS optimizer & logistic regression
* Changes linear methods, metrics and model selection methods to new n-dimensional arrays
* Switches KNN and clustering algorithms to new n-d array layer
* Refactors distance metrics
* Optimizes knn and clustering methods
* Refactors metrics module
* Switches decomposition methods to n-dimensional arrays
* Linalg refactoring - cleanup rng merge (#172)
* Remove legacy DenseMatrix and BaseMatrix implementation. Port the new Number, FloatNumber and Array implementation into module structure.
* Exclude AUC metrics. Needs reimplementation
* Improve developers walkthrough

New traits system in place at `src/numbers` and `src/linalg`
Co-authored-by: Lorenzo <tunedconsulting@gmail.com>

* Provide SupervisedEstimator with a constructor to avoid explicit dynamical box allocation in 'cross_validate' and 'cross_validate_predict' as required by the use of 'dyn' as per Rust 2021
* Implement getters to use as_ref() in src/neighbors
* Implement getters to use as_ref() in src/naive_bayes
* Implement getters to use as_ref() in src/linear
* Add Clone to src/naive_bayes
* Change signature for cross_validate and other model_selection functions to abide to use of dyn in Rust 2021
* Implement ndarray-bindings. Remove FloatNumber from implementations
* Drop nalgebra-bindings support (as decided in conf-call to go for ndarray)
* Remove benches. Benches will have their own repo at smartcore-benches
* Implement SVC
* Implement SVC serialization. Move search parameters in dedicated module
* Implement SVR. Definitely too slow
* Fix compilation issues for wasm (#202)

Co-authored-by: Luis Moreno <morenol@users.noreply.github.com>
* Fix tests (#203)

* Port linalg/traits/stats.rs
* Improve methods naming
* Improve Display for DenseMatrix

Co-authored-by: Montana Low <montanalow@users.noreply.github.com>
Co-authored-by: VolodymyrOrlov <volodymyr.orlov@gmail.com>
2022-11-08 11:29:56 -05:00
RJ Nowling
a32eb66a6a Dataset doc cleanup (#205)
* Update iris.rs

* Update mod.rs

* Update digits.rs
2022-11-08 11:29:56 -05:00
Lorenzo
f605f6e075 Update README.md 2022-11-08 11:29:56 -05:00
Lorenzo
3b1aaaadf7 Update README.md 2022-11-08 11:29:56 -05:00
Lorenzo
d015b12402 Update CONTRIBUTING.md 2022-11-08 11:29:56 -05:00
morenol
d5200074c2 fix: fix issue with iterator for svc search (#182) 2022-11-08 11:29:56 -05:00
morenol
473cdfc44d refactor: Try to follow similar pattern to other APIs (#180)
Co-authored-by: Luis Moreno <morenol@users.noreply.github.com>
2022-11-08 11:29:56 -05:00
morenol
ad2e6c2900 feat: expose hyper tuning module in model_selection (#179)
* feat: expose hyper tuning module in model_selection

* Move to a folder

Co-authored-by: Luis Moreno <morenol@users.noreply.github.com>
2022-11-08 11:29:56 -05:00
Lorenzo
9ea3133c27 Update CONTRIBUTING.md 2022-11-08 11:29:56 -05:00
Lorenzo
e4c47c7540 Add contribution guidelines (#178) 2022-11-08 11:29:56 -05:00
Montana Low
f4fd4d2239 make default params available to serde (#167)
* add seed param to search params

* make default params available to serde

* lints

* create defaults for enums

* lint
2022-11-08 11:29:56 -05:00
Montana Low
05dfffad5c add seed param to search params (#168) 2022-11-08 11:29:56 -05:00
morenol
a37b552a7d Lmm/add seeds in more algorithms (#164)
* Provide better output in flaky tests

* feat: add seed parameter to multiple algorithms

* Update changelog

Co-authored-by: Luis Moreno <morenol@users.noreply.github.com>
2022-11-08 11:29:56 -05:00
Montana Low
55e1158581 Complete grid search params (#166)
* grid search draft

* hyperparam search for linear estimators

* grid search for ensembles

* support grid search for more algos

* grid search for unsupervised algos

* minor cleanup
2022-11-08 11:29:56 -05:00
morenol
cfa824d7db Provide better output in flaky tests (#163) 2022-11-08 11:29:56 -05:00
morenol
bb5b437a32 feat: allocate first and then proceed to create matrix from Vec of Ro… (#159)
* feat: allocate first and then proceed to create matrix from Vec of RowVectors
2022-11-08 11:29:56 -05:00
morenol
851533dfa7 Make rand_distr optional (#161) 2022-11-08 11:29:56 -05:00
Lorenzo
0d996edafe Update LICENSE 2022-11-08 11:29:56 -05:00
morenol
f291b71f4a fix: fix compilation warnings when running only with default features (#160)
* fix: fix compilation warnings when running only with default features
Co-authored-by: Luis Moreno <morenol@users.noreply.github.com>
2022-11-08 11:29:56 -05:00
Tim Toebrock
2d75c2c405 Implement a generic read_csv method (#147)
* feat: Add interface to build `Matrix` from rows.
* feat: Add option to derive `RealNumber` from string.
To construct a `Matrix` from csv, and therefore from string, I need to be able to deserialize a generic `RealNumber` from string.
* feat: Implement `Matrix::read_csv`.
2022-11-08 11:29:56 -05:00
Montana Low
1f2597be74 grid search (#154)
* grid search draft
* hyperparam search for linear estimators
2022-11-08 11:29:56 -05:00
Montana Low
0f442e96c0 Handle multiclass precision/recall (#152)
* handle multiclass precision/recall
2022-11-08 11:29:56 -05:00
dependabot[bot]
44e4be23a6 Update criterion requirement from 0.3 to 0.4 (#150)
* Update criterion requirement from 0.3 to 0.4

Updates the requirements on [criterion](https://github.com/bheisler/criterion.rs) to permit the latest version.
- [Release notes](https://github.com/bheisler/criterion.rs/releases)
- [Changelog](https://github.com/bheisler/criterion.rs/blob/master/CHANGELOG.md)
- [Commits](https://github.com/bheisler/criterion.rs/compare/0.3.0...0.4.0)

---
updated-dependencies:
- dependency-name: criterion
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

* fix criterion

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Luis Moreno <morenol@users.noreply.github.com>
2022-11-08 11:29:56 -05:00
Christos Katsakioris
01f753f86d Add serde for StandardScaler (#148)
* Derive `serde::Serialize` and `serde::Deserialize` for
  `StandardScaler`.
* Add relevant unit test.

Signed-off-by: Christos Katsakioris <ckatsak@gmail.com>

Signed-off-by: Christos Katsakioris <ckatsak@gmail.com>
2022-11-08 11:29:56 -05:00
Tim Toebrock
df766eaf79 Implementation of Standard scaler (#143)
* docs: Fix typo in doc for categorical transformer.
* feat: Add option to take a column from Matrix.
I created the method `Matrix::take_column` that uses the `Matrix::take`-interface to extract a single column from a matrix. I need that feature in the implementation of  `StandardScaler`.
* feat: Add `StandardScaler`.
Authored-by: titoeb <timtoebrock@googlemail.com>
2022-11-08 11:29:56 -05:00
Lorenzo
09d9205696 Add example for FastPair (#144)
* Add example

* Move to top

* Add imports to example

* Fix imports
2022-11-08 11:29:56 -05:00
Lorenzo
dc7f01db4a Implement fastpair (#142)
* initial fastpair implementation
* FastPair initial implementation
* implement fastpair
* Add random test
* Add bench for fastpair
* Refactor with constructor for FastPair
* Add serialization for PairwiseDistance
* Add fp_bench feature for fastpair bench
2022-11-08 11:29:56 -05:00
Chris McComb
eb4b49d552 Added additional doctest and fixed indices (#141) 2022-11-08 11:29:56 -05:00
morenol
98e3465e7b Fix clippy warnings (#139)
Co-authored-by: Luis Moreno <morenol@users.noreply.github.com>
2022-11-08 11:29:56 -05:00
ferrouille
ea39024fd2 Add SVC::decision_function (#135) 2022-11-08 11:29:56 -05:00
dependabot[bot]
4e94feb872 Update nalgebra requirement from 0.23.0 to 0.31.0 (#128)
Updates the requirements on [nalgebra](https://github.com/dimforge/nalgebra) to permit the latest version.
- [Release notes](https://github.com/dimforge/nalgebra/releases)
- [Changelog](https://github.com/dimforge/nalgebra/blob/dev/CHANGELOG.md)
- [Commits](https://github.com/dimforge/nalgebra/compare/v0.23.0...v0.31.0)

---
updated-dependencies:
- dependency-name: nalgebra
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-11-08 11:29:56 -05:00
dependabot-preview[bot]
fa802d2d3f build(deps): update nalgebra requirement from 0.23.0 to 0.26.2 (#98)
* build(deps): update nalgebra requirement from 0.23.0 to 0.26.2

Updates the requirements on [nalgebra](https://github.com/dimforge/nalgebra) to permit the latest version.
- [Release notes](https://github.com/dimforge/nalgebra/releases)
- [Changelog](https://github.com/dimforge/nalgebra/blob/dev/CHANGELOG.md)
- [Commits](https://github.com/dimforge/nalgebra/compare/v0.23.0...v0.26.2)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* fix: updates for nalgebre

* test: explicitly call pow_mut from BaseVector since now it conflicts with nalgebra implementation

* Don't be strict with dependencies

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: Luis Moreno <morenol@users.noreply.github.com>
2022-11-08 11:29:56 -05:00
41 changed files with 1171 additions and 337 deletions
+1 -1
View File
@@ -36,7 +36,7 @@ jobs:
- name: Install Rust toolchain - name: Install Rust toolchain
uses: actions-rs/toolchain@v1 uses: actions-rs/toolchain@v1
with: with:
toolchain: stable toolchain: 1.81 # 1.82 seems to break wasm32 tests https://github.com/rustwasm/wasm-bindgen/issues/4274
target: ${{ matrix.platform.target }} target: ${{ matrix.platform.target }}
profile: minimal profile: minimal
default: true default: true
+1 -1
View File
@@ -48,7 +48,7 @@ getrandom = { version = "0.2.8", optional = true }
wasm-bindgen-test = "0.3" wasm-bindgen-test = "0.3"
[dev-dependencies] [dev-dependencies]
itertools = "0.12.0" itertools = "0.13.0"
serde_json = "1.0" serde_json = "1.0"
bincode = "1.3.1" bincode = "1.3.1"
+4 -4
View File
@@ -124,7 +124,7 @@ impl<T: Debug + PartialEq, D: Distance<T>> CoverTree<T, D> {
current_cover_set.push((d, &self.root)); current_cover_set.push((d, &self.root));
let mut heap = HeapSelection::with_capacity(k); let mut heap = HeapSelection::with_capacity(k);
heap.add(std::f64::MAX); heap.add(f64::MAX);
let mut empty_heap = true; let mut empty_heap = true;
if !self.identical_excluded || self.get_data_value(self.root.idx) != p { if !self.identical_excluded || self.get_data_value(self.root.idx) != p {
@@ -145,7 +145,7 @@ impl<T: Debug + PartialEq, D: Distance<T>> CoverTree<T, D> {
} }
let upper_bound = if empty_heap { let upper_bound = if empty_heap {
std::f64::INFINITY f64::INFINITY
} else { } else {
*heap.peek() *heap.peek()
}; };
@@ -291,7 +291,7 @@ impl<T: Debug + PartialEq, D: Distance<T>> CoverTree<T, D> {
} else { } else {
let max_dist = self.max(point_set); let max_dist = self.max(point_set);
let next_scale = (max_scale - 1).min(self.get_scale(max_dist)); let next_scale = (max_scale - 1).min(self.get_scale(max_dist));
if next_scale == std::i64::MIN { if next_scale == i64::MIN {
let mut children: Vec<Node> = Vec::new(); let mut children: Vec<Node> = Vec::new();
let mut leaf = self.new_leaf(p); let mut leaf = self.new_leaf(p);
children.push(leaf); children.push(leaf);
@@ -435,7 +435,7 @@ impl<T: Debug + PartialEq, D: Distance<T>> CoverTree<T, D> {
fn get_scale(&self, d: f64) -> i64 { fn get_scale(&self, d: f64) -> i64 {
if d == 0f64 { if d == 0f64 {
std::i64::MIN i64::MIN
} else { } else {
(self.inv_log_base * d.ln()).ceil() as i64 (self.inv_log_base * d.ln()).ceil() as i64
} }
+219
View File
@@ -0,0 +1,219 @@
//! This module provides FastPair, a data-structure for efficiently tracking the dynamic
//! closest pairs in a set of points, with an example usage in hierarchical clustering.[2][3][5]
//!
//! ## Purpose
//!
//! FastPair allows quick retrieval of the nearest neighbor for each data point by maintaining
//! a "conga line" of closest pairs. Each point retains a link to its known nearest neighbor,
//! and updates in the data structure propagate accordingly. This can be leveraged in
//! agglomerative clustering steps, where merging or insertion of new points must be reflected
//! in nearest-neighbor relationships.
//!
//! ## Example
//!
//! ```
//! use smartcore::metrics::distance::PairwiseDistance;
//! use smartcore::linalg::basic::matrix::DenseMatrix;
//! use smartcore::algorithm::neighbour::fastpair::FastPair;
//!
//! let x = DenseMatrix::from_2d_array(&[
//! &[5.1, 3.5, 1.4, 0.2],
//! &[4.9, 3.0, 1.4, 0.2],
//! &[4.7, 3.2, 1.3, 0.2],
//! &[4.6, 3.1, 1.5, 0.2],
//! &[5.0, 3.6, 1.4, 0.2],
//! &[5.4, 3.9, 1.7, 0.4],
//! ]).unwrap();
//!
//! let fastpair = FastPair::new(&x).unwrap();
//! let closest = fastpair.closest_pair();
//! println!("Closest pair: {:?}", closest);
//! ```
use std::collections::HashMap;
use num::Bounded;
use crate::error::{Failed, FailedError};
use crate::linalg::basic::arrays::{Array, Array1, Array2};
use crate::metrics::distance::euclidian::Euclidian;
use crate::metrics::distance::PairwiseDistance;
use crate::numbers::floatnum::FloatNumber;
use crate::numbers::realnum::RealNumber;
/// Eppstein dynamic closet-pair structure
/// 'M' can be a matrix-like trait that provides row access
#[derive(Debug)]
pub struct EppsteinDCP<'a, T: RealNumber + FloatNumber, M: Array2<T>> {
samples: &'a M,
// "buckets" store, for each row, a small structure recording potential neighbors
neighbors: HashMap<usize, PairwiseDistance<T>>,
}
impl<'a, T: RealNumber + FloatNumber, M: Array2<T>> EppsteinDCP<'a, T, M> {
/// Creates a new EppsteinDCP instance with the given data
pub fn new(m: &'a M) -> Result<Self, Failed> {
if m.shape().0 < 3 {
return Err(Failed::because(
FailedError::FindFailed,
"min number of rows should be 3",
));
}
let mut this = Self {
samples: m,
neighbors: HashMap::with_capacity(m.shape().0),
};
this.initialize();
Ok(this)
}
/// Build an initial "conga line" or chain of potential neighbors
/// akin to Eppsteins technique[2].
fn initialize(&mut self) {
let n = self.samples.shape().0;
if n < 2 {
return;
}
// Assign each row i some large distance by default
for i in 0..n {
self.neighbors.insert(
i,
PairwiseDistance {
node: i,
neighbour: None,
distance: Some(<T as Bounded>::max_value()),
},
);
}
// Example: link each i to the next, forming a chain
// (depending on the actual Eppstein approach, can refine)
for i in 0..(n - 1) {
let dist = self.compute_dist(i, i + 1);
self.neighbors.entry(i).and_modify(|pd| {
pd.neighbour = Some(i + 1);
pd.distance = Some(dist);
});
}
// Potential refinement steps omitted for brevity
}
/// Insert a point into the structure.
pub fn insert(&mut self, row_idx: usize) {
// Expand data, find neighbor to link with
// For example, link row_idx to nearest among existing
let mut best_neighbor = None;
let mut best_d = <T as Bounded>::max_value();
for (i, _) in &self.neighbors {
let d = self.compute_dist(*i, row_idx);
if d < best_d {
best_d = d;
best_neighbor = Some(*i);
}
}
self.neighbors.insert(
row_idx,
PairwiseDistance {
node: row_idx,
neighbour: best_neighbor,
distance: Some(best_d),
},
);
// For the best_neighbor, you might want to see if row_idx becomes closer
if let Some(kn) = best_neighbor {
let dist = self.compute_dist(row_idx, kn);
let entry = self.neighbors.get_mut(&kn).unwrap();
if dist < entry.distance.unwrap() {
entry.neighbour = Some(row_idx);
entry.distance = Some(dist);
}
}
}
/// For hierarchical clustering, discover minimal pairs, then merge
pub fn closest_pair(&self) -> Option<PairwiseDistance<T>> {
let mut min_pair: Option<PairwiseDistance<T>> = None;
for (_, pd) in &self.neighbors {
if let Some(d) = pd.distance {
if min_pair.is_none() || d < min_pair.as_ref().unwrap().distance.unwrap() {
min_pair = Some(pd.clone());
}
}
}
min_pair
}
fn compute_dist(&self, i: usize, j: usize) -> T {
// Example: Euclidean
let row_i = self.samples.get_row(i);
let row_j = self.samples.get_row(j);
row_i
.iterator(0)
.zip(row_j.iterator(0))
.map(|(a, b)| (*a - *b) * (*a - *b))
.sum()
}
}
/// Simple usage
#[cfg(test)]
mod tests_eppstein {
use super::*;
use crate::linalg::basic::matrix::DenseMatrix;
#[test]
fn test_eppstein() {
let matrix =
DenseMatrix::from_2d_array(&[&vec![1.0, 2.0], &vec![2.0, 2.0], &vec![5.0, 3.0]])
.unwrap();
let mut dcp = EppsteinDCP::new(&matrix).unwrap();
dcp.insert(2);
let cp = dcp.closest_pair();
assert!(cp.is_some());
}
#[test]
fn compare_fastpair_eppstein() {
use crate::algorithm::neighbour::fastpair::FastPair;
// Assuming EppsteinDCP is implemented in a similar module
use crate::algorithm::neighbour::eppstein::EppsteinDCP;
// Create a static example matrix
let x = DenseMatrix::from_2d_array(&[
&[5.1, 3.5, 1.4, 0.2],
&[4.9, 3.0, 1.4, 0.2],
&[4.7, 3.2, 1.3, 0.2],
&[4.6, 3.1, 1.5, 0.2],
&[5.0, 3.6, 1.4, 0.2],
&[5.4, 3.9, 1.7, 0.4],
&[4.6, 3.4, 1.4, 0.3],
&[5.0, 3.4, 1.5, 0.2],
&[4.4, 2.9, 1.4, 0.2],
&[4.9, 3.1, 1.5, 0.1],
])
.unwrap();
// Build FastPair
let fastpair = FastPair::new(&x).unwrap();
let pair_fastpair = fastpair.closest_pair();
// Build EppsteinDCP
let eppstein = EppsteinDCP::new(&x).unwrap();
let pair_eppstein = eppstein.closest_pair();
// Compare the results
assert_eq!(pair_fastpair.node, pair_eppstein.as_ref().unwrap().node);
assert_eq!(
pair_fastpair.neighbour.unwrap(),
pair_eppstein.as_ref().unwrap().neighbour.unwrap()
);
// Use a small epsilon for floating-point comparison
let epsilon = 1e-9;
let diff: f64 =
pair_fastpair.distance.unwrap() - pair_eppstein.as_ref().unwrap().distance.unwrap();
assert!(diff.abs() < epsilon);
println!("FastPair result: {:?}", pair_fastpair);
println!("EppsteinDCP result: {:?}", pair_eppstein);
}
}
+118 -10
View File
@@ -52,10 +52,8 @@ pub struct FastPair<'a, T: RealNumber + FloatNumber, M: Array2<T>> {
} }
impl<'a, T: RealNumber + FloatNumber, M: Array2<T>> FastPair<'a, T, M> { impl<'a, T: RealNumber + FloatNumber, M: Array2<T>> FastPair<'a, T, M> {
///
/// Constructor /// Constructor
/// Instantiate and inizialise the algorithm /// Instantiate and initialize the algorithm
///
pub fn new(m: &'a M) -> Result<Self, Failed> { pub fn new(m: &'a M) -> Result<Self, Failed> {
if m.shape().0 < 3 { if m.shape().0 < 3 {
return Err(Failed::because( return Err(Failed::because(
@@ -74,10 +72,8 @@ impl<'a, T: RealNumber + FloatNumber, M: Array2<T>> FastPair<'a, T, M> {
Ok(init) Ok(init)
} }
///
/// Initialise `FastPair` by passing a `Array2`. /// Initialise `FastPair` by passing a `Array2`.
/// Build a FastPairs data-structure from a set of (new) points. /// Build a FastPairs data-structure from a set of (new) points.
///
fn init(&mut self) { fn init(&mut self) {
// basic measures // basic measures
let len = self.samples.shape().0; let len = self.samples.shape().0;
@@ -158,9 +154,7 @@ impl<'a, T: RealNumber + FloatNumber, M: Array2<T>> FastPair<'a, T, M> {
self.neighbours = neighbours; self.neighbours = neighbours;
} }
///
/// Find closest pair by scanning list of nearest neighbors. /// Find closest pair by scanning list of nearest neighbors.
///
#[allow(dead_code)] #[allow(dead_code)]
pub fn closest_pair(&self) -> PairwiseDistance<T> { pub fn closest_pair(&self) -> PairwiseDistance<T> {
let mut a = self.neighbours[0]; // Start with first point let mut a = self.neighbours[0]; // Start with first point
@@ -179,6 +173,21 @@ impl<'a, T: RealNumber + FloatNumber, M: Array2<T>> FastPair<'a, T, M> {
} }
} }
///
/// Return order dissimilarities from closest to furthest
///
#[allow(dead_code)]
pub fn ordered_pairs(&self) -> std::vec::IntoIter<&PairwiseDistance<T>> {
// improvement: implement this to return `impl Iterator<Item = &PairwiseDistance<T>>`
// need to implement trait `Iterator` for `Vec<&PairwiseDistance<T>>`
let mut distances = self
.distances
.values()
.collect::<Vec<&PairwiseDistance<T>>>();
distances.sort_by(|a, b| a.partial_cmp(b).unwrap());
distances.into_iter()
}
// //
// Compute distances from input to all other points in data-structure. // Compute distances from input to all other points in data-structure.
// input is the row index of the sample matrix // input is the row index of the sample matrix
@@ -217,10 +226,10 @@ mod tests_fastpair {
use super::*; use super::*;
use crate::linalg::basic::{arrays::Array, matrix::DenseMatrix}; use crate::linalg::basic::{arrays::Array, matrix::DenseMatrix};
///
/// Brute force algorithm, used only for comparison and testing /// Brute force algorithm, used only for comparison and testing
/// pub fn closest_pair_brute(
pub fn closest_pair_brute(fastpair: &FastPair<f64, DenseMatrix<f64>>) -> PairwiseDistance<f64> { fastpair: &FastPair<'_, f64, DenseMatrix<f64>>,
) -> PairwiseDistance<f64> {
use itertools::Itertools; use itertools::Itertools;
let m = fastpair.samples.shape().0; let m = fastpair.samples.shape().0;
@@ -594,4 +603,103 @@ mod tests_fastpair {
assert_eq!(closest, min_dissimilarity); assert_eq!(closest, min_dissimilarity);
} }
#[test]
fn fastpair_ordered_pairs() {
let x = DenseMatrix::<f64>::from_2d_array(&[
&[5.1, 3.5, 1.4, 0.2],
&[4.9, 3.0, 1.4, 0.2],
&[4.7, 3.2, 1.3, 0.2],
&[4.6, 3.1, 1.5, 0.2],
&[5.0, 3.6, 1.4, 0.2],
&[5.4, 3.9, 1.7, 0.4],
&[4.9, 3.1, 1.5, 0.1],
&[7.0, 3.2, 4.7, 1.4],
&[6.4, 3.2, 4.5, 1.5],
&[6.9, 3.1, 4.9, 1.5],
&[5.5, 2.3, 4.0, 1.3],
&[6.5, 2.8, 4.6, 1.5],
&[4.6, 3.4, 1.4, 0.3],
&[5.0, 3.4, 1.5, 0.2],
&[4.4, 2.9, 1.4, 0.2],
])
.unwrap();
let fastpair = FastPair::new(&x).unwrap();
let ordered = fastpair.ordered_pairs();
let mut previous: f64 = -1.0;
for p in ordered {
if previous == -1.0 {
previous = p.distance.unwrap();
} else {
let current = p.distance.unwrap();
assert!(current >= previous);
previous = current;
}
}
}
#[test]
fn test_empty_set() {
let empty_matrix = DenseMatrix::<f64>::zeros(0, 0);
let result = FastPair::new(&empty_matrix);
assert!(result.is_err());
if let Err(e) = result {
assert_eq!(
e,
Failed::because(FailedError::FindFailed, "min number of rows should be 3")
);
}
}
#[test]
fn test_single_point() {
let single_point = DenseMatrix::from_2d_array(&[&[1.0, 2.0, 3.0]]).unwrap();
let result = FastPair::new(&single_point);
assert!(result.is_err());
if let Err(e) = result {
assert_eq!(
e,
Failed::because(FailedError::FindFailed, "min number of rows should be 3")
);
}
}
#[test]
fn test_two_points() {
let two_points = DenseMatrix::from_2d_array(&[&[1.0, 2.0], &[3.0, 4.0]]).unwrap();
let result = FastPair::new(&two_points);
assert!(result.is_err());
if let Err(e) = result {
assert_eq!(
e,
Failed::because(FailedError::FindFailed, "min number of rows should be 3")
);
}
}
#[test]
fn test_three_identical_points() {
let identical_points =
DenseMatrix::from_2d_array(&[&[1.0, 1.0], &[1.0, 1.0], &[1.0, 1.0]]).unwrap();
let result = FastPair::new(&identical_points);
assert!(result.is_ok());
let fastpair = result.unwrap();
let closest_pair = fastpair.closest_pair();
assert_eq!(closest_pair.distance, Some(0.0));
}
#[test]
fn test_result_unwrapping() {
let valid_matrix =
DenseMatrix::from_2d_array(&[&[1.0, 2.0], &[3.0, 4.0], &[5.0, 6.0], &[7.0, 8.0]])
.unwrap();
let result = FastPair::new(&valid_matrix);
assert!(result.is_ok());
// This should not panic
let _fastpair = result.unwrap();
}
} }
+2 -2
View File
@@ -61,7 +61,7 @@ impl<T, D: Distance<T>> LinearKNNSearch<T, D> {
for _ in 0..k { for _ in 0..k {
heap.add(KNNPoint { heap.add(KNNPoint {
distance: std::f64::INFINITY, distance: f64::INFINITY,
index: None, index: None,
}); });
} }
@@ -215,7 +215,7 @@ mod tests {
}; };
let point_inf = KNNPoint { let point_inf = KNNPoint {
distance: std::f64::INFINITY, distance: f64::INFINITY,
index: Some(3), index: Some(3),
}; };
+3 -1
View File
@@ -41,7 +41,9 @@ use serde::{Deserialize, Serialize};
pub(crate) mod bbd_tree; pub(crate) mod bbd_tree;
/// tree data structure for fast nearest neighbor search /// tree data structure for fast nearest neighbor search
pub mod cover_tree; pub mod cover_tree;
/// fastpair closest neighbour algorithm /// eppstein pairwise closest neighbour algorithm
pub mod eppstein;
/// fastpair pairwise closest neighbour algorithm
pub mod fastpair; pub mod fastpair;
/// very simple algorithm that sequentially checks each element of the list until a match is found or the whole list has been searched. /// very simple algorithm that sequentially checks each element of the list until a match is found or the whole list has been searched.
pub mod linear_search; pub mod linear_search;
+2 -2
View File
@@ -133,7 +133,7 @@ mod tests {
#[test] #[test]
fn test_add1() { fn test_add1() {
let mut heap = HeapSelection::with_capacity(3); let mut heap = HeapSelection::with_capacity(3);
heap.add(std::f64::INFINITY); heap.add(f64::INFINITY);
heap.add(-5f64); heap.add(-5f64);
heap.add(4f64); heap.add(4f64);
heap.add(-1f64); heap.add(-1f64);
@@ -151,7 +151,7 @@ mod tests {
#[test] #[test]
fn test_add2() { fn test_add2() {
let mut heap = HeapSelection::with_capacity(3); let mut heap = HeapSelection::with_capacity(3);
heap.add(std::f64::INFINITY); heap.add(f64::INFINITY);
heap.add(0.0); heap.add(0.0);
heap.add(8.4852); heap.add(8.4852);
heap.add(5.6568); heap.add(5.6568);
+1
View File
@@ -3,6 +3,7 @@ use num_traits::Num;
pub trait QuickArgSort { pub trait QuickArgSort {
fn quick_argsort_mut(&mut self) -> Vec<usize>; fn quick_argsort_mut(&mut self) -> Vec<usize>;
#[allow(dead_code)]
fn quick_argsort(&self) -> Vec<usize>; fn quick_argsort(&self) -> Vec<usize>;
} }
+4 -4
View File
@@ -96,7 +96,7 @@ impl<TX: Number, TY: Number, X: Array2<TX>, Y: Array1<TY>> PartialEq for KMeans<
return false; return false;
} }
for j in 0..self.centroids[i].len() { for j in 0..self.centroids[i].len() {
if (self.centroids[i][j] - other.centroids[i][j]).abs() > std::f64::EPSILON { if (self.centroids[i][j] - other.centroids[i][j]).abs() > f64::EPSILON {
return false; return false;
} }
} }
@@ -270,7 +270,7 @@ impl<TX: Number, TY: Number, X: Array2<TX>, Y: Array1<TY>> KMeans<TX, TY, X, Y>
let (n, d) = data.shape(); let (n, d) = data.shape();
let mut distortion = std::f64::MAX; let mut distortion = f64::MAX;
let mut y = KMeans::<TX, TY, X, Y>::kmeans_plus_plus(data, parameters.k, parameters.seed); let mut y = KMeans::<TX, TY, X, Y>::kmeans_plus_plus(data, parameters.k, parameters.seed);
let mut size = vec![0; parameters.k]; let mut size = vec![0; parameters.k];
let mut centroids = vec![vec![0f64; d]; parameters.k]; let mut centroids = vec![vec![0f64; d]; parameters.k];
@@ -331,7 +331,7 @@ impl<TX: Number, TY: Number, X: Array2<TX>, Y: Array1<TY>> KMeans<TX, TY, X, Y>
let mut row = vec![0f64; x.shape().1]; let mut row = vec![0f64; x.shape().1];
for i in 0..n { for i in 0..n {
let mut min_dist = std::f64::MAX; let mut min_dist = f64::MAX;
let mut best_cluster = 0; let mut best_cluster = 0;
for j in 0..self.k { for j in 0..self.k {
@@ -361,7 +361,7 @@ impl<TX: Number, TY: Number, X: Array2<TX>, Y: Array1<TY>> KMeans<TX, TY, X, Y>
.cloned() .cloned()
.collect(); .collect();
let mut d = vec![std::f64::MAX; n]; let mut d = vec![f64::MAX; n];
let mut row = vec![TX::zero(); data.shape().1]; let mut row = vec![TX::zero(); data.shape().1];
for j in 1..k { for j in 1..k {
-1
View File
@@ -7,7 +7,6 @@
clippy::approx_constant clippy::approx_constant
)] )]
#![warn(missing_docs)] #![warn(missing_docs)]
#![warn(rustdoc::missing_doc_code_examples)]
//! # smartcore //! # smartcore
//! //!
+76 -76
View File
@@ -265,11 +265,11 @@ pub trait ArrayView1<T: Debug + Display + Copy + Sized>: Array<T, usize> {
if p.is_infinite() && p.is_sign_positive() { if p.is_infinite() && p.is_sign_positive() {
self.iterator(0) self.iterator(0)
.map(|x| x.to_f64().unwrap().abs()) .map(|x| x.to_f64().unwrap().abs())
.fold(std::f64::NEG_INFINITY, |a, b| a.max(b)) .fold(f64::NEG_INFINITY, |a, b| a.max(b))
} else if p.is_infinite() && p.is_sign_negative() { } else if p.is_infinite() && p.is_sign_negative() {
self.iterator(0) self.iterator(0)
.map(|x| x.to_f64().unwrap().abs()) .map(|x| x.to_f64().unwrap().abs())
.fold(std::f64::INFINITY, |a, b| a.min(b)) .fold(f64::INFINITY, |a, b| a.min(b))
} else { } else {
let mut norm = 0f64; let mut norm = 0f64;
@@ -558,11 +558,11 @@ pub trait ArrayView2<T: Debug + Display + Copy + Sized>: Array<T, (usize, usize)
if p.is_infinite() && p.is_sign_positive() { if p.is_infinite() && p.is_sign_positive() {
self.iterator(0) self.iterator(0)
.map(|x| x.to_f64().unwrap().abs()) .map(|x| x.to_f64().unwrap().abs())
.fold(std::f64::NEG_INFINITY, |a, b| a.max(b)) .fold(f64::NEG_INFINITY, |a, b| a.max(b))
} else if p.is_infinite() && p.is_sign_negative() { } else if p.is_infinite() && p.is_sign_negative() {
self.iterator(0) self.iterator(0)
.map(|x| x.to_f64().unwrap().abs()) .map(|x| x.to_f64().unwrap().abs())
.fold(std::f64::INFINITY, |a, b| a.min(b)) .fold(f64::INFINITY, |a, b| a.min(b))
} else { } else {
let mut norm = 0f64; let mut norm = 0f64;
@@ -731,34 +731,34 @@ pub trait MutArrayView1<T: Debug + Display + Copy + Sized>:
pub trait MutArrayView2<T: Debug + Display + Copy + Sized>: pub trait MutArrayView2<T: Debug + Display + Copy + Sized>:
MutArray<T, (usize, usize)> + ArrayView2<T> MutArray<T, (usize, usize)> + ArrayView2<T>
{ {
/// /// copy values from another array
fn copy_from(&mut self, other: &dyn Array<T, (usize, usize)>) { fn copy_from(&mut self, other: &dyn Array<T, (usize, usize)>) {
self.iterator_mut(0) self.iterator_mut(0)
.zip(other.iterator(0)) .zip(other.iterator(0))
.for_each(|(s, o)| *s = *o); .for_each(|(s, o)| *s = *o);
} }
/// /// update view with absolute values
fn abs_mut(&mut self) fn abs_mut(&mut self)
where where
T: Number + Signed, T: Number + Signed,
{ {
self.iterator_mut(0).for_each(|v| *v = v.abs()); self.iterator_mut(0).for_each(|v| *v = v.abs());
} }
/// /// update view values with opposite sign
fn neg_mut(&mut self) fn neg_mut(&mut self)
where where
T: Number + Neg<Output = T>, T: Number + Neg<Output = T>,
{ {
self.iterator_mut(0).for_each(|v| *v = -*v); self.iterator_mut(0).for_each(|v| *v = -*v);
} }
/// /// update view values at power `p`
fn pow_mut(&mut self, p: T) fn pow_mut(&mut self, p: T)
where where
T: RealNumber, T: RealNumber,
{ {
self.iterator_mut(0).for_each(|v| *v = v.powf(p)); self.iterator_mut(0).for_each(|v| *v = v.powf(p));
} }
/// /// scale view values
fn scale_mut(&mut self, mean: &[T], std: &[T], axis: u8) fn scale_mut(&mut self, mean: &[T], std: &[T], axis: u8)
where where
T: Number, T: Number,
@@ -784,27 +784,27 @@ pub trait MutArrayView2<T: Debug + Display + Copy + Sized>:
/// Trait for mutable 1D-array view /// Trait for mutable 1D-array view
pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized + Clone { pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized + Clone {
/// /// return a view of the array
fn slice<'a>(&'a self, range: Range<usize>) -> Box<dyn ArrayView1<T> + 'a>; fn slice<'a>(&'a self, range: Range<usize>) -> Box<dyn ArrayView1<T> + 'a>;
/// /// return a mutable view of the array
fn slice_mut<'a>(&'a mut self, range: Range<usize>) -> Box<dyn MutArrayView1<T> + 'a>; fn slice_mut<'a>(&'a mut self, range: Range<usize>) -> Box<dyn MutArrayView1<T> + 'a>;
/// /// fill array with a given value
fn fill(len: usize, value: T) -> Self fn fill(len: usize, value: T) -> Self
where where
Self: Sized; Self: Sized;
/// /// create array from iterator
fn from_iterator<I: Iterator<Item = T>>(iter: I, len: usize) -> Self fn from_iterator<I: Iterator<Item = T>>(iter: I, len: usize) -> Self
where where
Self: Sized; Self: Sized;
/// /// create array from vector
fn from_vec_slice(slice: &[T]) -> Self fn from_vec_slice(slice: &[T]) -> Self
where where
Self: Sized; Self: Sized;
/// /// create array from slice
fn from_slice(slice: &'_ dyn ArrayView1<T>) -> Self fn from_slice(slice: &'_ dyn ArrayView1<T>) -> Self
where where
Self: Sized; Self: Sized;
/// /// create a zero array
fn zeros(len: usize) -> Self fn zeros(len: usize) -> Self
where where
T: Number, T: Number,
@@ -812,7 +812,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
{ {
Self::fill(len, T::zero()) Self::fill(len, T::zero())
} }
/// /// create an array of ones
fn ones(len: usize) -> Self fn ones(len: usize) -> Self
where where
T: Number, T: Number,
@@ -820,7 +820,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
{ {
Self::fill(len, T::one()) Self::fill(len, T::one())
} }
/// /// create an array of random values
fn rand(len: usize) -> Self fn rand(len: usize) -> Self
where where
T: RealNumber, T: RealNumber,
@@ -828,7 +828,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
{ {
Self::from_iterator((0..len).map(|_| T::rand()), len) Self::from_iterator((0..len).map(|_| T::rand()), len)
} }
/// /// add a scalar to the array
fn add_scalar(&self, x: T) -> Self fn add_scalar(&self, x: T) -> Self
where where
T: Number, T: Number,
@@ -838,7 +838,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
result.add_scalar_mut(x); result.add_scalar_mut(x);
result result
} }
/// /// subtract a scalar from the array
fn sub_scalar(&self, x: T) -> Self fn sub_scalar(&self, x: T) -> Self
where where
T: Number, T: Number,
@@ -848,7 +848,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
result.sub_scalar_mut(x); result.sub_scalar_mut(x);
result result
} }
/// /// divide a scalar from the array
fn div_scalar(&self, x: T) -> Self fn div_scalar(&self, x: T) -> Self
where where
T: Number, T: Number,
@@ -858,7 +858,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
result.div_scalar_mut(x); result.div_scalar_mut(x);
result result
} }
/// /// multiply a scalar to the array
fn mul_scalar(&self, x: T) -> Self fn mul_scalar(&self, x: T) -> Self
where where
T: Number, T: Number,
@@ -868,7 +868,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
result.mul_scalar_mut(x); result.mul_scalar_mut(x);
result result
} }
/// /// sum of two arrays
fn add(&self, other: &dyn Array<T, usize>) -> Self fn add(&self, other: &dyn Array<T, usize>) -> Self
where where
T: Number, T: Number,
@@ -878,7 +878,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
result.add_mut(other); result.add_mut(other);
result result
} }
/// /// subtract two arrays
fn sub(&self, other: &impl Array1<T>) -> Self fn sub(&self, other: &impl Array1<T>) -> Self
where where
T: Number, T: Number,
@@ -888,7 +888,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
result.sub_mut(other); result.sub_mut(other);
result result
} }
/// /// multiply two arrays
fn mul(&self, other: &dyn Array<T, usize>) -> Self fn mul(&self, other: &dyn Array<T, usize>) -> Self
where where
T: Number, T: Number,
@@ -898,7 +898,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
result.mul_mut(other); result.mul_mut(other);
result result
} }
/// /// divide two arrays
fn div(&self, other: &dyn Array<T, usize>) -> Self fn div(&self, other: &dyn Array<T, usize>) -> Self
where where
T: Number, T: Number,
@@ -908,7 +908,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
result.div_mut(other); result.div_mut(other);
result result
} }
/// /// replace values with another array
fn take(&self, index: &[usize]) -> Self fn take(&self, index: &[usize]) -> Self
where where
Self: Sized, Self: Sized,
@@ -920,7 +920,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
); );
Self::from_iterator(index.iter().map(move |&i| *self.get(i)), index.len()) Self::from_iterator(index.iter().map(move |&i| *self.get(i)), index.len())
} }
/// /// create a view of the array with absolute values
fn abs(&self) -> Self fn abs(&self) -> Self
where where
T: Number + Signed, T: Number + Signed,
@@ -930,7 +930,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
result.abs_mut(); result.abs_mut();
result result
} }
/// /// create a view of the array with opposite sign
fn neg(&self) -> Self fn neg(&self) -> Self
where where
T: Number + Neg<Output = T>, T: Number + Neg<Output = T>,
@@ -940,7 +940,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
result.neg_mut(); result.neg_mut();
result result
} }
/// /// create a view of the array with values at power `p`
fn pow(&self, p: T) -> Self fn pow(&self, p: T) -> Self
where where
T: RealNumber, T: RealNumber,
@@ -950,7 +950,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
result.pow_mut(p); result.pow_mut(p);
result result
} }
/// /// apply argsort to the array
fn argsort(&self) -> Vec<usize> fn argsort(&self) -> Vec<usize>
where where
T: Number + PartialOrd, T: Number + PartialOrd,
@@ -958,12 +958,12 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
let mut v = self.clone(); let mut v = self.clone();
v.argsort_mut() v.argsort_mut()
} }
/// /// map values of the array
fn map<O: Debug + Display + Copy + Sized, A: Array1<O>, F: FnMut(&T) -> O>(self, f: F) -> A { fn map<O: Debug + Display + Copy + Sized, A: Array1<O>, F: FnMut(&T) -> O>(self, f: F) -> A {
let len = self.shape(); let len = self.shape();
A::from_iterator(self.iterator(0).map(f), len) A::from_iterator(self.iterator(0).map(f), len)
} }
/// /// apply softmax to the array
fn softmax(&self) -> Self fn softmax(&self) -> Self
where where
T: RealNumber, T: RealNumber,
@@ -973,7 +973,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
result.softmax_mut(); result.softmax_mut();
result result
} }
/// /// multiply array by matrix
fn xa(&self, a_transpose: bool, a: &dyn ArrayView2<T>) -> Self fn xa(&self, a_transpose: bool, a: &dyn ArrayView2<T>) -> Self
where where
T: Number, T: Number,
@@ -1003,7 +1003,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
result result
} }
/// /// check if two arrays are approximately equal
fn approximate_eq(&self, other: &Self, error: T) -> bool fn approximate_eq(&self, other: &Self, error: T) -> bool
where where
T: Number + RealNumber, T: Number + RealNumber,
@@ -1015,13 +1015,13 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
/// Trait for mutable 2D-array view /// Trait for mutable 2D-array view
pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized + Clone { pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized + Clone {
/// /// fill 2d array with a given value
fn fill(nrows: usize, ncols: usize, value: T) -> Self; fn fill(nrows: usize, ncols: usize, value: T) -> Self;
/// /// get a view of the 2d array
fn slice<'a>(&'a self, rows: Range<usize>, cols: Range<usize>) -> Box<dyn ArrayView2<T> + 'a> fn slice<'a>(&'a self, rows: Range<usize>, cols: Range<usize>) -> Box<dyn ArrayView2<T> + 'a>
where where
Self: Sized; Self: Sized;
/// /// get a mutable view of the 2d array
fn slice_mut<'a>( fn slice_mut<'a>(
&'a mut self, &'a mut self,
rows: Range<usize>, rows: Range<usize>,
@@ -1029,31 +1029,31 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
) -> Box<dyn MutArrayView2<T> + 'a> ) -> Box<dyn MutArrayView2<T> + 'a>
where where
Self: Sized; Self: Sized;
/// /// create 2d array from iterator
fn from_iterator<I: Iterator<Item = T>>(iter: I, nrows: usize, ncols: usize, axis: u8) -> Self; fn from_iterator<I: Iterator<Item = T>>(iter: I, nrows: usize, ncols: usize, axis: u8) -> Self;
/// /// get row from 2d array
fn get_row<'a>(&'a self, row: usize) -> Box<dyn ArrayView1<T> + 'a> fn get_row<'a>(&'a self, row: usize) -> Box<dyn ArrayView1<T> + 'a>
where where
Self: Sized; Self: Sized;
/// /// get column from 2d array
fn get_col<'a>(&'a self, col: usize) -> Box<dyn ArrayView1<T> + 'a> fn get_col<'a>(&'a self, col: usize) -> Box<dyn ArrayView1<T> + 'a>
where where
Self: Sized; Self: Sized;
/// /// create a zero 2d array
fn zeros(nrows: usize, ncols: usize) -> Self fn zeros(nrows: usize, ncols: usize) -> Self
where where
T: Number, T: Number,
{ {
Self::fill(nrows, ncols, T::zero()) Self::fill(nrows, ncols, T::zero())
} }
/// /// create a 2d array of ones
fn ones(nrows: usize, ncols: usize) -> Self fn ones(nrows: usize, ncols: usize) -> Self
where where
T: Number, T: Number,
{ {
Self::fill(nrows, ncols, T::one()) Self::fill(nrows, ncols, T::one())
} }
/// /// create an identity matrix
fn eye(size: usize) -> Self fn eye(size: usize) -> Self
where where
T: Number, T: Number,
@@ -1066,29 +1066,29 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
matrix matrix
} }
/// /// create a 2d array of random values
fn rand(nrows: usize, ncols: usize) -> Self fn rand(nrows: usize, ncols: usize) -> Self
where where
T: RealNumber, T: RealNumber,
{ {
Self::from_iterator((0..nrows * ncols).map(|_| T::rand()), nrows, ncols, 0) Self::from_iterator((0..nrows * ncols).map(|_| T::rand()), nrows, ncols, 0)
} }
/// /// crate from 2d slice
fn from_slice(slice: &dyn ArrayView2<T>) -> Self { fn from_slice(slice: &dyn ArrayView2<T>) -> Self {
let (nrows, ncols) = slice.shape(); let (nrows, ncols) = slice.shape();
Self::from_iterator(slice.iterator(0).cloned(), nrows, ncols, 0) Self::from_iterator(slice.iterator(0).cloned(), nrows, ncols, 0)
} }
/// /// create from row
fn from_row(slice: &dyn ArrayView1<T>) -> Self { fn from_row(slice: &dyn ArrayView1<T>) -> Self {
let ncols = slice.shape(); let ncols = slice.shape();
Self::from_iterator(slice.iterator(0).cloned(), 1, ncols, 0) Self::from_iterator(slice.iterator(0).cloned(), 1, ncols, 0)
} }
/// /// create from column
fn from_column(slice: &dyn ArrayView1<T>) -> Self { fn from_column(slice: &dyn ArrayView1<T>) -> Self {
let nrows = slice.shape(); let nrows = slice.shape();
Self::from_iterator(slice.iterator(0).cloned(), nrows, 1, 0) Self::from_iterator(slice.iterator(0).cloned(), nrows, 1, 0)
} }
/// /// transpose 2d array
fn transpose(&self) -> Self { fn transpose(&self) -> Self {
let (nrows, ncols) = self.shape(); let (nrows, ncols) = self.shape();
let mut m = Self::fill(ncols, nrows, *self.get((0, 0))); let mut m = Self::fill(ncols, nrows, *self.get((0, 0)));
@@ -1099,7 +1099,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
} }
m m
} }
/// /// change shape of 2d array
fn reshape(&self, nrows: usize, ncols: usize, axis: u8) -> Self { fn reshape(&self, nrows: usize, ncols: usize, axis: u8) -> Self {
let (onrows, oncols) = self.shape(); let (onrows, oncols) = self.shape();
@@ -1110,7 +1110,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
Self::from_iterator(self.iterator(0).cloned(), nrows, ncols, axis) Self::from_iterator(self.iterator(0).cloned(), nrows, ncols, axis)
} }
/// /// multiply two 2d arrays
fn matmul(&self, other: &dyn ArrayView2<T>) -> Self fn matmul(&self, other: &dyn ArrayView2<T>) -> Self
where where
T: Number, T: Number,
@@ -1136,7 +1136,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
result result
} }
/// /// matrix multiplication
fn ab(&self, a_transpose: bool, b: &dyn ArrayView2<T>, b_transpose: bool) -> Self fn ab(&self, a_transpose: bool, b: &dyn ArrayView2<T>, b_transpose: bool) -> Self
where where
T: Number, T: Number,
@@ -1171,7 +1171,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
result result
} }
} }
/// /// matrix vector multiplication
fn ax(&self, a_transpose: bool, x: &dyn ArrayView1<T>) -> Self fn ax(&self, a_transpose: bool, x: &dyn ArrayView1<T>) -> Self
where where
T: Number, T: Number,
@@ -1199,7 +1199,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
} }
result result
} }
/// /// concatenate 1d array
fn concatenate_1d<'a>(arrays: &'a [&'a dyn ArrayView1<T>], axis: u8) -> Self { fn concatenate_1d<'a>(arrays: &'a [&'a dyn ArrayView1<T>], axis: u8) -> Self {
assert!( assert!(
axis == 1 || axis == 0, axis == 1 || axis == 0,
@@ -1237,7 +1237,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
), ),
} }
} }
/// /// concatenate 2d array
fn concatenate_2d<'a>(arrays: &'a [&'a dyn ArrayView2<T>], axis: u8) -> Self { fn concatenate_2d<'a>(arrays: &'a [&'a dyn ArrayView2<T>], axis: u8) -> Self {
assert!( assert!(
axis == 1 || axis == 0, axis == 1 || axis == 0,
@@ -1294,7 +1294,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
} }
} }
} }
/// /// merge 1d arrays
fn merge_1d<'a>(&'a self, arrays: &'a [&'a dyn ArrayView1<T>], axis: u8, append: bool) -> Self { fn merge_1d<'a>(&'a self, arrays: &'a [&'a dyn ArrayView1<T>], axis: u8, append: bool) -> Self {
assert!( assert!(
axis == 1 || axis == 0, axis == 1 || axis == 0,
@@ -1362,7 +1362,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
} }
} }
} }
/// /// Stack arrays in sequence vertically
fn v_stack(&self, other: &dyn ArrayView2<T>) -> Self { fn v_stack(&self, other: &dyn ArrayView2<T>) -> Self {
let (nrows, ncols) = self.shape(); let (nrows, ncols) = self.shape();
let (other_nrows, other_ncols) = other.shape(); let (other_nrows, other_ncols) = other.shape();
@@ -1378,7 +1378,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
0, 0,
) )
} }
/// /// Stack arrays in sequence horizontally
fn h_stack(&self, other: &dyn ArrayView2<T>) -> Self { fn h_stack(&self, other: &dyn ArrayView2<T>) -> Self {
let (nrows, ncols) = self.shape(); let (nrows, ncols) = self.shape();
let (other_nrows, other_ncols) = other.shape(); let (other_nrows, other_ncols) = other.shape();
@@ -1394,20 +1394,20 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
1, 1,
) )
} }
/// /// map array values
fn map<O: Debug + Display + Copy + Sized, A: Array2<O>, F: FnMut(&T) -> O>(self, f: F) -> A { fn map<O: Debug + Display + Copy + Sized, A: Array2<O>, F: FnMut(&T) -> O>(self, f: F) -> A {
let (nrows, ncols) = self.shape(); let (nrows, ncols) = self.shape();
A::from_iterator(self.iterator(0).map(f), nrows, ncols, 0) A::from_iterator(self.iterator(0).map(f), nrows, ncols, 0)
} }
/// /// iter rows
fn row_iter<'a>(&'a self) -> Box<dyn Iterator<Item = Box<dyn ArrayView1<T> + 'a>> + 'a> { fn row_iter<'a>(&'a self) -> Box<dyn Iterator<Item = Box<dyn ArrayView1<T> + 'a>> + 'a> {
Box::new((0..self.shape().0).map(move |r| self.get_row(r))) Box::new((0..self.shape().0).map(move |r| self.get_row(r)))
} }
/// /// iter cols
fn col_iter<'a>(&'a self) -> Box<dyn Iterator<Item = Box<dyn ArrayView1<T> + 'a>> + 'a> { fn col_iter<'a>(&'a self) -> Box<dyn Iterator<Item = Box<dyn ArrayView1<T> + 'a>> + 'a> {
Box::new((0..self.shape().1).map(move |r| self.get_col(r))) Box::new((0..self.shape().1).map(move |r| self.get_col(r)))
} }
/// /// take elements from 2d array
fn take(&self, index: &[usize], axis: u8) -> Self { fn take(&self, index: &[usize], axis: u8) -> Self {
let (nrows, ncols) = self.shape(); let (nrows, ncols) = self.shape();
@@ -1447,7 +1447,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
fn take_column(&self, column_index: usize) -> Self { fn take_column(&self, column_index: usize) -> Self {
self.take(&[column_index], 1) self.take(&[column_index], 1)
} }
/// /// add a scalar to the array
fn add_scalar(&self, x: T) -> Self fn add_scalar(&self, x: T) -> Self
where where
T: Number, T: Number,
@@ -1456,7 +1456,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
result.add_scalar_mut(x); result.add_scalar_mut(x);
result result
} }
/// /// subtract a scalar from the array
fn sub_scalar(&self, x: T) -> Self fn sub_scalar(&self, x: T) -> Self
where where
T: Number, T: Number,
@@ -1465,7 +1465,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
result.sub_scalar_mut(x); result.sub_scalar_mut(x);
result result
} }
/// /// divide a scalar from the array
fn div_scalar(&self, x: T) -> Self fn div_scalar(&self, x: T) -> Self
where where
T: Number, T: Number,
@@ -1474,7 +1474,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
result.div_scalar_mut(x); result.div_scalar_mut(x);
result result
} }
/// /// multiply a scalar to the array
fn mul_scalar(&self, x: T) -> Self fn mul_scalar(&self, x: T) -> Self
where where
T: Number, T: Number,
@@ -1483,7 +1483,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
result.mul_scalar_mut(x); result.mul_scalar_mut(x);
result result
} }
/// /// sum of two arrays
fn add(&self, other: &dyn Array<T, (usize, usize)>) -> Self fn add(&self, other: &dyn Array<T, (usize, usize)>) -> Self
where where
T: Number, T: Number,
@@ -1492,7 +1492,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
result.add_mut(other); result.add_mut(other);
result result
} }
/// /// subtract two arrays
fn sub(&self, other: &dyn Array<T, (usize, usize)>) -> Self fn sub(&self, other: &dyn Array<T, (usize, usize)>) -> Self
where where
T: Number, T: Number,
@@ -1501,7 +1501,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
result.sub_mut(other); result.sub_mut(other);
result result
} }
/// /// multiply two arrays
fn mul(&self, other: &dyn Array<T, (usize, usize)>) -> Self fn mul(&self, other: &dyn Array<T, (usize, usize)>) -> Self
where where
T: Number, T: Number,
@@ -1510,7 +1510,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
result.mul_mut(other); result.mul_mut(other);
result result
} }
/// /// divide two arrays
fn div(&self, other: &dyn Array<T, (usize, usize)>) -> Self fn div(&self, other: &dyn Array<T, (usize, usize)>) -> Self
where where
T: Number, T: Number,
@@ -1519,7 +1519,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
result.div_mut(other); result.div_mut(other);
result result
} }
/// /// absolute values of the array
fn abs(&self) -> Self fn abs(&self) -> Self
where where
T: Number + Signed, T: Number + Signed,
@@ -1528,7 +1528,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
result.abs_mut(); result.abs_mut();
result result
} }
/// /// negation of the array
fn neg(&self) -> Self fn neg(&self) -> Self
where where
T: Number + Neg<Output = T>, T: Number + Neg<Output = T>,
@@ -1537,7 +1537,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
result.neg_mut(); result.neg_mut();
result result
} }
/// /// values at power `p`
fn pow(&self, p: T) -> Self fn pow(&self, p: T) -> Self
where where
T: RealNumber, T: RealNumber,
@@ -1575,7 +1575,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
} }
} }
/// appriximate equality of the elements of a matrix according to a given error /// approximate equality of the elements of a matrix according to a given error
fn approximate_eq(&self, other: &Self, error: T) -> bool fn approximate_eq(&self, other: &Self, error: T) -> bool
where where
T: Number + RealNumber, T: Number + RealNumber,
@@ -1631,8 +1631,8 @@ mod tests {
let v = vec![3., -2., 6.]; let v = vec![3., -2., 6.];
assert_eq!(v.norm(1.), 11.); assert_eq!(v.norm(1.), 11.);
assert_eq!(v.norm(2.), 7.); assert_eq!(v.norm(2.), 7.);
assert_eq!(v.norm(std::f64::INFINITY), 6.); assert_eq!(v.norm(f64::INFINITY), 6.);
assert_eq!(v.norm(std::f64::NEG_INFINITY), 2.); assert_eq!(v.norm(f64::NEG_INFINITY), 2.);
} }
#[test] #[test]
+12 -13
View File
@@ -91,7 +91,7 @@ impl<'a, T: Debug + Display + Copy + Sized> DenseMatrixView<'a, T> {
} }
} }
impl<'a, T: Debug + Display + Copy + Sized> fmt::Display for DenseMatrixView<'a, T> { impl<T: Debug + Display + Copy + Sized> fmt::Display for DenseMatrixView<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!( writeln!(
f, f,
@@ -142,7 +142,7 @@ impl<'a, T: Debug + Display + Copy + Sized> DenseMatrixMutView<'a, T> {
} }
} }
fn iter_mut<'b>(&'b mut self, axis: u8) -> Box<dyn Iterator<Item = &mut T> + 'b> { fn iter_mut<'b>(&'b mut self, axis: u8) -> Box<dyn Iterator<Item = &'b mut T> + 'b> {
let column_major = self.column_major; let column_major = self.column_major;
let stride = self.stride; let stride = self.stride;
let ptr = self.values.as_mut_ptr(); let ptr = self.values.as_mut_ptr();
@@ -169,7 +169,7 @@ impl<'a, T: Debug + Display + Copy + Sized> DenseMatrixMutView<'a, T> {
} }
} }
impl<'a, T: Debug + Display + Copy + Sized> fmt::Display for DenseMatrixMutView<'a, T> { impl<T: Debug + Display + Copy + Sized> fmt::Display for DenseMatrixMutView<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!( writeln!(
f, f,
@@ -493,7 +493,7 @@ impl<T: Number + RealNumber> EVDDecomposable<T> for DenseMatrix<T> {}
impl<T: Number + RealNumber> LUDecomposable<T> for DenseMatrix<T> {} impl<T: Number + RealNumber> LUDecomposable<T> for DenseMatrix<T> {}
impl<T: Number + RealNumber> SVDDecomposable<T> for DenseMatrix<T> {} impl<T: Number + RealNumber> SVDDecomposable<T> for DenseMatrix<T> {}
impl<'a, T: Debug + Display + Copy + Sized> Array<T, (usize, usize)> for DenseMatrixView<'a, T> { impl<T: Debug + Display + Copy + Sized> Array<T, (usize, usize)> for DenseMatrixView<'_, T> {
fn get(&self, pos: (usize, usize)) -> &T { fn get(&self, pos: (usize, usize)) -> &T {
if self.column_major { if self.column_major {
&self.values[pos.0 + pos.1 * self.stride] &self.values[pos.0 + pos.1 * self.stride]
@@ -515,7 +515,7 @@ impl<'a, T: Debug + Display + Copy + Sized> Array<T, (usize, usize)> for DenseMa
} }
} }
impl<'a, T: Debug + Display + Copy + Sized> Array<T, usize> for DenseMatrixView<'a, T> { impl<T: Debug + Display + Copy + Sized> Array<T, usize> for DenseMatrixView<'_, T> {
fn get(&self, i: usize) -> &T { fn get(&self, i: usize) -> &T {
if self.nrows == 1 { if self.nrows == 1 {
if self.column_major { if self.column_major {
@@ -553,11 +553,11 @@ impl<'a, T: Debug + Display + Copy + Sized> Array<T, usize> for DenseMatrixView<
} }
} }
impl<'a, T: Debug + Display + Copy + Sized> ArrayView2<T> for DenseMatrixView<'a, T> {} impl<T: Debug + Display + Copy + Sized> ArrayView2<T> for DenseMatrixView<'_, T> {}
impl<'a, T: Debug + Display + Copy + Sized> ArrayView1<T> for DenseMatrixView<'a, T> {} impl<T: Debug + Display + Copy + Sized> ArrayView1<T> for DenseMatrixView<'_, T> {}
impl<'a, T: Debug + Display + Copy + Sized> Array<T, (usize, usize)> for DenseMatrixMutView<'a, T> { impl<T: Debug + Display + Copy + Sized> Array<T, (usize, usize)> for DenseMatrixMutView<'_, T> {
fn get(&self, pos: (usize, usize)) -> &T { fn get(&self, pos: (usize, usize)) -> &T {
if self.column_major { if self.column_major {
&self.values[pos.0 + pos.1 * self.stride] &self.values[pos.0 + pos.1 * self.stride]
@@ -579,9 +579,7 @@ impl<'a, T: Debug + Display + Copy + Sized> Array<T, (usize, usize)> for DenseMa
} }
} }
impl<'a, T: Debug + Display + Copy + Sized> MutArray<T, (usize, usize)> impl<T: Debug + Display + Copy + Sized> MutArray<T, (usize, usize)> for DenseMatrixMutView<'_, T> {
for DenseMatrixMutView<'a, T>
{
fn set(&mut self, pos: (usize, usize), x: T) { fn set(&mut self, pos: (usize, usize), x: T) {
if self.column_major { if self.column_major {
self.values[pos.0 + pos.1 * self.stride] = x; self.values[pos.0 + pos.1 * self.stride] = x;
@@ -595,15 +593,16 @@ impl<'a, T: Debug + Display + Copy + Sized> MutArray<T, (usize, usize)>
} }
} }
impl<'a, T: Debug + Display + Copy + Sized> MutArrayView2<T> for DenseMatrixMutView<'a, T> {} impl<T: Debug + Display + Copy + Sized> MutArrayView2<T> for DenseMatrixMutView<'_, T> {}
impl<'a, T: Debug + Display + Copy + Sized> ArrayView2<T> for DenseMatrixMutView<'a, T> {} impl<T: Debug + Display + Copy + Sized> ArrayView2<T> for DenseMatrixMutView<'_, T> {}
impl<T: RealNumber> MatrixStats<T> for DenseMatrix<T> {} impl<T: RealNumber> MatrixStats<T> for DenseMatrix<T> {}
impl<T: RealNumber> MatrixPreprocessing<T> for DenseMatrix<T> {} impl<T: RealNumber> MatrixPreprocessing<T> for DenseMatrix<T> {}
#[cfg(test)] #[cfg(test)]
#[warn(clippy::reversed_empty_ranges)]
mod tests { mod tests {
use super::*; use super::*;
use approx::relative_eq; use approx::relative_eq;
+6 -6
View File
@@ -119,7 +119,7 @@ impl<T: Debug + Display + Copy + Sized> Array1<T> for Vec<T> {
} }
} }
impl<'a, T: Debug + Display + Copy + Sized> Array<T, usize> for VecMutView<'a, T> { impl<T: Debug + Display + Copy + Sized> Array<T, usize> for VecMutView<'_, T> {
fn get(&self, i: usize) -> &T { fn get(&self, i: usize) -> &T {
&self.ptr[i] &self.ptr[i]
} }
@@ -138,7 +138,7 @@ impl<'a, T: Debug + Display + Copy + Sized> Array<T, usize> for VecMutView<'a, T
} }
} }
impl<'a, T: Debug + Display + Copy + Sized> MutArray<T, usize> for VecMutView<'a, T> { impl<T: Debug + Display + Copy + Sized> MutArray<T, usize> for VecMutView<'_, T> {
fn set(&mut self, i: usize, x: T) { fn set(&mut self, i: usize, x: T) {
self.ptr[i] = x; self.ptr[i] = x;
} }
@@ -149,10 +149,10 @@ impl<'a, T: Debug + Display + Copy + Sized> MutArray<T, usize> for VecMutView<'a
} }
} }
impl<'a, T: Debug + Display + Copy + Sized> ArrayView1<T> for VecMutView<'a, T> {} impl<T: Debug + Display + Copy + Sized> ArrayView1<T> for VecMutView<'_, T> {}
impl<'a, T: Debug + Display + Copy + Sized> MutArrayView1<T> for VecMutView<'a, T> {} impl<T: Debug + Display + Copy + Sized> MutArrayView1<T> for VecMutView<'_, T> {}
impl<'a, T: Debug + Display + Copy + Sized> Array<T, usize> for VecView<'a, T> { impl<T: Debug + Display + Copy + Sized> Array<T, usize> for VecView<'_, T> {
fn get(&self, i: usize) -> &T { fn get(&self, i: usize) -> &T {
&self.ptr[i] &self.ptr[i]
} }
@@ -171,7 +171,7 @@ impl<'a, T: Debug + Display + Copy + Sized> Array<T, usize> for VecView<'a, T> {
} }
} }
impl<'a, T: Debug + Display + Copy + Sized> ArrayView1<T> for VecView<'a, T> {} impl<T: Debug + Display + Copy + Sized> ArrayView1<T> for VecView<'_, T> {}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
+6 -10
View File
@@ -68,7 +68,7 @@ impl<T: Debug + Display + Copy + Sized> ArrayView2<T> for ArrayBase<OwnedRepr<T>
impl<T: Debug + Display + Copy + Sized> MutArrayView2<T> for ArrayBase<OwnedRepr<T>, Ix2> {} impl<T: Debug + Display + Copy + Sized> MutArrayView2<T> for ArrayBase<OwnedRepr<T>, Ix2> {}
impl<'a, T: Debug + Display + Copy + Sized> BaseArray<T, (usize, usize)> for ArrayView<'a, T, Ix2> { impl<T: Debug + Display + Copy + Sized> BaseArray<T, (usize, usize)> for ArrayView<'_, T, Ix2> {
fn get(&self, pos: (usize, usize)) -> &T { fn get(&self, pos: (usize, usize)) -> &T {
&self[[pos.0, pos.1]] &self[[pos.0, pos.1]]
} }
@@ -144,11 +144,9 @@ impl<T: Number + RealNumber> EVDDecomposable<T> for ArrayBase<OwnedRepr<T>, Ix2>
impl<T: Number + RealNumber> LUDecomposable<T> for ArrayBase<OwnedRepr<T>, Ix2> {} impl<T: Number + RealNumber> LUDecomposable<T> for ArrayBase<OwnedRepr<T>, Ix2> {}
impl<T: Number + RealNumber> SVDDecomposable<T> for ArrayBase<OwnedRepr<T>, Ix2> {} impl<T: Number + RealNumber> SVDDecomposable<T> for ArrayBase<OwnedRepr<T>, Ix2> {}
impl<'a, T: Debug + Display + Copy + Sized> ArrayView2<T> for ArrayView<'a, T, Ix2> {} impl<T: Debug + Display + Copy + Sized> ArrayView2<T> for ArrayView<'_, T, Ix2> {}
impl<'a, T: Debug + Display + Copy + Sized> BaseArray<T, (usize, usize)> impl<T: Debug + Display + Copy + Sized> BaseArray<T, (usize, usize)> for ArrayViewMut<'_, T, Ix2> {
for ArrayViewMut<'a, T, Ix2>
{
fn get(&self, pos: (usize, usize)) -> &T { fn get(&self, pos: (usize, usize)) -> &T {
&self[[pos.0, pos.1]] &self[[pos.0, pos.1]]
} }
@@ -175,9 +173,7 @@ impl<'a, T: Debug + Display + Copy + Sized> BaseArray<T, (usize, usize)>
} }
} }
impl<'a, T: Debug + Display + Copy + Sized> MutArray<T, (usize, usize)> impl<T: Debug + Display + Copy + Sized> MutArray<T, (usize, usize)> for ArrayViewMut<'_, T, Ix2> {
for ArrayViewMut<'a, T, Ix2>
{
fn set(&mut self, pos: (usize, usize), x: T) { fn set(&mut self, pos: (usize, usize), x: T) {
self[[pos.0, pos.1]] = x self[[pos.0, pos.1]] = x
} }
@@ -195,9 +191,9 @@ impl<'a, T: Debug + Display + Copy + Sized> MutArray<T, (usize, usize)>
} }
} }
impl<'a, T: Debug + Display + Copy + Sized> MutArrayView2<T> for ArrayViewMut<'a, T, Ix2> {} impl<T: Debug + Display + Copy + Sized> MutArrayView2<T> for ArrayViewMut<'_, T, Ix2> {}
impl<'a, T: Debug + Display + Copy + Sized> ArrayView2<T> for ArrayViewMut<'a, T, Ix2> {} impl<T: Debug + Display + Copy + Sized> ArrayView2<T> for ArrayViewMut<'_, T, Ix2> {}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
+6 -6
View File
@@ -41,7 +41,7 @@ impl<T: Debug + Display + Copy + Sized> ArrayView1<T> for ArrayBase<OwnedRepr<T>
impl<T: Debug + Display + Copy + Sized> MutArrayView1<T> for ArrayBase<OwnedRepr<T>, Ix1> {} impl<T: Debug + Display + Copy + Sized> MutArrayView1<T> for ArrayBase<OwnedRepr<T>, Ix1> {}
impl<'a, T: Debug + Display + Copy + Sized> BaseArray<T, usize> for ArrayView<'a, T, Ix1> { impl<T: Debug + Display + Copy + Sized> BaseArray<T, usize> for ArrayView<'_, T, Ix1> {
fn get(&self, i: usize) -> &T { fn get(&self, i: usize) -> &T {
&self[i] &self[i]
} }
@@ -60,9 +60,9 @@ impl<'a, T: Debug + Display + Copy + Sized> BaseArray<T, usize> for ArrayView<'a
} }
} }
impl<'a, T: Debug + Display + Copy + Sized> ArrayView1<T> for ArrayView<'a, T, Ix1> {} impl<T: Debug + Display + Copy + Sized> ArrayView1<T> for ArrayView<'_, T, Ix1> {}
impl<'a, T: Debug + Display + Copy + Sized> BaseArray<T, usize> for ArrayViewMut<'a, T, Ix1> { impl<T: Debug + Display + Copy + Sized> BaseArray<T, usize> for ArrayViewMut<'_, T, Ix1> {
fn get(&self, i: usize) -> &T { fn get(&self, i: usize) -> &T {
&self[i] &self[i]
} }
@@ -81,7 +81,7 @@ impl<'a, T: Debug + Display + Copy + Sized> BaseArray<T, usize> for ArrayViewMut
} }
} }
impl<'a, T: Debug + Display + Copy + Sized> MutArray<T, usize> for ArrayViewMut<'a, T, Ix1> { impl<T: Debug + Display + Copy + Sized> MutArray<T, usize> for ArrayViewMut<'_, T, Ix1> {
fn set(&mut self, i: usize, x: T) { fn set(&mut self, i: usize, x: T) {
self[i] = x; self[i] = x;
} }
@@ -92,8 +92,8 @@ impl<'a, T: Debug + Display + Copy + Sized> MutArray<T, usize> for ArrayViewMut<
} }
} }
impl<'a, T: Debug + Display + Copy + Sized> ArrayView1<T> for ArrayViewMut<'a, T, Ix1> {} impl<T: Debug + Display + Copy + Sized> ArrayView1<T> for ArrayViewMut<'_, T, Ix1> {}
impl<'a, T: Debug + Display + Copy + Sized> MutArrayView1<T> for ArrayViewMut<'a, T, Ix1> {} impl<T: Debug + Display + Copy + Sized> MutArrayView1<T> for ArrayViewMut<'_, T, Ix1> {}
impl<T: Debug + Display + Copy + Sized> Array1<T> for ArrayBase<OwnedRepr<T>, Ix1> { impl<T: Debug + Display + Copy + Sized> Array1<T> for ArrayBase<OwnedRepr<T>, Ix1> {
fn slice<'a>(&'a self, range: Range<usize>) -> Box<dyn ArrayView1<T> + 'a> { fn slice<'a>(&'a self, range: Range<usize>) -> Box<dyn ArrayView1<T> + 'a> {
+2 -2
View File
@@ -841,7 +841,7 @@ mod tests {
)); ));
for (i, eigen_values_i) in eigen_values.iter().enumerate() { for (i, eigen_values_i) in eigen_values.iter().enumerate() {
assert!((eigen_values_i - evd.d[i]).abs() < 1e-4); assert!((eigen_values_i - evd.d[i]).abs() < 1e-4);
assert!((0f64 - evd.e[i]).abs() < std::f64::EPSILON); assert!((0f64 - evd.e[i]).abs() < f64::EPSILON);
} }
} }
#[cfg_attr( #[cfg_attr(
@@ -875,7 +875,7 @@ mod tests {
)); ));
for (i, eigen_values_i) in eigen_values.iter().enumerate() { for (i, eigen_values_i) in eigen_values.iter().enumerate() {
assert!((eigen_values_i - evd.d[i]).abs() < 1e-4); assert!((eigen_values_i - evd.d[i]).abs() < 1e-4);
assert!((0f64 - evd.e[i]).abs() < std::f64::EPSILON); assert!((0f64 - evd.e[i]).abs() < f64::EPSILON);
} }
} }
#[cfg_attr( #[cfg_attr(
+2 -3
View File
@@ -142,7 +142,6 @@ pub trait MatrixPreprocessing<T: RealNumber>: MutArrayView2<T> + Clone {
/// ///
/// assert_eq!(a, expected); /// assert_eq!(a, expected);
/// ``` /// ```
fn binarize_mut(&mut self, threshold: T) { fn binarize_mut(&mut self, threshold: T) {
let (nrows, ncols) = self.shape(); let (nrows, ncols) = self.shape();
for row in 0..nrows { for row in 0..nrows {
@@ -217,8 +216,8 @@ mod tests {
let expected_0 = vec![0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]; let expected_0 = vec![0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
let expected_1 = vec![1.25, 1.25]; let expected_1 = vec![1.25, 1.25];
assert!(m.var(0).approximate_eq(&expected_0, std::f64::EPSILON)); assert!(m.var(0).approximate_eq(&expected_0, f64::EPSILON));
assert!(m.var(1).approximate_eq(&expected_1, std::f64::EPSILON)); assert!(m.var(1).approximate_eq(&expected_1, f64::EPSILON));
assert_eq!( assert_eq!(
m.mean(0), m.mean(0),
vec![0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25] vec![0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25]
+1 -3
View File
@@ -48,11 +48,9 @@ pub struct SVD<T: Number + RealNumber, M: SVDDecomposable<T>> {
pub V: M, pub V: M,
/// Singular values of the original matrix /// Singular values of the original matrix
pub s: Vec<T>, pub s: Vec<T>,
///
m: usize, m: usize,
///
n: usize, n: usize,
/// /// Tolerance
tol: T, tol: T,
} }
+4 -4
View File
@@ -27,9 +27,9 @@ use crate::error::Failed;
use crate::linalg::basic::arrays::{Array, Array1, Array2, ArrayView1, MutArrayView1}; use crate::linalg::basic::arrays::{Array, Array1, Array2, ArrayView1, MutArrayView1};
use crate::numbers::floatnum::FloatNumber; use crate::numbers::floatnum::FloatNumber;
/// /// Trait for Biconjugate Gradient Solver
pub trait BiconjugateGradientSolver<'a, T: FloatNumber, X: Array2<T>> { pub trait BiconjugateGradientSolver<'a, T: FloatNumber, X: Array2<T>> {
/// /// Solve Ax = b
fn solve_mut( fn solve_mut(
&self, &self,
a: &'a X, a: &'a X,
@@ -109,7 +109,7 @@ pub trait BiconjugateGradientSolver<'a, T: FloatNumber, X: Array2<T>> {
Ok(err) Ok(err)
} }
/// /// solve preconditioner
fn solve_preconditioner(&self, a: &'a X, b: &[T], x: &mut [T]) { fn solve_preconditioner(&self, a: &'a X, b: &[T], x: &mut [T]) {
let diag = Self::diag(a); let diag = Self::diag(a);
let n = diag.len(); let n = diag.len();
@@ -133,7 +133,7 @@ pub trait BiconjugateGradientSolver<'a, T: FloatNumber, X: Array2<T>> {
y.copy_from(&x.xa(true, a)); y.copy_from(&x.xa(true, a));
} }
/// /// Extract the diagonal from a matrix
fn diag(a: &X) -> Vec<T> { fn diag(a: &X) -> Vec<T> {
let (nrows, ncols) = a.shape(); let (nrows, ncols) = a.shape();
let n = nrows.min(ncols); let n = nrows.min(ncols);
+4 -10
View File
@@ -16,7 +16,7 @@ use crate::linalg::basic::arrays::{Array1, Array2, ArrayView1, MutArray, MutArra
use crate::linear::bg_solver::BiconjugateGradientSolver; use crate::linear::bg_solver::BiconjugateGradientSolver;
use crate::numbers::floatnum::FloatNumber; use crate::numbers::floatnum::FloatNumber;
/// /// Interior Point Optimizer
pub struct InteriorPointOptimizer<T: FloatNumber, X: Array2<T>> { pub struct InteriorPointOptimizer<T: FloatNumber, X: Array2<T>> {
ata: X, ata: X,
d1: Vec<T>, d1: Vec<T>,
@@ -25,9 +25,8 @@ pub struct InteriorPointOptimizer<T: FloatNumber, X: Array2<T>> {
prs: Vec<T>, prs: Vec<T>,
} }
///
impl<T: FloatNumber, X: Array2<T>> InteriorPointOptimizer<T, X> { impl<T: FloatNumber, X: Array2<T>> InteriorPointOptimizer<T, X> {
/// /// Initialize a new Interior Point Optimizer
pub fn new(a: &X, n: usize) -> InteriorPointOptimizer<T, X> { pub fn new(a: &X, n: usize) -> InteriorPointOptimizer<T, X> {
InteriorPointOptimizer { InteriorPointOptimizer {
ata: a.ab(true, a, false), ata: a.ab(true, a, false),
@@ -38,7 +37,7 @@ impl<T: FloatNumber, X: Array2<T>> InteriorPointOptimizer<T, X> {
} }
} }
/// /// Run the optimization
pub fn optimize( pub fn optimize(
&mut self, &mut self,
x: &X, x: &X,
@@ -101,7 +100,7 @@ impl<T: FloatNumber, X: Array2<T>> InteriorPointOptimizer<T, X> {
// CALCULATE DUALITY GAP // CALCULATE DUALITY GAP
let xnu = nu.xa(false, x); let xnu = nu.xa(false, x);
let max_xnu = xnu.norm(std::f64::INFINITY); let max_xnu = xnu.norm(f64::INFINITY);
if max_xnu > lambda_f64 { if max_xnu > lambda_f64 {
let lnu = T::from_f64(lambda_f64 / max_xnu).unwrap(); let lnu = T::from_f64(lambda_f64 / max_xnu).unwrap();
nu.mul_scalar_mut(lnu); nu.mul_scalar_mut(lnu);
@@ -208,7 +207,6 @@ impl<T: FloatNumber, X: Array2<T>> InteriorPointOptimizer<T, X> {
Ok(w) Ok(w)
} }
///
fn sumlogneg(f: &X) -> T { fn sumlogneg(f: &X) -> T {
let (n, _) = f.shape(); let (n, _) = f.shape();
let mut sum = T::zero(); let mut sum = T::zero();
@@ -220,11 +218,9 @@ impl<T: FloatNumber, X: Array2<T>> InteriorPointOptimizer<T, X> {
} }
} }
///
impl<'a, T: FloatNumber, X: Array2<T>> BiconjugateGradientSolver<'a, T, X> impl<'a, T: FloatNumber, X: Array2<T>> BiconjugateGradientSolver<'a, T, X>
for InteriorPointOptimizer<T, X> for InteriorPointOptimizer<T, X>
{ {
///
fn solve_preconditioner(&self, a: &'a X, b: &[T], x: &mut [T]) { fn solve_preconditioner(&self, a: &'a X, b: &[T], x: &mut [T]) {
let (_, p) = a.shape(); let (_, p) = a.shape();
@@ -234,7 +230,6 @@ impl<'a, T: FloatNumber, X: Array2<T>> BiconjugateGradientSolver<'a, T, X>
} }
} }
///
fn mat_vec_mul(&self, _: &X, x: &Vec<T>, y: &mut Vec<T>) { fn mat_vec_mul(&self, _: &X, x: &Vec<T>, y: &mut Vec<T>) {
let (_, p) = self.ata.shape(); let (_, p) = self.ata.shape();
let x_slice = Vec::from_slice(x.slice(0..p).as_ref()); let x_slice = Vec::from_slice(x.slice(0..p).as_ref());
@@ -246,7 +241,6 @@ impl<'a, T: FloatNumber, X: Array2<T>> BiconjugateGradientSolver<'a, T, X>
} }
} }
///
fn mat_t_vec_mul(&self, a: &X, x: &Vec<T>, y: &mut Vec<T>) { fn mat_t_vec_mul(&self, a: &X, x: &Vec<T>, y: &mut Vec<T>) {
self.mat_vec_mul(a, x, y); self.mat_vec_mul(a, x, y);
} }
+13 -16
View File
@@ -183,14 +183,11 @@ pub struct LogisticRegression<
} }
trait ObjectiveFunction<T: Number + FloatNumber, X: Array2<T>> { trait ObjectiveFunction<T: Number + FloatNumber, X: Array2<T>> {
///
fn f(&self, w_bias: &[T]) -> T; fn f(&self, w_bias: &[T]) -> T;
///
#[allow(clippy::ptr_arg)] #[allow(clippy::ptr_arg)]
fn df(&self, g: &mut Vec<T>, w_bias: &Vec<T>); fn df(&self, g: &mut Vec<T>, w_bias: &Vec<T>);
///
#[allow(clippy::ptr_arg)] #[allow(clippy::ptr_arg)]
fn partial_dot(w: &[T], x: &X, v_col: usize, m_row: usize) -> T { fn partial_dot(w: &[T], x: &X, v_col: usize, m_row: usize) -> T {
let mut sum = T::zero(); let mut sum = T::zero();
@@ -261,8 +258,8 @@ impl<TX: Number + FloatNumber + RealNumber, TY: Number + Ord, X: Array2<TX>, Y:
} }
} }
impl<'a, T: Number + FloatNumber, X: Array2<T>> ObjectiveFunction<T, X> impl<T: Number + FloatNumber, X: Array2<T>> ObjectiveFunction<T, X>
for BinaryObjectiveFunction<'a, T, X> for BinaryObjectiveFunction<'_, T, X>
{ {
fn f(&self, w_bias: &[T]) -> T { fn f(&self, w_bias: &[T]) -> T {
let mut f = T::zero(); let mut f = T::zero();
@@ -316,8 +313,8 @@ struct MultiClassObjectiveFunction<'a, T: Number + FloatNumber, X: Array2<T>> {
_phantom_t: PhantomData<T>, _phantom_t: PhantomData<T>,
} }
impl<'a, T: Number + FloatNumber + RealNumber, X: Array2<T>> ObjectiveFunction<T, X> impl<T: Number + FloatNumber + RealNumber, X: Array2<T>> ObjectiveFunction<T, X>
for MultiClassObjectiveFunction<'a, T, X> for MultiClassObjectiveFunction<'_, T, X>
{ {
fn f(&self, w_bias: &[T]) -> T { fn f(&self, w_bias: &[T]) -> T {
let mut f = T::zero(); let mut f = T::zero();
@@ -629,11 +626,11 @@ mod tests {
objective.df(&mut g, &vec![1., 2., 3., 4., 5., 6., 7., 8., 9.]); objective.df(&mut g, &vec![1., 2., 3., 4., 5., 6., 7., 8., 9.]);
objective.df(&mut g, &vec![1., 2., 3., 4., 5., 6., 7., 8., 9.]); objective.df(&mut g, &vec![1., 2., 3., 4., 5., 6., 7., 8., 9.]);
assert!((g[0] + 33.000068218163484).abs() < std::f64::EPSILON); assert!((g[0] + 33.000068218163484).abs() < f64::EPSILON);
let f = objective.f(&[1., 2., 3., 4., 5., 6., 7., 8., 9.]); let f = objective.f(&[1., 2., 3., 4., 5., 6., 7., 8., 9.]);
assert!((f - 408.0052230582765).abs() < std::f64::EPSILON); assert!((f - 408.0052230582765).abs() < f64::EPSILON);
let objective_reg = MultiClassObjectiveFunction { let objective_reg = MultiClassObjectiveFunction {
x: &x, x: &x,
@@ -689,13 +686,13 @@ mod tests {
objective.df(&mut g, &vec![1., 2., 3.]); objective.df(&mut g, &vec![1., 2., 3.]);
objective.df(&mut g, &vec![1., 2., 3.]); objective.df(&mut g, &vec![1., 2., 3.]);
assert!((g[0] - 26.051064349381285).abs() < std::f64::EPSILON); assert!((g[0] - 26.051064349381285).abs() < f64::EPSILON);
assert!((g[1] - 10.239000702928523).abs() < std::f64::EPSILON); assert!((g[1] - 10.239000702928523).abs() < f64::EPSILON);
assert!((g[2] - 3.869294270156324).abs() < std::f64::EPSILON); assert!((g[2] - 3.869294270156324).abs() < f64::EPSILON);
let f = objective.f(&[1., 2., 3.]); let f = objective.f(&[1., 2., 3.]);
assert!((f - 59.76994756647412).abs() < std::f64::EPSILON); assert!((f - 59.76994756647412).abs() < f64::EPSILON);
let objective_reg = BinaryObjectiveFunction { let objective_reg = BinaryObjectiveFunction {
x: &x, x: &x,
@@ -916,7 +913,7 @@ mod tests {
let x: DenseMatrix<f32> = DenseMatrix::rand(52181, 94); let x: DenseMatrix<f32> = DenseMatrix::rand(52181, 94);
let y1: Vec<i32> = vec![1; 2181]; let y1: Vec<i32> = vec![1; 2181];
let y2: Vec<i32> = vec![0; 50000]; let y2: Vec<i32> = vec![0; 50000];
let y: Vec<i32> = y1.into_iter().chain(y2.into_iter()).collect(); let y: Vec<i32> = y1.into_iter().chain(y2).collect();
let lr = LogisticRegression::fit(&x, &y, Default::default()).unwrap(); let lr = LogisticRegression::fit(&x, &y, Default::default()).unwrap();
let lr_reg = LogisticRegression::fit( let lr_reg = LogisticRegression::fit(
@@ -938,12 +935,12 @@ mod tests {
let x: &DenseMatrix<f64> = &DenseMatrix::rand(52181, 94); let x: &DenseMatrix<f64> = &DenseMatrix::rand(52181, 94);
let y1: Vec<u32> = vec![1; 2181]; let y1: Vec<u32> = vec![1; 2181];
let y2: Vec<u32> = vec![0; 50000]; let y2: Vec<u32> = vec![0; 50000];
let y: &Vec<u32> = &(y1.into_iter().chain(y2.into_iter()).collect()); let y: &Vec<u32> = &(y1.into_iter().chain(y2).collect());
println!("y vec height: {:?}", y.len()); println!("y vec height: {:?}", y.len());
println!("x matrix shape: {:?}", x.shape()); println!("x matrix shape: {:?}", x.shape());
let lr = LogisticRegression::fit(x, y, Default::default()).unwrap(); let lr = LogisticRegression::fit(x, y, Default::default()).unwrap();
let y_hat = lr.predict(&x).unwrap(); let y_hat = lr.predict(x).unwrap();
println!("y_hat shape: {:?}", y_hat.shape()); println!("y_hat shape: {:?}", y_hat.shape());
+4 -3
View File
@@ -258,7 +258,7 @@ impl<TY: Number + Ord + Unsigned> BernoulliNBDistribution<TY> {
/// * `x` - training data. /// * `x` - training data.
/// * `y` - vector with target values (classes) of length N. /// * `y` - vector with target values (classes) of length N.
/// * `priors` - Optional vector with prior probabilities of the classes. If not defined, /// * `priors` - Optional vector with prior probabilities of the classes. If not defined,
/// priors are adjusted according to the data. /// priors are adjusted according to the data.
/// * `alpha` - Additive (Laplace/Lidstone) smoothing parameter. /// * `alpha` - Additive (Laplace/Lidstone) smoothing parameter.
/// * `binarize` - Threshold for binarizing. /// * `binarize` - Threshold for binarizing.
fn fit<TX: Number + PartialOrd, X: Array2<TX>, Y: Array1<TY>>( fn fit<TX: Number + PartialOrd, X: Array2<TX>, Y: Array1<TY>>(
@@ -402,10 +402,10 @@ impl<TX: Number + PartialOrd, TY: Number + Ord + Unsigned, X: Array2<TX>, Y: Arr
{ {
/// Fits BernoulliNB with given data /// Fits BernoulliNB with given data
/// * `x` - training data of size NxM where N is the number of samples and M is the number of /// * `x` - training data of size NxM where N is the number of samples and M is the number of
/// features. /// features.
/// * `y` - vector with target values (classes) of length N. /// * `y` - vector with target values (classes) of length N.
/// * `parameters` - additional parameters like class priors, alpha for smoothing and /// * `parameters` - additional parameters like class priors, alpha for smoothing and
/// binarizing threshold. /// binarizing threshold.
pub fn fit(x: &X, y: &Y, parameters: BernoulliNBParameters<TX>) -> Result<Self, Failed> { pub fn fit(x: &X, y: &Y, parameters: BernoulliNBParameters<TX>) -> Result<Self, Failed> {
let distribution = if let Some(threshold) = parameters.binarize { let distribution = if let Some(threshold) = parameters.binarize {
BernoulliNBDistribution::fit( BernoulliNBDistribution::fit(
@@ -427,6 +427,7 @@ impl<TX: Number + PartialOrd, TY: Number + Ord + Unsigned, X: Array2<TX>, Y: Arr
/// Estimates the class labels for the provided data. /// Estimates the class labels for the provided data.
/// * `x` - data of shape NxM where N is number of data points to estimate and M is number of features. /// * `x` - data of shape NxM where N is number of data points to estimate and M is number of features.
///
/// Returns a vector of size N with class estimates. /// Returns a vector of size N with class estimates.
pub fn predict(&self, x: &X) -> Result<Y, Failed> { pub fn predict(&self, x: &X) -> Result<Y, Failed> {
if let Some(threshold) = self.binarize { if let Some(threshold) = self.binarize {
+3 -2
View File
@@ -95,7 +95,7 @@ impl<T: Number + Unsigned> PartialEq for CategoricalNBDistribution<T> {
return false; return false;
} }
for (a_i_j, b_i_j) in a_i.iter().zip(b_i.iter()) { for (a_i_j, b_i_j) in a_i.iter().zip(b_i.iter()) {
if (*a_i_j - *b_i_j).abs() > std::f64::EPSILON { if (*a_i_j - *b_i_j).abs() > f64::EPSILON {
return false; return false;
} }
} }
@@ -363,7 +363,7 @@ impl<T: Number + Unsigned, X: Array2<T>, Y: Array1<T>> Predictor<X, Y> for Categ
impl<T: Number + Unsigned, X: Array2<T>, Y: Array1<T>> CategoricalNB<T, X, Y> { impl<T: Number + Unsigned, X: Array2<T>, Y: Array1<T>> CategoricalNB<T, X, Y> {
/// Fits CategoricalNB with given data /// Fits CategoricalNB with given data
/// * `x` - training data of size NxM where N is the number of samples and M is the number of /// * `x` - training data of size NxM where N is the number of samples and M is the number of
/// features. /// features.
/// * `y` - vector with target values (classes) of length N. /// * `y` - vector with target values (classes) of length N.
/// * `parameters` - additional parameters like alpha for smoothing /// * `parameters` - additional parameters like alpha for smoothing
pub fn fit(x: &X, y: &Y, parameters: CategoricalNBParameters) -> Result<Self, Failed> { pub fn fit(x: &X, y: &Y, parameters: CategoricalNBParameters) -> Result<Self, Failed> {
@@ -375,6 +375,7 @@ impl<T: Number + Unsigned, X: Array2<T>, Y: Array1<T>> CategoricalNB<T, X, Y> {
/// Estimates the class labels for the provided data. /// Estimates the class labels for the provided data.
/// * `x` - data of shape NxM where N is number of data points to estimate and M is number of features. /// * `x` - data of shape NxM where N is number of data points to estimate and M is number of features.
///
/// Returns a vector of size N with class estimates. /// Returns a vector of size N with class estimates.
pub fn predict(&self, x: &X) -> Result<Y, Failed> { pub fn predict(&self, x: &X) -> Result<Y, Failed> {
self.inner.as_ref().unwrap().predict(x) self.inner.as_ref().unwrap().predict(x)
+3 -2
View File
@@ -175,7 +175,7 @@ impl<TY: Number + Ord + Unsigned> GaussianNBDistribution<TY> {
/// * `x` - training data. /// * `x` - training data.
/// * `y` - vector with target values (classes) of length N. /// * `y` - vector with target values (classes) of length N.
/// * `priors` - Optional vector with prior probabilities of the classes. If not defined, /// * `priors` - Optional vector with prior probabilities of the classes. If not defined,
/// priors are adjusted according to the data. /// priors are adjusted according to the data.
pub fn fit<TX: Number + RealNumber, X: Array2<TX>, Y: Array1<TY>>( pub fn fit<TX: Number + RealNumber, X: Array2<TX>, Y: Array1<TY>>(
x: &X, x: &X,
y: &Y, y: &Y,
@@ -317,7 +317,7 @@ impl<TX: Number + RealNumber, TY: Number + Ord + Unsigned, X: Array2<TX>, Y: Arr
{ {
/// Fits GaussianNB with given data /// Fits GaussianNB with given data
/// * `x` - training data of size NxM where N is the number of samples and M is the number of /// * `x` - training data of size NxM where N is the number of samples and M is the number of
/// features. /// features.
/// * `y` - vector with target values (classes) of length N. /// * `y` - vector with target values (classes) of length N.
/// * `parameters` - additional parameters like class priors. /// * `parameters` - additional parameters like class priors.
pub fn fit(x: &X, y: &Y, parameters: GaussianNBParameters) -> Result<Self, Failed> { pub fn fit(x: &X, y: &Y, parameters: GaussianNBParameters) -> Result<Self, Failed> {
@@ -328,6 +328,7 @@ impl<TX: Number + RealNumber, TY: Number + Ord + Unsigned, X: Array2<TX>, Y: Arr
/// Estimates the class labels for the provided data. /// Estimates the class labels for the provided data.
/// * `x` - data of shape NxM where N is number of data points to estimate and M is number of features. /// * `x` - data of shape NxM where N is number of data points to estimate and M is number of features.
///
/// Returns a vector of size N with class estimates. /// Returns a vector of size N with class estimates.
pub fn predict(&self, x: &X) -> Result<Y, Failed> { pub fn predict(&self, x: &X) -> Result<Y, Failed> {
self.inner.as_ref().unwrap().predict(x) self.inner.as_ref().unwrap().predict(x)
+477 -39
View File
@@ -40,7 +40,7 @@ use crate::linalg::basic::arrays::{Array1, Array2, ArrayView1};
use crate::numbers::basenum::Number; use crate::numbers::basenum::Number;
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{cmp::Ordering, marker::PhantomData}; use std::marker::PhantomData;
/// Distribution used in the Naive Bayes classifier. /// Distribution used in the Naive Bayes classifier.
pub(crate) trait NBDistribution<X: Number, Y: Number>: Clone { pub(crate) trait NBDistribution<X: Number, Y: Number>: Clone {
@@ -89,44 +89,45 @@ impl<TX: Number, TY: Number, X: Array2<TX>, Y: Array1<TY>, D: NBDistribution<TX,
/// Estimates the class labels for the provided data. /// Estimates the class labels for the provided data.
/// * `x` - data of shape NxM where N is number of data points to estimate and M is number of features. /// * `x` - data of shape NxM where N is number of data points to estimate and M is number of features.
///
/// Returns a vector of size N with class estimates. /// Returns a vector of size N with class estimates.
pub fn predict(&self, x: &X) -> Result<Y, Failed> { pub fn predict(&self, x: &X) -> Result<Y, Failed> {
let y_classes = self.distribution.classes(); let y_classes = self.distribution.classes();
let predictions = x
.row_iter() if y_classes.is_empty() {
.map(|row| { return Err(Failed::predict("Failed to predict, no classes available"));
y_classes }
.iter()
.enumerate() let (rows, _) = x.shape();
.map(|(class_index, class)| { let mut predictions = Vec::with_capacity(rows);
( let mut all_probs_nan = true;
class,
self.distribution.log_likelihood(class_index, &row) for row_index in 0..rows {
+ self.distribution.prior(class_index).ln(), let row = x.get_row(row_index);
) let mut max_log_prob = f64::NEG_INFINITY;
}) let mut max_class = None;
// For some reason, the max_by method cannot use NaNs for finding the maximum value, it panics.
// NaN must be considered as minimum values, for (class_index, class) in y_classes.iter().enumerate() {
// therefore it's like NaNs would not be considered for choosing the maximum value. let log_likelihood = self.distribution.log_likelihood(class_index, &row);
// So we need to handle this case for avoiding panicking by using `Option::unwrap`. let log_prob = log_likelihood + self.distribution.prior(class_index).ln();
.max_by(|(_, p1), (_, p2)| match p1.partial_cmp(p2) {
Some(ordering) => ordering, if !log_prob.is_nan() && log_prob > max_log_prob {
None => { max_log_prob = log_prob;
if p1.is_nan() { max_class = Some(*class);
Ordering::Less all_probs_nan = false;
} else if p2.is_nan() { }
Ordering::Greater }
} else {
Ordering::Equal predictions.push(max_class.unwrap_or(y_classes[0]));
} }
}
}) if all_probs_nan {
.map(|(prediction, _probability)| *prediction) Err(Failed::predict(
.ok_or_else(|| Failed::predict("Failed to predict, there is no result")) "Failed to predict, all probabilities were NaN",
}) ))
.collect::<Result<Vec<TY>, Failed>>()?; } else {
let y_hat = Y::from_vec_slice(&predictions); Ok(Y::from_vec_slice(&predictions))
Ok(y_hat) }
} }
} }
pub mod bernoulli; pub mod bernoulli;
@@ -146,7 +147,7 @@ mod tests {
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
struct TestDistribution<'d>(&'d Vec<i32>); struct TestDistribution<'d>(&'d Vec<i32>);
impl<'d> NBDistribution<i32, i32> for TestDistribution<'d> { impl NBDistribution<i32, i32> for TestDistribution<'_> {
fn prior(&self, _class_index: usize) -> f64 { fn prior(&self, _class_index: usize) -> f64 {
1. 1.
} }
@@ -163,7 +164,7 @@ mod tests {
} }
fn classes(&self) -> &Vec<i32> { fn classes(&self) -> &Vec<i32> {
&self.0 self.0
} }
} }
@@ -176,7 +177,7 @@ mod tests {
Ok(_) => panic!("Should return error in case of empty classes"), Ok(_) => panic!("Should return error in case of empty classes"),
Err(err) => assert_eq!( Err(err) => assert_eq!(
err.to_string(), err.to_string(),
"Predict failed: Failed to predict, there is no result" "Predict failed: Failed to predict, no classes available"
), ),
} }
@@ -192,4 +193,441 @@ mod tests {
Err(_) => panic!("Should success in normal case without NaNs"), Err(_) => panic!("Should success in normal case without NaNs"),
} }
} }
// A simple test distribution using float
#[derive(Debug, PartialEq, Clone)]
struct TestDistributionAgain {
classes: Vec<u32>,
probs: Vec<f64>,
}
impl NBDistribution<f64, u32> for TestDistributionAgain {
fn classes(&self) -> &Vec<u32> {
&self.classes
}
fn prior(&self, class_index: usize) -> f64 {
self.probs[class_index]
}
fn log_likelihood<'a>(
&'a self,
class_index: usize,
_j: &'a Box<dyn ArrayView1<f64> + 'a>,
) -> f64 {
self.probs[class_index].ln()
}
}
type TestNB = BaseNaiveBayes<f64, u32, DenseMatrix<f64>, Vec<u32>, TestDistributionAgain>;
#[test]
fn test_predict_empty_classes() {
let dist = TestDistributionAgain {
classes: vec![],
probs: vec![],
};
let nb = TestNB::fit(dist).unwrap();
let x = DenseMatrix::from_2d_array(&[&[1.0, 2.0], &[3.0, 4.0]]).unwrap();
assert!(nb.predict(&x).is_err());
}
#[test]
fn test_predict_single_class() {
let dist = TestDistributionAgain {
classes: vec![1],
probs: vec![1.0],
};
let nb = TestNB::fit(dist).unwrap();
let x = DenseMatrix::from_2d_array(&[&[1.0, 2.0], &[3.0, 4.0]]).unwrap();
let result = nb.predict(&x).unwrap();
assert_eq!(result, vec![1, 1]);
}
#[test]
fn test_predict_multiple_classes() {
let dist = TestDistributionAgain {
classes: vec![1, 2, 3],
probs: vec![0.2, 0.5, 0.3],
};
let nb = TestNB::fit(dist).unwrap();
let x = DenseMatrix::from_2d_array(&[&[1.0, 2.0], &[3.0, 4.0], &[5.0, 6.0]]).unwrap();
let result = nb.predict(&x).unwrap();
assert_eq!(result, vec![2, 2, 2]);
}
#[test]
fn test_predict_with_nans() {
let dist = TestDistributionAgain {
classes: vec![1, 2],
probs: vec![f64::NAN, 0.5],
};
let nb = TestNB::fit(dist).unwrap();
let x = DenseMatrix::from_2d_array(&[&[1.0, 2.0], &[3.0, 4.0]]).unwrap();
let result = nb.predict(&x).unwrap();
assert_eq!(result, vec![2, 2]);
}
#[test]
fn test_predict_all_nans() {
let dist = TestDistributionAgain {
classes: vec![1, 2],
probs: vec![f64::NAN, f64::NAN],
};
let nb = TestNB::fit(dist).unwrap();
let x = DenseMatrix::from_2d_array(&[&[1.0, 2.0], &[3.0, 4.0]]).unwrap();
assert!(nb.predict(&x).is_err());
}
#[test]
fn test_predict_extreme_probabilities() {
let dist = TestDistributionAgain {
classes: vec![1, 2],
probs: vec![1e-300, 1e-301],
};
let nb = TestNB::fit(dist).unwrap();
let x = DenseMatrix::from_2d_array(&[&[1.0, 2.0], &[3.0, 4.0]]).unwrap();
let result = nb.predict(&x).unwrap();
assert_eq!(result, vec![1, 1]);
}
#[test]
fn test_predict_with_infinity() {
let dist = TestDistributionAgain {
classes: vec![1, 2, 3],
probs: vec![f64::INFINITY, 1.0, 2.0],
};
let nb = TestNB::fit(dist).unwrap();
let x = DenseMatrix::from_2d_array(&[&[1.0, 2.0], &[3.0, 4.0]]).unwrap();
let result = nb.predict(&x).unwrap();
assert_eq!(result, vec![1, 1]);
}
#[test]
fn test_predict_with_negative_infinity() {
let dist = TestDistributionAgain {
classes: vec![1, 2, 3],
probs: vec![f64::NEG_INFINITY, 1.0, 2.0],
};
let nb = TestNB::fit(dist).unwrap();
let x = DenseMatrix::from_2d_array(&[&[1.0, 2.0], &[3.0, 4.0]]).unwrap();
let result = nb.predict(&x).unwrap();
assert_eq!(result, vec![3, 3]);
}
#[test]
fn test_gaussian_naive_bayes_numerical_stability() {
#[derive(Debug, PartialEq, Clone)]
struct GaussianTestDistribution {
classes: Vec<u32>,
means: Vec<Vec<f64>>,
variances: Vec<Vec<f64>>,
priors: Vec<f64>,
}
impl NBDistribution<f64, u32> for GaussianTestDistribution {
fn classes(&self) -> &Vec<u32> {
&self.classes
}
fn prior(&self, class_index: usize) -> f64 {
self.priors[class_index]
}
fn log_likelihood<'a>(
&'a self,
class_index: usize,
j: &'a Box<dyn ArrayView1<f64> + 'a>,
) -> f64 {
let means = &self.means[class_index];
let variances = &self.variances[class_index];
j.iterator(0)
.enumerate()
.map(|(i, &xi)| {
let mean = means[i];
let var = variances[i] + 1e-9; // Small smoothing for numerical stability
let coeff = -0.5 * (2.0 * std::f64::consts::PI * var).ln();
let exponent = -(xi - mean).powi(2) / (2.0 * var);
coeff + exponent
})
.sum()
}
}
fn train_distribution(x: &DenseMatrix<f64>, y: &[u32]) -> GaussianTestDistribution {
let mut classes: Vec<u32> = y
.iter()
.cloned()
.collect::<std::collections::HashSet<u32>>()
.into_iter()
.collect();
classes.sort();
let n_classes = classes.len();
let n_features = x.shape().1;
let mut means = vec![vec![0.0; n_features]; n_classes];
let mut variances = vec![vec![0.0; n_features]; n_classes];
let mut class_counts = vec![0; n_classes];
// Calculate means and count samples per class
for (sample, &class) in x.row_iter().zip(y.iter()) {
let class_idx = classes.iter().position(|&c| c == class).unwrap();
class_counts[class_idx] += 1;
for (i, &value) in sample.iterator(0).enumerate() {
means[class_idx][i] += value;
}
}
// Normalize means
for (class_idx, mean) in means.iter_mut().enumerate() {
for value in mean.iter_mut() {
*value /= class_counts[class_idx] as f64;
}
}
// Calculate variances
for (sample, &class) in x.row_iter().zip(y.iter()) {
let class_idx = classes.iter().position(|&c| c == class).unwrap();
for (i, &value) in sample.iterator(0).enumerate() {
let diff = value - means[class_idx][i];
variances[class_idx][i] += diff * diff;
}
}
// Normalize variances and add small epsilon to avoid zero variance
let epsilon = 1e-9;
for (class_idx, variance) in variances.iter_mut().enumerate() {
for value in variance.iter_mut() {
*value = *value / class_counts[class_idx] as f64 + epsilon;
}
}
// Calculate priors
let total_samples = y.len() as f64;
let priors: Vec<f64> = class_counts
.iter()
.map(|&count| count as f64 / total_samples)
.collect();
GaussianTestDistribution {
classes,
means,
variances,
priors,
}
}
type TestNBGaussian =
BaseNaiveBayes<f64, u32, DenseMatrix<f64>, Vec<u32>, GaussianTestDistribution>;
// Create a constant training dataset
let n_samples = 1000;
let n_features = 5;
let n_classes = 4;
let mut x_data = Vec::with_capacity(n_samples * n_features);
let mut y_data = Vec::with_capacity(n_samples);
for i in 0..n_samples {
for j in 0..n_features {
x_data.push((i * j) as f64 % 10.0);
}
y_data.push((i % n_classes) as u32);
}
let x = DenseMatrix::new(n_samples, n_features, x_data, true).unwrap();
let y = y_data;
// Train the model
let dist = train_distribution(&x, &y);
let nb = TestNBGaussian::fit(dist).unwrap();
// Create constant test data
let n_test_samples = 100;
let mut test_x_data = Vec::with_capacity(n_test_samples * n_features);
for i in 0..n_test_samples {
for j in 0..n_features {
test_x_data.push((i * j * 2) as f64 % 15.0);
}
}
let test_x = DenseMatrix::new(n_test_samples, n_features, test_x_data, true).unwrap();
// Make predictions
let predictions = nb
.predict(&test_x)
.map_err(|e| format!("Prediction failed: {}", e))
.unwrap();
// Check numerical stability
assert_eq!(
predictions.len(),
n_test_samples,
"Number of predictions should match number of test samples"
);
// Check that all predictions are valid class labels
for &pred in predictions.iter() {
assert!(pred < n_classes as u32, "Predicted class should be valid");
}
// Check consistency of predictions
let repeated_predictions = nb
.predict(&test_x)
.map_err(|e| format!("Repeated prediction failed: {}", e))
.unwrap();
assert_eq!(
predictions, repeated_predictions,
"Predictions should be consistent when repeated"
);
// Check extreme values
let extreme_x =
DenseMatrix::new(2, n_features, vec![f64::MAX; n_features * 2], true).unwrap();
let extreme_predictions = nb.predict(&extreme_x);
assert!(
extreme_predictions.is_err(),
"Extreme value input should result in an error"
);
assert_eq!(
extreme_predictions.unwrap_err().to_string(),
"Predict failed: Failed to predict, all probabilities were NaN",
"Incorrect error message for extreme values"
);
// Check for NaN handling
let nan_x = DenseMatrix::new(2, n_features, vec![f64::NAN; n_features * 2], true).unwrap();
let nan_predictions = nb.predict(&nan_x);
assert!(
nan_predictions.is_err(),
"NaN input should result in an error"
);
// Check for very small values
let small_x =
DenseMatrix::new(2, n_features, vec![f64::MIN_POSITIVE; n_features * 2], true).unwrap();
let small_predictions = nb
.predict(&small_x)
.map_err(|e| format!("Small value prediction failed: {}", e))
.unwrap();
for &pred in small_predictions.iter() {
assert!(
pred < n_classes as u32,
"Predictions for very small values should be valid"
);
}
// Check for values close to zero
let near_zero_x =
DenseMatrix::new(2, n_features, vec![1e-300; n_features * 2], true).unwrap();
let near_zero_predictions = nb
.predict(&near_zero_x)
.map_err(|e| format!("Near-zero value prediction failed: {}", e))
.unwrap();
for &pred in near_zero_predictions.iter() {
assert!(
pred < n_classes as u32,
"Predictions for near-zero values should be valid"
);
}
println!("All numerical stability checks passed!");
}
#[test]
fn test_gaussian_naive_bayes_numerical_stability_random_data() {
#[derive(Debug)]
struct MySimpleRng {
state: u64,
}
impl MySimpleRng {
fn new(seed: u64) -> Self {
MySimpleRng { state: seed }
}
/// Get the next u64 in the sequence.
fn next_u64(&mut self) -> u64 {
// LCG parameters; these are somewhat arbitrary but commonly used.
// Feel free to tweak the multiplier/adder etc.
self.state = self.state.wrapping_mul(6364136223846793005).wrapping_add(1);
self.state
}
/// Get an f64 in the range [min, max).
fn next_f64(&mut self, min: f64, max: f64) -> f64 {
let fraction = (self.next_u64() as f64) / (u64::MAX as f64);
min + fraction * (max - min)
}
/// Get a usize in the range [min, max). This floors the floating result.
fn gen_range_usize(&mut self, min: usize, max: usize) -> usize {
let v = self.next_f64(min as f64, max as f64);
// Truncate into the integer range. Because of floating inexactness,
// ensure we also clamp.
let int_v = v.floor() as isize;
// simple clamp to avoid any float rounding out of range
let clamped = int_v.max(min as isize).min((max - 1) as isize);
clamped as usize
}
}
use crate::naive_bayes::gaussian::GaussianNB;
// We will generate random data in a reproducible way (using a fixed seed).
// We will generate random data in a reproducible way:
let mut rng = MySimpleRng::new(42);
let n_samples = 1000;
let n_features = 5;
let n_classes = 4;
// Our feature matrix and label vector
let mut x_data = Vec::with_capacity(n_samples * n_features);
let mut y_data = Vec::with_capacity(n_samples);
// Fill x_data with random values and y_data with random class labels.
for _i in 0..n_samples {
for _j in 0..n_features {
// Well pick random values in [-10, 10).
x_data.push(rng.next_f64(-10.0, 10.0));
}
let class = rng.gen_range_usize(0, n_classes) as u32;
y_data.push(class);
}
// Create DenseMatrix from x_data
let x = DenseMatrix::new(n_samples, n_features, x_data, true).unwrap();
// Train GaussianNB
let gnb = GaussianNB::fit(&x, &y_data, Default::default())
.expect("Fitting GaussianNB with random data failed.");
// Predict on the same training data to verify no numerical instability
let predictions = gnb.predict(&x).expect("Prediction on random data failed.");
// Basic sanity checks
assert_eq!(
predictions.len(),
n_samples,
"Prediction size must match n_samples"
);
for &pred_class in &predictions {
assert!(
(pred_class as usize) < n_classes,
"Predicted class {} is out of range [0..n_classes).",
pred_class
);
}
// If you want to compare with scikit-learn, you can do something like:
// println!("X = {:?}", &x);
// println!("Y = {:?}", &y_data);
// println!("predictions = {:?}", &predictions);
// and then in Python:
// import numpy as np
// from sklearn.naive_bayes import GaussianNB
// X = np.reshape(np.array(x), (1000, 5), order='F')
// Y = np.array(y)
// gnb = GaussianNB().fit(X, Y)
// preds = gnb.predict(X)
// expected = np.array(predictions)
// assert expected == preds
// They should match closely (or exactly) depending on floating rounding.
}
} }
+4 -3
View File
@@ -208,7 +208,7 @@ impl<TY: Number + Ord + Unsigned> MultinomialNBDistribution<TY> {
/// * `x` - training data. /// * `x` - training data.
/// * `y` - vector with target values (classes) of length N. /// * `y` - vector with target values (classes) of length N.
/// * `priors` - Optional vector with prior probabilities of the classes. If not defined, /// * `priors` - Optional vector with prior probabilities of the classes. If not defined,
/// priors are adjusted according to the data. /// priors are adjusted according to the data.
/// * `alpha` - Additive (Laplace/Lidstone) smoothing parameter. /// * `alpha` - Additive (Laplace/Lidstone) smoothing parameter.
pub fn fit<TX: Number + Unsigned, X: Array2<TX>, Y: Array1<TY>>( pub fn fit<TX: Number + Unsigned, X: Array2<TX>, Y: Array1<TY>>(
x: &X, x: &X,
@@ -345,10 +345,10 @@ impl<TX: Number + Unsigned, TY: Number + Ord + Unsigned, X: Array2<TX>, Y: Array
{ {
/// Fits MultinomialNB with given data /// Fits MultinomialNB with given data
/// * `x` - training data of size NxM where N is the number of samples and M is the number of /// * `x` - training data of size NxM where N is the number of samples and M is the number of
/// features. /// features.
/// * `y` - vector with target values (classes) of length N. /// * `y` - vector with target values (classes) of length N.
/// * `parameters` - additional parameters like class priors, alpha for smoothing and /// * `parameters` - additional parameters like class priors, alpha for smoothing and
/// binarizing threshold. /// binarizing threshold.
pub fn fit(x: &X, y: &Y, parameters: MultinomialNBParameters) -> Result<Self, Failed> { pub fn fit(x: &X, y: &Y, parameters: MultinomialNBParameters) -> Result<Self, Failed> {
let distribution = let distribution =
MultinomialNBDistribution::fit(x, y, parameters.alpha, parameters.priors)?; MultinomialNBDistribution::fit(x, y, parameters.alpha, parameters.priors)?;
@@ -358,6 +358,7 @@ impl<TX: Number + Unsigned, TY: Number + Ord + Unsigned, X: Array2<TX>, Y: Array
/// Estimates the class labels for the provided data. /// Estimates the class labels for the provided data.
/// * `x` - data of shape NxM where N is number of data points to estimate and M is number of features. /// * `x` - data of shape NxM where N is number of data points to estimate and M is number of features.
///
/// Returns a vector of size N with class estimates. /// Returns a vector of size N with class estimates.
pub fn predict(&self, x: &X) -> Result<Y, Failed> { pub fn predict(&self, x: &X) -> Result<Y, Failed> {
self.inner.as_ref().unwrap().predict(x) self.inner.as_ref().unwrap().predict(x)
+1
View File
@@ -261,6 +261,7 @@ impl<TX: Number, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>, D: Distance<Vec
/// Estimates the class labels for the provided data. /// Estimates the class labels for the provided data.
/// * `x` - data of shape NxM where N is number of data points to estimate and M is number of features. /// * `x` - data of shape NxM where N is number of data points to estimate and M is number of features.
///
/// Returns a vector of size N with class estimates. /// Returns a vector of size N with class estimates.
pub fn predict(&self, x: &X) -> Result<Y, Failed> { pub fn predict(&self, x: &X) -> Result<Y, Failed> {
let mut result = Y::zeros(x.shape().0); let mut result = Y::zeros(x.shape().0);
+2 -5
View File
@@ -88,25 +88,21 @@ pub struct KNNRegressor<TX: Number, TY: Number, X: Array2<TX>, Y: Array1<TY>, D:
impl<TX: Number, TY: Number, X: Array2<TX>, Y: Array1<TY>, D: Distance<Vec<TX>>> impl<TX: Number, TY: Number, X: Array2<TX>, Y: Array1<TY>, D: Distance<Vec<TX>>>
KNNRegressor<TX, TY, X, Y, D> KNNRegressor<TX, TY, X, Y, D>
{ {
///
fn y(&self) -> &Y { fn y(&self) -> &Y {
self.y.as_ref().unwrap() self.y.as_ref().unwrap()
} }
///
fn knn_algorithm(&self) -> &KNNAlgorithm<TX, D> { fn knn_algorithm(&self) -> &KNNAlgorithm<TX, D> {
self.knn_algorithm self.knn_algorithm
.as_ref() .as_ref()
.expect("Missing parameter: KNNAlgorithm") .expect("Missing parameter: KNNAlgorithm")
} }
///
fn weight(&self) -> &KNNWeightFunction { fn weight(&self) -> &KNNWeightFunction {
self.weight.as_ref().expect("Missing parameter: weight") self.weight.as_ref().expect("Missing parameter: weight")
} }
#[allow(dead_code)] #[allow(dead_code)]
///
fn k(&self) -> usize { fn k(&self) -> usize {
self.k.unwrap() self.k.unwrap()
} }
@@ -250,6 +246,7 @@ impl<TX: Number, TY: Number, X: Array2<TX>, Y: Array1<TY>, D: Distance<Vec<TX>>>
/// Predict the target for the provided data. /// Predict the target for the provided data.
/// * `x` - data of shape NxM where N is number of data points to estimate and M is number of features. /// * `x` - data of shape NxM where N is number of data points to estimate and M is number of features.
///
/// Returns a vector of size N with estimates. /// Returns a vector of size N with estimates.
pub fn predict(&self, x: &X) -> Result<Y, Failed> { pub fn predict(&self, x: &X) -> Result<Y, Failed> {
let mut result = Y::zeros(x.shape().0); let mut result = Y::zeros(x.shape().0);
@@ -312,7 +309,7 @@ mod tests {
let y_hat = knn.predict(&x).unwrap(); let y_hat = knn.predict(&x).unwrap();
assert_eq!(5, Vec::len(&y_hat)); assert_eq!(5, Vec::len(&y_hat));
for i in 0..y_hat.len() { for i in 0..y_hat.len() {
assert!((y_hat[i] - y_exp[i]).abs() < std::f64::EPSILON); assert!((y_hat[i] - y_exp[i]).abs() < f64::EPSILON);
} }
} }
@@ -1,5 +1,3 @@
// TODO: missing documentation
use std::default::Default; use std::default::Default;
use crate::linalg::basic::arrays::Array1; use crate::linalg::basic::arrays::Array1;
@@ -8,30 +6,27 @@ use crate::optimization::first_order::{FirstOrderOptimizer, OptimizerResult};
use crate::optimization::line_search::LineSearchMethod; use crate::optimization::line_search::LineSearchMethod;
use crate::optimization::{DF, F}; use crate::optimization::{DF, F};
/// /// Gradient Descent optimization algorithm
pub struct GradientDescent { pub struct GradientDescent {
/// /// Maximum number of iterations
pub max_iter: usize, pub max_iter: usize,
/// /// Relative tolerance for the gradient norm
pub g_rtol: f64, pub g_rtol: f64,
/// /// Absolute tolerance for the gradient norm
pub g_atol: f64, pub g_atol: f64,
} }
///
impl Default for GradientDescent { impl Default for GradientDescent {
fn default() -> Self { fn default() -> Self {
GradientDescent { GradientDescent {
max_iter: 10000, max_iter: 10000,
g_rtol: std::f64::EPSILON.sqrt(), g_rtol: f64::EPSILON.sqrt(),
g_atol: std::f64::EPSILON, g_atol: f64::EPSILON,
} }
} }
} }
///
impl<T: FloatNumber> FirstOrderOptimizer<T> for GradientDescent { impl<T: FloatNumber> FirstOrderOptimizer<T> for GradientDescent {
///
fn optimize<'a, X: Array1<T>, LS: LineSearchMethod<T>>( fn optimize<'a, X: Array1<T>, LS: LineSearchMethod<T>>(
&self, &self,
f: &'a F<'_, T, X>, f: &'a F<'_, T, X>,
+14 -25
View File
@@ -11,31 +11,29 @@ use crate::optimization::first_order::{FirstOrderOptimizer, OptimizerResult};
use crate::optimization::line_search::LineSearchMethod; use crate::optimization::line_search::LineSearchMethod;
use crate::optimization::{DF, F}; use crate::optimization::{DF, F};
/// /// Limited-memory BFGS optimization algorithm
pub struct LBFGS { pub struct LBFGS {
/// /// Maximum number of iterations
pub max_iter: usize, pub max_iter: usize,
/// /// TODO: Add documentation
pub g_rtol: f64, pub g_rtol: f64,
/// /// TODO: Add documentation
pub g_atol: f64, pub g_atol: f64,
/// /// TODO: Add documentation
pub x_atol: f64, pub x_atol: f64,
/// /// TODO: Add documentation
pub x_rtol: f64, pub x_rtol: f64,
/// /// TODO: Add documentation
pub f_abstol: f64, pub f_abstol: f64,
/// /// TODO: Add documentation
pub f_reltol: f64, pub f_reltol: f64,
/// /// TODO: Add documentation
pub successive_f_tol: usize, pub successive_f_tol: usize,
/// /// TODO: Add documentation
pub m: usize, pub m: usize,
} }
///
impl Default for LBFGS { impl Default for LBFGS {
///
fn default() -> Self { fn default() -> Self {
LBFGS { LBFGS {
max_iter: 1000, max_iter: 1000,
@@ -51,9 +49,7 @@ impl Default for LBFGS {
} }
} }
///
impl LBFGS { impl LBFGS {
///
fn two_loops<T: FloatNumber + RealNumber, X: Array1<T>>(&self, state: &mut LBFGSState<T, X>) { fn two_loops<T: FloatNumber + RealNumber, X: Array1<T>>(&self, state: &mut LBFGSState<T, X>) {
let lower = state.iteration.max(self.m) - self.m; let lower = state.iteration.max(self.m) - self.m;
let upper = state.iteration; let upper = state.iteration;
@@ -95,7 +91,6 @@ impl LBFGS {
state.s.mul_scalar_mut(-T::one()); state.s.mul_scalar_mut(-T::one());
} }
///
fn init_state<T: FloatNumber + RealNumber, X: Array1<T>>(&self, x: &X) -> LBFGSState<T, X> { fn init_state<T: FloatNumber + RealNumber, X: Array1<T>>(&self, x: &X) -> LBFGSState<T, X> {
LBFGSState { LBFGSState {
x: x.clone(), x: x.clone(),
@@ -119,7 +114,6 @@ impl LBFGS {
} }
} }
///
fn update_state<'a, T: FloatNumber + RealNumber, X: Array1<T>, LS: LineSearchMethod<T>>( fn update_state<'a, T: FloatNumber + RealNumber, X: Array1<T>, LS: LineSearchMethod<T>>(
&self, &self,
f: &'a F<'_, T, X>, f: &'a F<'_, T, X>,
@@ -161,7 +155,6 @@ impl LBFGS {
df(&mut state.x_df, &state.x); df(&mut state.x_df, &state.x);
} }
///
fn assess_convergence<T: FloatNumber, X: Array1<T>>( fn assess_convergence<T: FloatNumber, X: Array1<T>>(
&self, &self,
state: &mut LBFGSState<T, X>, state: &mut LBFGSState<T, X>,
@@ -173,7 +166,7 @@ impl LBFGS {
} }
if state.x.max_diff(&state.x_prev) if state.x.max_diff(&state.x_prev)
<= T::from_f64(self.x_rtol * state.x.norm(std::f64::INFINITY)).unwrap() <= T::from_f64(self.x_rtol * state.x.norm(f64::INFINITY)).unwrap()
{ {
x_converged = true; x_converged = true;
} }
@@ -188,14 +181,13 @@ impl LBFGS {
state.counter_f_tol += 1; state.counter_f_tol += 1;
} }
if state.x_df.norm(std::f64::INFINITY) <= self.g_atol { if state.x_df.norm(f64::INFINITY) <= self.g_atol {
g_converged = true; g_converged = true;
} }
g_converged || x_converged || state.counter_f_tol > self.successive_f_tol g_converged || x_converged || state.counter_f_tol > self.successive_f_tol
} }
///
fn update_hessian<T: FloatNumber, X: Array1<T>>( fn update_hessian<T: FloatNumber, X: Array1<T>>(
&self, &self,
_: &DF<'_, X>, _: &DF<'_, X>,
@@ -212,7 +204,6 @@ impl LBFGS {
} }
} }
///
#[derive(Debug)] #[derive(Debug)]
struct LBFGSState<T: FloatNumber, X: Array1<T>> { struct LBFGSState<T: FloatNumber, X: Array1<T>> {
x: X, x: X,
@@ -234,9 +225,7 @@ struct LBFGSState<T: FloatNumber, X: Array1<T>> {
alpha: T, alpha: T,
} }
///
impl<T: FloatNumber + RealNumber> FirstOrderOptimizer<T> for LBFGS { impl<T: FloatNumber + RealNumber> FirstOrderOptimizer<T> for LBFGS {
///
fn optimize<'a, X: Array1<T>, LS: LineSearchMethod<T>>( fn optimize<'a, X: Array1<T>, LS: LineSearchMethod<T>>(
&self, &self,
f: &F<'_, T, X>, f: &F<'_, T, X>,
@@ -248,7 +237,7 @@ impl<T: FloatNumber + RealNumber> FirstOrderOptimizer<T> for LBFGS {
df(&mut state.x_df, x0); df(&mut state.x_df, x0);
let g_converged = state.x_df.norm(std::f64::INFINITY) < self.g_atol; let g_converged = state.x_df.norm(f64::INFINITY) < self.g_atol;
let mut converged = g_converged; let mut converged = g_converged;
let stopped = false; let stopped = false;
@@ -299,7 +288,7 @@ mod tests {
let result = optimizer.optimize(&f, &df, &x0, &ls); let result = optimizer.optimize(&f, &df, &x0, &ls);
assert!((result.f_x - 0.0).abs() < std::f64::EPSILON); assert!((result.f_x - 0.0).abs() < f64::EPSILON);
assert!((result.x[0] - 1.0).abs() < 1e-8); assert!((result.x[0] - 1.0).abs() < 1e-8);
assert!((result.x[1] - 1.0).abs() < 1e-8); assert!((result.x[1] - 1.0).abs() < 1e-8);
assert!(result.iterations <= 24); assert!(result.iterations <= 24);
+8 -8
View File
@@ -1,6 +1,6 @@
/// /// Gradient descent optimization algorithm
pub mod gradient_descent; pub mod gradient_descent;
/// /// Limited-memory BFGS optimization algorithm
pub mod lbfgs; pub mod lbfgs;
use std::clone::Clone; use std::clone::Clone;
@@ -11,9 +11,9 @@ use crate::numbers::floatnum::FloatNumber;
use crate::optimization::line_search::LineSearchMethod; use crate::optimization::line_search::LineSearchMethod;
use crate::optimization::{DF, F}; use crate::optimization::{DF, F};
/// /// First-order optimization is a class of algorithms that use the first derivative of a function to find optimal solutions.
pub trait FirstOrderOptimizer<T: FloatNumber> { pub trait FirstOrderOptimizer<T: FloatNumber> {
/// /// run first order optimization
fn optimize<'a, X: Array1<T>, LS: LineSearchMethod<T>>( fn optimize<'a, X: Array1<T>, LS: LineSearchMethod<T>>(
&self, &self,
f: &F<'_, T, X>, f: &F<'_, T, X>,
@@ -23,13 +23,13 @@ pub trait FirstOrderOptimizer<T: FloatNumber> {
) -> OptimizerResult<T, X>; ) -> OptimizerResult<T, X>;
} }
/// /// Result of optimization
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct OptimizerResult<T: FloatNumber, X: Array1<T>> { pub struct OptimizerResult<T: FloatNumber, X: Array1<T>> {
/// /// Solution
pub x: X, pub x: X,
/// /// f(x) value
pub f_x: T, pub f_x: T,
/// /// number of iterations
pub iterations: usize, pub iterations: usize,
} }
+12 -17
View File
@@ -1,11 +1,9 @@
// TODO: missing documentation
use crate::optimization::FunctionOrder; use crate::optimization::FunctionOrder;
use num_traits::Float; use num_traits::Float;
/// /// Line search optimization.
pub trait LineSearchMethod<T: Float> { pub trait LineSearchMethod<T: Float> {
/// /// Find alpha that satisfies strong Wolfe conditions.
fn search( fn search(
&self, &self,
f: &(dyn Fn(T) -> T), f: &(dyn Fn(T) -> T),
@@ -16,32 +14,31 @@ pub trait LineSearchMethod<T: Float> {
) -> LineSearchResult<T>; ) -> LineSearchResult<T>;
} }
/// /// Line search result
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct LineSearchResult<T: Float> { pub struct LineSearchResult<T: Float> {
/// /// Alpha value
pub alpha: T, pub alpha: T,
/// /// f(alpha) value
pub f_x: T, pub f_x: T,
} }
/// /// Backtracking line search method.
pub struct Backtracking<T: Float> { pub struct Backtracking<T: Float> {
/// /// TODO: Add documentation
pub c1: T, pub c1: T,
/// /// Maximum number of iterations for Backtracking single run
pub max_iterations: usize, pub max_iterations: usize,
/// /// TODO: Add documentation
pub max_infinity_iterations: usize, pub max_infinity_iterations: usize,
/// /// TODO: Add documentation
pub phi: T, pub phi: T,
/// /// TODO: Add documentation
pub plo: T, pub plo: T,
/// /// function order
pub order: FunctionOrder, pub order: FunctionOrder,
} }
///
impl<T: Float> Default for Backtracking<T> { impl<T: Float> Default for Backtracking<T> {
fn default() -> Self { fn default() -> Self {
Backtracking { Backtracking {
@@ -55,9 +52,7 @@ impl<T: Float> Default for Backtracking<T> {
} }
} }
///
impl<T: Float> LineSearchMethod<T> for Backtracking<T> { impl<T: Float> LineSearchMethod<T> for Backtracking<T> {
///
fn search( fn search(
&self, &self,
f: &(dyn Fn(T) -> T), f: &(dyn Fn(T) -> T),
+7 -9
View File
@@ -1,21 +1,19 @@
// TODO: missing documentation /// first order optimization algorithms
///
pub mod first_order; pub mod first_order;
/// /// line search algorithms
pub mod line_search; pub mod line_search;
/// /// Function f(x) = y
pub type F<'a, T, X> = dyn for<'b> Fn(&'b X) -> T + 'a; pub type F<'a, T, X> = dyn for<'b> Fn(&'b X) -> T + 'a;
/// /// Function df(x)
pub type DF<'a, X> = dyn for<'b> Fn(&'b mut X, &'b X) + 'a; pub type DF<'a, X> = dyn for<'b> Fn(&'b mut X, &'b X) + 'a;
/// /// Function order
#[allow(clippy::upper_case_acronyms)] #[allow(clippy::upper_case_acronyms)]
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum FunctionOrder { pub enum FunctionOrder {
/// /// Second order
SECOND, SECOND,
/// /// Third order
THIRD, THIRD,
} }
+8 -12
View File
@@ -172,18 +172,14 @@ where
T: Number + RealNumber, T: Number + RealNumber,
M: Array2<T>, M: Array2<T>,
{ {
if let Some(output_matrix) = columns.first().cloned() { columns.first().cloned().map(|output_matrix| {
return Some( columns
columns .iter()
.iter() .skip(1)
.skip(1) .fold(output_matrix, |current_matrix, new_colum| {
.fold(output_matrix, |current_matrix, new_colum| { current_matrix.h_stack(new_colum)
current_matrix.h_stack(new_colum) })
}), })
);
} else {
None
}
} }
#[cfg(test)] #[cfg(test)]
+1 -1
View File
@@ -30,7 +30,7 @@ pub struct CSVDefinition<'a> {
/// What seperates the fields in your csv-file? /// What seperates the fields in your csv-file?
field_seperator: &'a str, field_seperator: &'a str,
} }
impl<'a> Default for CSVDefinition<'a> { impl Default for CSVDefinition<'_> {
fn default() -> Self { fn default() -> Self {
Self { Self {
n_rows_header: 1, n_rows_header: 1,
+1 -1
View File
@@ -292,7 +292,7 @@ mod tests {
.unwrap() .unwrap()
.abs(); .abs();
assert!((4913f64 - result) < std::f64::EPSILON); assert!((4913f64 - result).abs() < f64::EPSILON);
} }
#[cfg_attr( #[cfg_attr(
+3 -3
View File
@@ -360,8 +360,8 @@ impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX> + 'a, Y: Array
} }
} }
impl<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>> PartialEq impl<TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>> PartialEq
for SVC<'a, TX, TY, X, Y> for SVC<'_, TX, TY, X, Y>
{ {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
if (self.b.unwrap().sub(other.b.unwrap())).abs() > TX::epsilon() * TX::two() if (self.b.unwrap().sub(other.b.unwrap())).abs() > TX::epsilon() * TX::two()
@@ -1110,7 +1110,7 @@ mod tests {
let svc = SVC::fit(&x, &y, &params).unwrap(); let svc = SVC::fit(&x, &y, &params).unwrap();
// serialization // serialization
let deserialized_svc: SVC<f64, i32, _, _> = let deserialized_svc: SVC<'_, f64, i32, _, _> =
serde_json::from_str(&serde_json::to_string(&svc).unwrap()).unwrap(); serde_json::from_str(&serde_json::to_string(&svc).unwrap()).unwrap();
assert_eq!(svc, deserialized_svc); assert_eq!(svc, deserialized_svc);
+3 -3
View File
@@ -281,8 +281,8 @@ impl<'a, T: Number + FloatNumber + PartialOrd, X: Array2<T>, Y: Array1<T>> SVR<'
} }
} }
impl<'a, T: Number + FloatNumber + PartialOrd, X: Array2<T>, Y: Array1<T>> PartialEq impl<T: Number + FloatNumber + PartialOrd, X: Array2<T>, Y: Array1<T>> PartialEq
for SVR<'a, T, X, Y> for SVR<'_, T, X, Y>
{ {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
if (self.b - other.b).abs() > T::epsilon() * T::two() if (self.b - other.b).abs() > T::epsilon() * T::two()
@@ -702,7 +702,7 @@ mod tests {
let svr = SVR::fit(&x, &y, &params).unwrap(); let svr = SVR::fit(&x, &y, &params).unwrap();
let deserialized_svr: SVR<f64, DenseMatrix<f64>, _> = let deserialized_svr: SVR<'_, f64, DenseMatrix<f64>, _> =
serde_json::from_str(&serde_json::to_string(&svr).unwrap()).unwrap(); serde_json::from_str(&serde_json::to_string(&svr).unwrap()).unwrap();
assert_eq!(svr, deserialized_svr); assert_eq!(svr, deserialized_svr);
+121 -10
View File
@@ -77,7 +77,9 @@ use serde::{Deserialize, Serialize};
use crate::api::{Predictor, SupervisedEstimator}; use crate::api::{Predictor, SupervisedEstimator};
use crate::error::Failed; use crate::error::Failed;
use crate::linalg::basic::arrays::MutArray;
use crate::linalg::basic::arrays::{Array1, Array2, MutArrayView1}; use crate::linalg::basic::arrays::{Array1, Array2, MutArrayView1};
use crate::linalg::basic::matrix::DenseMatrix;
use crate::numbers::basenum::Number; use crate::numbers::basenum::Number;
use crate::rand_custom::get_rng_impl; use crate::rand_custom::get_rng_impl;
@@ -197,12 +199,12 @@ impl PartialEq for Node {
self.output == other.output self.output == other.output
&& self.split_feature == other.split_feature && self.split_feature == other.split_feature
&& match (self.split_value, other.split_value) { && match (self.split_value, other.split_value) {
(Some(a), Some(b)) => (a - b).abs() < std::f64::EPSILON, (Some(a), Some(b)) => (a - b).abs() < f64::EPSILON,
(None, None) => true, (None, None) => true,
_ => false, _ => false,
} }
&& match (self.split_score, other.split_score) { && match (self.split_score, other.split_score) {
(Some(a), Some(b)) => (a - b).abs() < std::f64::EPSILON, (Some(a), Some(b)) => (a - b).abs() < f64::EPSILON,
(None, None) => true, (None, None) => true,
_ => false, _ => false,
} }
@@ -613,7 +615,7 @@ impl<TX: Number + PartialOrd, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>>
visitor_queue.push_back(visitor); visitor_queue.push_back(visitor);
} }
while tree.depth() < tree.parameters().max_depth.unwrap_or(std::u16::MAX) { while tree.depth() < tree.parameters().max_depth.unwrap_or(u16::MAX) {
match visitor_queue.pop_front() { match visitor_queue.pop_front() {
Some(node) => tree.split(node, mtry, &mut visitor_queue, &mut rng), Some(node) => tree.split(node, mtry, &mut visitor_queue, &mut rng),
None => break, None => break,
@@ -650,7 +652,7 @@ impl<TX: Number + PartialOrd, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>>
if node.true_child.is_none() && node.false_child.is_none() { if node.true_child.is_none() && node.false_child.is_none() {
result = node.output; result = node.output;
} else if x.get((row, node.split_feature)).to_f64().unwrap() } else if x.get((row, node.split_feature)).to_f64().unwrap()
<= node.split_value.unwrap_or(std::f64::NAN) <= node.split_value.unwrap_or(f64::NAN)
{ {
queue.push_back(node.true_child.unwrap()); queue.push_back(node.true_child.unwrap());
} else { } else {
@@ -803,9 +805,7 @@ impl<TX: Number + PartialOrd, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>>
.get((i, self.nodes()[visitor.node].split_feature)) .get((i, self.nodes()[visitor.node].split_feature))
.to_f64() .to_f64()
.unwrap() .unwrap()
<= self.nodes()[visitor.node] <= self.nodes()[visitor.node].split_value.unwrap_or(f64::NAN)
.split_value
.unwrap_or(std::f64::NAN)
{ {
*true_sample = visitor.samples[i]; *true_sample = visitor.samples[i];
tc += *true_sample; tc += *true_sample;
@@ -889,11 +889,77 @@ impl<TX: Number + PartialOrd, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>>
} }
importances importances
} }
/// Predict class probabilities for the input samples.
///
/// # Arguments
///
/// * `x` - The input samples as a matrix where each row is a sample and each column is a feature.
///
/// # Returns
///
/// A `Result` containing a `DenseMatrix<f64>` where each row corresponds to a sample and each column
/// corresponds to a class. The values represent the probability of the sample belonging to each class.
///
/// # Errors
///
/// Returns an error if at least one row prediction process fails.
pub fn predict_proba(&self, x: &X) -> Result<DenseMatrix<f64>, Failed> {
let (n_samples, _) = x.shape();
let n_classes = self.classes().len();
let mut result = DenseMatrix::<f64>::zeros(n_samples, n_classes);
for i in 0..n_samples {
let probs = self.predict_proba_for_row(x, i)?;
for (j, &prob) in probs.iter().enumerate() {
result.set((i, j), prob);
}
}
Ok(result)
}
/// Predict class probabilities for a single input sample.
///
/// # Arguments
///
/// * `x` - The input matrix containing all samples.
/// * `row` - The index of the row in `x` for which to predict probabilities.
///
/// # Returns
///
/// A vector of probabilities, one for each class, representing the probability
/// of the input sample belonging to each class.
fn predict_proba_for_row(&self, x: &X, row: usize) -> Result<Vec<f64>, Failed> {
let mut node = 0;
while let Some(current_node) = self.nodes().get(node) {
if current_node.true_child.is_none() && current_node.false_child.is_none() {
// Leaf node reached
let mut probs = vec![0.0; self.classes().len()];
probs[current_node.output] = 1.0;
return Ok(probs);
}
let split_feature = current_node.split_feature;
let split_value = current_node.split_value.unwrap_or(f64::NAN);
if x.get((row, split_feature)).to_f64().unwrap() <= split_value {
node = current_node.true_child.unwrap();
} else {
node = current_node.false_child.unwrap();
}
}
// This should never happen if the tree is properly constructed
Err(Failed::predict("Nodes iteration did not reach leaf"))
}
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::linalg::basic::arrays::Array;
use crate::linalg::basic::matrix::DenseMatrix; use crate::linalg::basic::matrix::DenseMatrix;
#[test] #[test]
@@ -925,17 +991,62 @@ mod tests {
)] )]
#[test] #[test]
fn gini_impurity() { fn gini_impurity() {
assert!((impurity(&SplitCriterion::Gini, &[7, 3], 10) - 0.42).abs() < std::f64::EPSILON); assert!((impurity(&SplitCriterion::Gini, &[7, 3], 10) - 0.42).abs() < f64::EPSILON);
assert!( assert!(
(impurity(&SplitCriterion::Entropy, &[7, 3], 10) - 0.8812908992306927).abs() (impurity(&SplitCriterion::Entropy, &[7, 3], 10) - 0.8812908992306927).abs()
< std::f64::EPSILON < f64::EPSILON
); );
assert!( assert!(
(impurity(&SplitCriterion::ClassificationError, &[7, 3], 10) - 0.3).abs() (impurity(&SplitCriterion::ClassificationError, &[7, 3], 10) - 0.3).abs()
< std::f64::EPSILON < f64::EPSILON
); );
} }
#[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test]
fn test_predict_proba() {
let x: DenseMatrix<f64> = DenseMatrix::from_2d_array(&[
&[5.1, 3.5, 1.4, 0.2],
&[4.9, 3.0, 1.4, 0.2],
&[4.7, 3.2, 1.3, 0.2],
&[4.6, 3.1, 1.5, 0.2],
&[5.0, 3.6, 1.4, 0.2],
&[7.0, 3.2, 4.7, 1.4],
&[6.4, 3.2, 4.5, 1.5],
&[6.9, 3.1, 4.9, 1.5],
&[5.5, 2.3, 4.0, 1.3],
&[6.5, 2.8, 4.6, 1.5],
])
.unwrap();
let y: Vec<usize> = vec![0, 0, 0, 0, 0, 1, 1, 1, 1, 1];
let tree = DecisionTreeClassifier::fit(&x, &y, Default::default()).unwrap();
let probabilities = tree.predict_proba(&x).unwrap();
assert_eq!(probabilities.shape(), (10, 2));
for row in 0..10 {
let row_sum: f64 = probabilities.get_row(row).sum();
assert!(
(row_sum - 1.0).abs() < 1e-6,
"Row probabilities should sum to 1"
);
}
// Check if the first 5 samples have higher probability for class 0
for i in 0..5 {
assert!(probabilities.get((i, 0)) > probabilities.get((i, 1)));
}
// Check if the last 5 samples have higher probability for class 1
for i in 5..10 {
assert!(probabilities.get((i, 1)) > probabilities.get((i, 0)));
}
}
#[cfg_attr( #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")), all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test wasm_bindgen_test::wasm_bindgen_test
+6 -8
View File
@@ -311,15 +311,15 @@ impl Node {
impl PartialEq for Node { impl PartialEq for Node {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
(self.output - other.output).abs() < std::f64::EPSILON (self.output - other.output).abs() < f64::EPSILON
&& self.split_feature == other.split_feature && self.split_feature == other.split_feature
&& match (self.split_value, other.split_value) { && match (self.split_value, other.split_value) {
(Some(a), Some(b)) => (a - b).abs() < std::f64::EPSILON, (Some(a), Some(b)) => (a - b).abs() < f64::EPSILON,
(None, None) => true, (None, None) => true,
_ => false, _ => false,
} }
&& match (self.split_score, other.split_score) { && match (self.split_score, other.split_score) {
(Some(a), Some(b)) => (a - b).abs() < std::f64::EPSILON, (Some(a), Some(b)) => (a - b).abs() < f64::EPSILON,
(None, None) => true, (None, None) => true,
_ => false, _ => false,
} }
@@ -478,7 +478,7 @@ impl<TX: Number + PartialOrd, TY: Number, X: Array2<TX>, Y: Array1<TY>>
visitor_queue.push_back(visitor); visitor_queue.push_back(visitor);
} }
while tree.depth() < tree.parameters().max_depth.unwrap_or(std::u16::MAX) { while tree.depth() < tree.parameters().max_depth.unwrap_or(u16::MAX) {
match visitor_queue.pop_front() { match visitor_queue.pop_front() {
Some(node) => tree.split(node, mtry, &mut visitor_queue, &mut rng), Some(node) => tree.split(node, mtry, &mut visitor_queue, &mut rng),
None => break, None => break,
@@ -515,7 +515,7 @@ impl<TX: Number + PartialOrd, TY: Number, X: Array2<TX>, Y: Array1<TY>>
if node.true_child.is_none() && node.false_child.is_none() { if node.true_child.is_none() && node.false_child.is_none() {
result = node.output; result = node.output;
} else if x.get((row, node.split_feature)).to_f64().unwrap() } else if x.get((row, node.split_feature)).to_f64().unwrap()
<= node.split_value.unwrap_or(std::f64::NAN) <= node.split_value.unwrap_or(f64::NAN)
{ {
queue.push_back(node.true_child.unwrap()); queue.push_back(node.true_child.unwrap());
} else { } else {
@@ -640,9 +640,7 @@ impl<TX: Number + PartialOrd, TY: Number, X: Array2<TX>, Y: Array1<TY>>
.get((i, self.nodes()[visitor.node].split_feature)) .get((i, self.nodes()[visitor.node].split_feature))
.to_f64() .to_f64()
.unwrap() .unwrap()
<= self.nodes()[visitor.node] <= self.nodes()[visitor.node].split_value.unwrap_or(f64::NAN)
.split_value
.unwrap_or(std::f64::NAN)
{ {
*true_sample = visitor.samples[i]; *true_sample = visitor.samples[i];
tc += *true_sample; tc += *true_sample;