86 Commits

Author SHA1 Message Date
Lorenzo
f498f9629e Implement realnum::rand (#251)
Co-authored-by: Luis Moreno <morenol@users.noreply.github.com>
Co-authored-by: Lorenzo <tunedconsulting@gmail.com>

* Implement rand. Use the new derive [#default]
* Use custom range
* Use range seed
* Bump version
* Add array length checks for
2023-03-20 14:45:44 +00:00
Lorenzo
7d059c4fb1 Update README.md 2023-03-20 11:54:10 +00:00
morenol
c7353d0b57 Run cargo clippy --fix (#250)
* Run `cargo clippy --fix`
* Run `cargo clippy --all-features --fix`
* Fix other clippy warnings
* cargo fmt

Co-authored-by: Luis Moreno <morenol@users.noreply.github.com>
2023-01-27 10:41:18 +00:00
Lorenzo
83dcf9a8ac Delete iml file 2022-11-10 14:09:55 +00:00
Lorenzo (Mec-iS)
3126ee87d3 Pin deps version 2022-11-09 12:03:03 +00:00
morenol
8efb959b3c 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 16:18:05 +00:00
morenol
9eaae9ef35 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 16:07:14 +00:00
Lorenzo (Mec-iS)
46b6285d05 Merge release-0.3 2022-11-08 15:37:11 +00:00
Lorenzo (Mec-iS)
c683073b14 make work cargo build --target wasm32-unknown-unknown 2022-11-08 15:35:04 +00:00
Lorenzo
161d249917 Release 0.3 (#235) 2022-11-08 15:22:34 +00:00
Lorenzo (Mec-iS)
4558be5f73 Merge branch 'release-0.3' of github.com:smartcorelib/smartcore into release-0.3 2022-11-08 15:17:48 +00:00
Lorenzo (Mec-iS)
6c03e6e0b3 update CHANGELOG 2022-11-08 15:17:31 +00:00
Lorenzo
c934f6b6cf update comment 2022-11-08 14:23:13 +00:00
Lorenzo (Mec-iS)
48f1d6b74d use getrandom/js 2022-11-08 14:19:40 +00:00
Lorenzo (Mec-iS)
dad0d01f6d Update CHANGELOG 2022-11-08 13:59:49 +00:00
Lorenzo (Mec-iS)
98b18c4dae Remove unused tests flags 2022-11-08 13:53:50 +00:00
Lorenzo (Mec-iS)
2418b24ff4 Merge branch 'release-0.3' of github.com:smartcorelib/smartcore into release-0.3 2022-11-08 12:22:06 +00:00
Lorenzo (Mec-iS)
6c6f92697f minor fixes to doc 2022-11-08 12:21:34 +00:00
Lorenzo
a4097fce15 minor fix 2022-11-08 12:18:35 +00:00
Lorenzo
b71c7b49cb minor fix 2022-11-08 12:18:03 +00:00
Lorenzo
78bf75b5d8 minor fix 2022-11-08 12:17:32 +00:00
Lorenzo
a60fdaf235 minor fix 2022-11-08 12:17:04 +00:00
Lorenzo
b4206c4b08 minor fix 2022-11-08 12:15:10 +00:00
Lorenzo (Mec-iS)
3c4a807be8 Fix std_rand feature 2022-11-08 12:04:39 +00:00
Lorenzo (Mec-iS)
c1af60cafb cleanup 2022-11-08 11:55:32 +00:00
Lorenzo (Mec-iS)
2fa454ea94 fmt 2022-11-08 11:48:14 +00:00
Lorenzo (Mec-iS)
8e6e5f9e68 Use getrandom as default (for no-std feature) 2022-11-08 11:47:31 +00:00
Lorenzo (Mec-iS)
bf7b714126 Add static analyzer to doc 2022-11-07 18:16:13 +00:00
Lorenzo (Mec-iS)
3ac6598951 Exclude datasets test for wasm/wasi 2022-11-07 13:56:29 +00:00
Lorenzo (Mec-iS)
cc91e31a0e minor fixes 2022-11-07 13:00:51 +00:00
Lorenzo (Mec-iS)
0ec89402e8 minor fix 2022-11-07 12:50:32 +00:00
Lorenzo (Mec-iS)
23b3699730 Release 0.3 2022-11-07 12:48:44 +00:00
Lorenzo
aab3817c58 Create DEVELOPERS.md 2022-11-04 22:23:36 +00:00
Lorenzo
d3a496419d Update README.md 2022-11-04 22:17:55 +00:00
Lorenzo
ab18f127a0 Update README.md 2022-11-04 22:11:54 +00:00
morenol
425c3c1d0b Use Box in SVM and remove lifetimes (#228)
* Do not change external API
Authored-by: Luis Moreno <morenol@users.noreply.github.com>
2022-11-04 22:08:30 +00:00
morenol
35fe68e024 Fix CI (#227)
* Update ci.yml
Co-authored-by: Luis Moreno <morenol@users.noreply.github.com>
2022-11-03 13:48:16 -05:00
Lorenzo
d592b628be Implement CSV reader with new traits (#209) 2022-11-03 15:49:00 +00:00
Lorenzo (Mec-iS)
b66afa9222 Improve options conditionals 2022-11-03 14:58:05 +00:00
Lorenzo (Mec-iS)
ba70bb941f Implement Display for NaiveBayes 2022-11-03 14:18:56 +00:00
Lorenzo (Mec-iS)
d298709040 cargo clippy 2022-11-03 13:44:27 +00:00
Lorenzo (Mec-iS)
e50b4e8637 Fix signature of metrics tests 2022-11-03 13:40:54 +00:00
Lorenzo (Mec-iS)
26b72b67f4 Add kernels' parameters to public interface 2022-11-03 12:30:43 +00:00
Lorenzo
1964424589 Fix svr tests (#222) 2022-11-03 11:48:40 +00:00
Lorenzo (Mec-iS)
deac31a2ab Refactor modules structure in src/svm 2022-11-02 15:28:50 +00:00
Lorenzo (Mec-iS)
4cff7da50d Merge branch 'development' of github.com:smartcorelib/smartcore into development 2022-11-02 15:24:06 +00:00
Lorenzo (Mec-iS)
df0ae907f7 clean up svm 2022-11-02 15:23:56 +00:00
Lorenzo
cfbd45bfc0 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-02 15:22:38 +00:00
Lorenzo
b60329ca5d Disambiguate distances. Implement Fastpair. (#220) 2022-11-02 14:53:28 +00:00
morenol
4b096ad558 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-02 10:09:03 +00:00
Lorenzo
4cf7e4d7b7 Improve features (#215) 2022-11-01 13:56:20 +00:00
Lorenzo
c3093f11f1 Fix metrics::auc (#212)
* Fix metrics::auc
2022-11-01 12:50:46 +00:00
Lorenzo
083803c900 Port ensemble. Add Display to naive_bayes (#208) 2022-10-31 17:35:33 +00:00
Lorenzo
4f64f2e0ff Update README.md 2022-10-31 10:45:51 +00:00
Lorenzo
52eb6ce023 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-10-31 10:44:57 +00:00
RJ Nowling
bb71656137 Dataset doc cleanup (#205)
* Update iris.rs

* Update mod.rs

* Update digits.rs
2022-10-30 09:32:41 +00:00
Lorenzo
edbac7e4c7 Update README.md 2022-10-18 15:44:38 +01:00
Lorenzo
8a2bdd5a75 Update README.md 2022-10-13 19:47:52 +01:00
Lorenzo
b823b55460 Update CONTRIBUTING.md 2022-10-12 12:21:09 +01:00
morenol
12df301f32 fix: fix issue with iterator for svc search (#182) 2022-10-02 06:15:28 -05:00
morenol
f8210d0af9 refactor: Try to follow similar pattern to other APIs (#180)
Co-authored-by: Luis Moreno <morenol@users.noreply.github.com>
2022-10-01 16:44:08 -05:00
morenol
3c62686d6e 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-10-01 12:47:56 -05:00
Lorenzo
9c59e37a0f Update CONTRIBUTING.md 2022-09-27 14:27:27 +01:00
Lorenzo
0b619fe7eb Add contribution guidelines (#178) 2022-09-27 14:23:18 +01:00
Montana Low
764309e313 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-09-21 22:48:31 -04:00
Montana Low
403d3f2348 add seed param to search params (#168) 2022-09-22 00:15:26 +01:00
morenol
3a44161406 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-09-21 20:35:22 +01:00
Montana Low
48514d1b15 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-09-21 20:34:21 +01:00
morenol
69d8be35de Provide better output in flaky tests (#163) 2022-09-20 17:12:09 +01:00
morenol
c21e75276a 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-09-20 11:29:54 +01:00
morenol
6a2e10452f Make rand_distr optional (#161) 2022-09-20 11:21:02 +01:00
Lorenzo
436da104d7 Update LICENSE 2022-09-19 18:00:17 +01:00
morenol
2510ca4e9d 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-09-19 10:44:01 -04:00
Tim Toebrock
b6f585e60f 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-09-19 10:38:01 +01:00
Montana Low
4685fc73e0 grid search (#154)
* grid search draft
* hyperparam search for linear estimators
2022-09-19 10:31:56 +01:00
Montana Low
2e5f88fad8 Handle multiclass precision/recall (#152)
* handle multiclass precision/recall
2022-09-13 16:23:45 +01:00
dependabot[bot]
e445f0d558 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-09-12 12:03:43 -04:00
Christos Katsakioris
4d5f64c758 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-09-06 18:37:54 +01:00
Tim Toebrock
d305406dfd 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-08-26 15:20:20 +01:00
Lorenzo
3d2f4f71fa Add example for FastPair (#144)
* Add example

* Move to top

* Add imports to example

* Fix imports
2022-08-24 13:40:22 +01:00
Lorenzo
a1c56a859e 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-08-23 16:56:21 +01:00
Chris McComb
d905ebea15 Added additional doctest and fixed indices (#141) 2022-08-12 17:38:13 -04:00
morenol
b482acdc8d Fix clippy warnings (#139)
Co-authored-by: Luis Moreno <morenol@users.noreply.github.com>
2022-07-13 21:06:05 -04:00
ferrouille
b4a807eb9f Add SVC::decision_function (#135) 2022-06-21 12:48:16 -04:00
dependabot[bot]
ff456df0a4 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-05-11 13:14:14 -04:00
dependabot-preview[bot]
322610c7fb 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-05-11 13:04:27 -04:00
56 changed files with 267 additions and 284 deletions
+3 -3
View File
@@ -2,7 +2,7 @@
name = "smartcore"
description = "Machine Learning in Rust."
homepage = "https://smartcorelib.org"
version = "0.3.0"
version = "0.3.1"
authors = ["smartcore Developers"]
edition = "2021"
license = "Apache-2.0"
@@ -42,13 +42,13 @@ std_rand = ["rand/std_rng", "rand/std"]
js = ["getrandom/js"]
[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "*", optional = true }
getrandom = { version = "0.2.8", optional = true }
[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dev-dependencies]
wasm-bindgen-test = "0.3"
[dev-dependencies]
itertools = "*"
itertools = "0.10.5"
serde_json = "1.0"
bincode = "1.3.1"
+1 -1
View File
@@ -18,4 +18,4 @@
-----
[![CI](https://github.com/smartcorelib/smartcore/actions/workflows/ci.yml/badge.svg)](https://github.com/smartcorelib/smartcore/actions/workflows/ci.yml)
To start getting familiar with the new smartcore v0.5 API, there is now available a [**Jupyter Notebook environment repository**](https://github.com/smartcorelib/smartcore-jupyter). Please see instructions there, contributions welcome see [CONTRIBUTING](.github/CONTRIBUTING.md).
To start getting familiar with the new smartcore v0.3 API, there is now available a [**Jupyter Notebook environment repository**](https://github.com/smartcorelib/smartcore-jupyter). Please see instructions there, contributions welcome see [CONTRIBUTING](.github/CONTRIBUTING.md).
-15
View File
@@ -1,15 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="RUST_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/benches" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
+9 -13
View File
@@ -260,8 +260,8 @@ mod tests_fastpair {
let distances = fastpair.distances;
let neighbours = fastpair.neighbours;
assert!(distances.len() != 0);
assert!(neighbours.len() != 0);
assert!(!distances.is_empty());
assert!(!neighbours.is_empty());
assert_eq!(10, neighbours.len());
assert_eq!(10, distances.len());
@@ -276,17 +276,13 @@ mod tests_fastpair {
// We expect an error when we run `FastPair` on this dataset,
// becuase `FastPair` currently only works on a minimum of 3
// points.
let _fastpair = FastPair::new(&dataset);
let fastpair = FastPair::new(&dataset);
assert!(fastpair.is_err());
match _fastpair {
Err(e) => {
let expected_error =
Failed::because(FailedError::FindFailed, "min number of rows should be 3");
assert_eq!(e, expected_error)
}
_ => {
assert!(false);
}
if let Err(e) = fastpair {
let expected_error =
Failed::because(FailedError::FindFailed, "min number of rows should be 3");
assert_eq!(e, expected_error)
}
}
@@ -582,7 +578,7 @@ mod tests_fastpair {
};
for p in dissimilarities.iter() {
if p.distance.unwrap() < min_dissimilarity.distance.unwrap() {
min_dissimilarity = p.clone()
min_dissimilarity = *p
}
}
+2 -7
View File
@@ -49,20 +49,15 @@ pub mod linear_search;
/// Both, KNN classifier and regressor benefits from underlying search algorithms that helps to speed up queries.
/// `KNNAlgorithmName` maintains a list of supported search algorithms, see [KNN algorithms](../algorithm/neighbour/index.html)
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub enum KNNAlgorithmName {
/// Heap Search algorithm, see [`LinearSearch`](../algorithm/neighbour/linear_search/index.html)
LinearSearch,
/// Cover Tree Search algorithm, see [`CoverTree`](../algorithm/neighbour/cover_tree/index.html)
#[default]
CoverTree,
}
impl Default for KNNAlgorithmName {
fn default() -> Self {
KNNAlgorithmName::CoverTree
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub(crate) enum KNNAlgorithm<T: Number, D: Distance<Vec<T>>> {
+2 -2
View File
@@ -18,7 +18,7 @@
//!
//! Example:
//!
//! ```
//! ```ignore
//! use smartcore::linalg::basic::matrix::DenseMatrix;
//! use smartcore::linalg::basic::arrays::Array2;
//! use smartcore::cluster::dbscan::*;
@@ -511,6 +511,6 @@ mod tests {
.and_then(|dbscan| dbscan.predict(&x))
.unwrap();
println!("{:?}", labels);
println!("{labels:?}");
}
}
+2 -2
View File
@@ -498,8 +498,8 @@ mod tests {
let y: Vec<usize> = kmeans.predict(&x).unwrap();
for i in 0..y.len() {
assert_eq!(y[i] as usize, kmeans._y[i]);
for (i, _y_i) in y.iter().enumerate() {
assert_eq!({ y[i] }, kmeans._y[i]);
}
}
+1 -1
View File
@@ -31,7 +31,7 @@ use crate::dataset::Dataset;
pub fn load_dataset() -> Dataset<f32, f32> {
let (x, y, num_samples, num_features) = match deserialize_data(std::include_bytes!("boston.xy"))
{
Err(why) => panic!("Can't deserialize boston.xy. {}", why),
Err(why) => panic!("Can't deserialize boston.xy. {why}"),
Ok((x, y, num_samples, num_features)) => (x, y, num_samples, num_features),
};
+1 -1
View File
@@ -33,7 +33,7 @@ use crate::dataset::Dataset;
pub fn load_dataset() -> Dataset<f32, u32> {
let (x, y, num_samples, num_features) =
match deserialize_data(std::include_bytes!("breast_cancer.xy")) {
Err(why) => panic!("Can't deserialize breast_cancer.xy. {}", why),
Err(why) => panic!("Can't deserialize breast_cancer.xy. {why}"),
Ok((x, y, num_samples, num_features)) => (
x,
y.into_iter().map(|x| x as u32).collect(),
+1 -1
View File
@@ -26,7 +26,7 @@ use crate::dataset::Dataset;
pub fn load_dataset() -> Dataset<f32, u32> {
let (x, y, num_samples, num_features) =
match deserialize_data(std::include_bytes!("diabetes.xy")) {
Err(why) => panic!("Can't deserialize diabetes.xy. {}", why),
Err(why) => panic!("Can't deserialize diabetes.xy. {why}"),
Ok((x, y, num_samples, num_features)) => (
x,
y.into_iter().map(|x| x as u32).collect(),
+1 -1
View File
@@ -16,7 +16,7 @@ use crate::dataset::Dataset;
pub fn load_dataset() -> Dataset<f32, f32> {
let (x, y, num_samples, num_features) = match deserialize_data(std::include_bytes!("digits.xy"))
{
Err(why) => panic!("Can't deserialize digits.xy. {}", why),
Err(why) => panic!("Can't deserialize digits.xy. {why}"),
Ok((x, y, num_samples, num_features)) => (x, y, num_samples, num_features),
};
+1 -1
View File
@@ -22,7 +22,7 @@ use crate::dataset::Dataset;
pub fn load_dataset() -> Dataset<f32, u32> {
let (x, y, num_samples, num_features): (Vec<f32>, Vec<u32>, usize, usize) =
match deserialize_data(std::include_bytes!("iris.xy")) {
Err(why) => panic!("Can't deserialize iris.xy. {}", why),
Err(why) => panic!("Can't deserialize iris.xy. {why}"),
Ok((x, y, num_samples, num_features)) => (
x,
y.into_iter().map(|x| x as u32).collect(),
+1 -1
View File
@@ -78,7 +78,7 @@ pub(crate) fn serialize_data<X: Number + RealNumber, Y: RealNumber>(
.collect();
file.write_all(&y)?;
}
Err(why) => panic!("couldn't create {}: {}", filename, why),
Err(why) => panic!("couldn't create {filename}: {why}"),
}
Ok(())
}
+9 -11
View File
@@ -231,8 +231,7 @@ impl<T: Number + RealNumber, X: Array2<T> + SVDDecomposable<T> + EVDDecomposable
if parameters.n_components > n {
return Err(Failed::fit(&format!(
"Number of components, n_components should be <= number of attributes ({})",
n
"Number of components, n_components should be <= number of attributes ({n})"
)));
}
@@ -374,21 +373,20 @@ mod tests {
let parameters = PCASearchParameters {
n_components: vec![2, 4],
use_correlation_matrix: vec![true, false],
..Default::default()
};
let mut iter = parameters.into_iter();
let next = iter.next().unwrap();
assert_eq!(next.n_components, 2);
assert_eq!(next.use_correlation_matrix, true);
assert!(next.use_correlation_matrix);
let next = iter.next().unwrap();
assert_eq!(next.n_components, 4);
assert_eq!(next.use_correlation_matrix, true);
assert!(next.use_correlation_matrix);
let next = iter.next().unwrap();
assert_eq!(next.n_components, 2);
assert_eq!(next.use_correlation_matrix, false);
assert!(!next.use_correlation_matrix);
let next = iter.next().unwrap();
assert_eq!(next.n_components, 4);
assert_eq!(next.use_correlation_matrix, false);
assert!(!next.use_correlation_matrix);
assert!(iter.next().is_none());
}
@@ -572,8 +570,8 @@ mod tests {
epsilon = 1e-4
));
for i in 0..pca.eigenvalues.len() {
assert!((pca.eigenvalues[i].abs() - expected_eigenvalues[i].abs()).abs() < 1e-8);
for (i, pca_eigenvalues_i) in pca.eigenvalues.iter().enumerate() {
assert!((pca_eigenvalues_i.abs() - expected_eigenvalues[i].abs()).abs() < 1e-8);
}
let us_arrests_t = pca.transform(&us_arrests).unwrap();
@@ -694,8 +692,8 @@ mod tests {
epsilon = 1e-4
));
for i in 0..pca.eigenvalues.len() {
assert!((pca.eigenvalues[i].abs() - expected_eigenvalues[i].abs()).abs() < 1e-8);
for (i, pca_eigenvalues_i) in pca.eigenvalues.iter().enumerate() {
assert!((pca_eigenvalues_i.abs() - expected_eigenvalues[i].abs()).abs() < 1e-8);
}
let us_arrests_t = pca.transform(&us_arrests).unwrap();
+2 -5
View File
@@ -180,8 +180,7 @@ impl<T: Number + RealNumber, X: Array2<T> + SVDDecomposable<T> + EVDDecomposable
if parameters.n_components >= p {
return Err(Failed::fit(&format!(
"Number of components, n_components should be < number of attributes ({})",
p
"Number of components, n_components should be < number of attributes ({p})"
)));
}
@@ -202,8 +201,7 @@ impl<T: Number + RealNumber, X: Array2<T> + SVDDecomposable<T> + EVDDecomposable
let (p_c, k) = self.components.shape();
if p_c != p {
return Err(Failed::transform(&format!(
"Can not transform a {}x{} matrix into {}x{} matrix, incorrect input dimentions",
n, p, n, k
"Can not transform a {n}x{p} matrix into {n}x{k} matrix, incorrect input dimentions"
)));
}
@@ -227,7 +225,6 @@ mod tests {
fn search_parameters() {
let parameters = SVDSearchParameters {
n_components: vec![10, 100],
..Default::default()
};
let mut iter = parameters.into_iter();
let next = iter.next().unwrap();
+29 -1
View File
@@ -454,8 +454,12 @@ impl<TX: FloatNumber + PartialOrd, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY
y: &Y,
parameters: RandomForestClassifierParameters,
) -> Result<RandomForestClassifier<TX, TY, X, Y>, Failed> {
let (_, num_attributes) = x.shape();
let (x_nrows, num_attributes) = x.shape();
let y_ncols = y.shape();
if x_nrows != y_ncols {
return Err(Failed::fit("Number of rows in X should = len(y)"));
}
let mut yi: Vec<usize> = vec![0; y_ncols];
let classes = y.unique();
@@ -678,6 +682,30 @@ mod tests {
assert!(accuracy(&y, &classifier.predict(&x).unwrap()) >= 0.95);
}
#[test]
fn test_random_matrix_with_wrong_rownum() {
let x_rand: DenseMatrix<f64> = DenseMatrix::<f64>::rand(21, 200);
let y: Vec<u32> = vec![0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
let fail = RandomForestClassifier::fit(
&x_rand,
&y,
RandomForestClassifierParameters {
criterion: SplitCriterion::Gini,
max_depth: Option::None,
min_samples_leaf: 1,
min_samples_split: 2,
n_trees: 100,
m: Option::None,
keep_samples: false,
seed: 87,
},
);
assert!(fail.is_err());
}
#[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
+30
View File
@@ -399,6 +399,10 @@ impl<TX: Number + FloatNumber + PartialOrd, TY: Number, X: Array2<TX>, Y: Array1
) -> Result<RandomForestRegressor<TX, TY, X, Y>, Failed> {
let (n_rows, num_attributes) = x.shape();
if n_rows != y.shape() {
return Err(Failed::fit("Number of rows in X should = len(y)"));
}
let mtry = parameters
.m
.unwrap_or((num_attributes as f64).sqrt().floor() as usize);
@@ -595,6 +599,32 @@ mod tests {
assert!(mean_absolute_error(&y, &y_hat) < 1.0);
}
#[test]
fn test_random_matrix_with_wrong_rownum() {
let x_rand: DenseMatrix<f64> = DenseMatrix::<f64>::rand(17, 200);
let y = vec![
83.0, 88.5, 88.2, 89.5, 96.2, 98.1, 99.0, 100.0, 101.2, 104.6, 108.4, 110.8, 112.6,
114.2, 115.7, 116.9,
];
let fail = RandomForestRegressor::fit(
&x_rand,
&y,
RandomForestRegressorParameters {
max_depth: Option::None,
min_samples_leaf: 1,
min_samples_split: 2,
n_trees: 1000,
m: Option::None,
keep_samples: false,
seed: 87,
},
);
assert!(fail.is_err());
}
#[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
+2 -2
View File
@@ -30,7 +30,7 @@ pub enum FailedError {
DecompositionFailed,
/// Can't solve for x
SolutionFailed,
/// Erro in input
/// Error in input parameters
ParametersError,
}
@@ -98,7 +98,7 @@ impl fmt::Display for FailedError {
FailedError::SolutionFailed => "Can't find solution",
FailedError::ParametersError => "Error in input, check parameters",
};
write!(f, "{}", failed_err_str)
write!(f, "{failed_err_str}")
}
}
+2 -1
View File
@@ -3,7 +3,8 @@
clippy::too_many_arguments,
clippy::many_single_char_names,
clippy::unnecessary_wraps,
clippy::upper_case_acronyms
clippy::upper_case_acronyms,
clippy::approx_constant
)]
#![warn(missing_docs)]
#![warn(rustdoc::missing_doc_code_examples)]
+12 -29
View File
@@ -548,7 +548,7 @@ pub trait ArrayView2<T: Debug + Display + Copy + Sized>: Array<T, (usize, usize)
let (nrows, ncols) = self.shape();
for r in 0..nrows {
let row: Vec<T> = (0..ncols).map(|c| *self.get((r, c))).collect();
writeln!(f, "{:?}", row)?
writeln!(f, "{row:?}")?
}
Ok(())
}
@@ -918,8 +918,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
let len = self.shape();
assert!(
index.iter().all(|&i| i < len),
"All indices in `take` should be < {}",
len
"All indices in `take` should be < {len}"
);
Self::from_iterator(index.iter().map(move |&i| *self.get(i)), index.len())
}
@@ -990,10 +989,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
};
assert!(
d1 == len,
"Can not multiply {}x{} matrix by {} vector",
nrows,
ncols,
len
"Can not multiply {nrows}x{ncols} matrix by {len} vector"
);
let mut result = Self::zeros(d2);
for i in 0..d2 {
@@ -1111,11 +1107,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
assert!(
nrows * ncols == onrows * oncols,
"Can't reshape {}x{} array into a {}x{} array",
onrows,
oncols,
nrows,
ncols
"Can't reshape {onrows}x{oncols} array into a {nrows}x{ncols} array"
);
Self::from_iterator(self.iterator(0).cloned(), nrows, ncols, axis)
@@ -1129,11 +1121,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
let (o_nrows, o_ncols) = other.shape();
assert!(
ncols == o_nrows,
"Can't multiply {}x{} and {}x{} matrices",
nrows,
ncols,
o_nrows,
o_ncols
"Can't multiply {nrows}x{ncols} and {o_nrows}x{o_ncols} matrices"
);
let inner_d = ncols;
let mut result = Self::zeros(nrows, o_ncols);
@@ -1166,7 +1154,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
_ => (nrows, ncols, o_nrows, o_ncols),
};
if d1 != d4 {
panic!("Can not multiply {}x{} by {}x{} matrices", d2, d1, d4, d3);
panic!("Can not multiply {d2}x{d1} by {d4}x{d3} matrices");
}
let mut result = Self::zeros(d2, d3);
for r in 0..d2 {
@@ -1198,10 +1186,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
};
assert!(
d2 == len,
"Can not multiply {}x{} matrix by {} vector",
nrows,
ncols,
len
"Can not multiply {nrows}x{ncols} matrix by {len} vector"
);
let mut result = Self::zeros(d1, 1);
for i in 0..d1 {
@@ -1432,8 +1417,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
0 => {
assert!(
index.iter().all(|&i| i < nrows),
"All indices in `take` should be < {}",
nrows
"All indices in `take` should be < {nrows}"
);
Self::from_iterator(
index
@@ -1448,8 +1432,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
_ => {
assert!(
index.iter().all(|&i| i < ncols),
"All indices in `take` should be < {}",
ncols
"All indices in `take` should be < {ncols}"
);
Self::from_iterator(
(0..nrows)
@@ -1736,7 +1719,7 @@ mod tests {
let r = Vec::<f32>::rand(4);
assert!(r.iterator(0).all(|&e| e <= 1f32));
assert!(r.iterator(0).all(|&e| e >= 0f32));
assert!(r.iterator(0).map(|v| *v).sum::<f32>() > 0f32);
assert!(r.iterator(0).copied().sum::<f32>() > 0f32);
}
#[test]
@@ -1954,7 +1937,7 @@ mod tests {
DenseMatrix::from_2d_array(&[&[1, 3], &[2, 4]])
);
assert_eq!(
DenseMatrix::concatenate_2d(&[&a.clone(), &b.clone()], 0),
DenseMatrix::concatenate_2d(&[&a, &b], 0),
DenseMatrix::from_2d_array(&[&[1, 2], &[3, 4], &[5, 6], &[7, 8]])
);
assert_eq!(
@@ -2025,7 +2008,7 @@ mod tests {
let r = DenseMatrix::<f32>::rand(2, 2);
assert!(r.iterator(0).all(|&e| e <= 1f32));
assert!(r.iterator(0).all(|&e| e >= 0f32));
assert!(r.iterator(0).map(|v| *v).sum::<f32>() > 0f32);
assert!(r.iterator(0).copied().sum::<f32>() > 0f32);
}
#[test]
+9 -9
View File
@@ -581,9 +581,9 @@ mod tests {
vec![4, 5, 6],
DenseMatrix::from_slice(&(*x.slice(1..2, 0..3))).values
);
let second_row: Vec<i32> = x.slice(1..2, 0..3).iterator(0).map(|x| *x).collect();
let second_row: Vec<i32> = x.slice(1..2, 0..3).iterator(0).copied().collect();
assert_eq!(vec![4, 5, 6], second_row);
let second_col: Vec<i32> = x.slice(0..3, 1..2).iterator(0).map(|x| *x).collect();
let second_col: Vec<i32> = x.slice(0..3, 1..2).iterator(0).copied().collect();
assert_eq!(vec![2, 5, 8], second_col);
}
@@ -640,12 +640,12 @@ mod tests {
let x = DenseMatrix::<&str>::from_2d_array(&[&["1", "2", "3"], &["4", "5", "6"]]);
assert_eq!(vec!["1", "4", "2", "5", "3", "6"], x.values);
assert!(x.column_major == true);
assert!(x.column_major);
// transpose
let x = x.transpose();
assert_eq!(vec!["1", "4", "2", "5", "3", "6"], x.values);
assert!(x.column_major == false); // should change column_major
assert!(!x.column_major); // should change column_major
}
#[test]
@@ -659,7 +659,7 @@ mod tests {
vec![1, 2, 3, 4, 5, 6],
m.values.iter().map(|e| **e).collect::<Vec<i32>>()
);
assert!(m.column_major == false);
assert!(!m.column_major);
}
#[test]
@@ -667,10 +667,10 @@ mod tests {
let a = DenseMatrix::from_2d_array(&[&[1, 2, 3], &[4, 5, 6]]);
let b = DenseMatrix::from_2d_array(&[&[1, 2], &[3, 4], &[5, 6]]);
println!("{}", a);
println!("{a}");
// take column 0 and 2
assert_eq!(vec![1, 3, 4, 6], a.take(&[0, 2], 1).values);
println!("{}", b);
println!("{b}");
// take rows 0 and 2
assert_eq!(vec![1, 2, 5, 6], b.take(&[0, 2], 0).values);
}
@@ -692,11 +692,11 @@ mod tests {
let a = a.reshape(2, 6, 0);
assert_eq!(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], a.values);
assert!(a.ncols == 6 && a.nrows == 2 && a.column_major == false);
assert!(a.ncols == 6 && a.nrows == 2 && !a.column_major);
let a = a.reshape(3, 4, 1);
assert_eq!(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], a.values);
assert!(a.ncols == 4 && a.nrows == 3 && a.column_major == true);
assert!(a.ncols == 4 && a.nrows == 3 && a.column_major);
}
#[test]
+3 -3
View File
@@ -160,8 +160,8 @@ mod tests {
fn dot_product<T: Number, V: Array1<T>>(v: &V) -> T {
let vv = V::zeros(10);
let v_s = vv.slice(0..3);
let dot = v_s.dot(v);
dot
v_s.dot(v)
}
fn vector_ops<T: Number + PartialOrd, V: Array1<T>>(_: &V) -> T {
@@ -216,7 +216,7 @@ mod tests {
#[test]
fn test_mut_iterator() {
let mut x = vec![1, 2, 3];
x.iterator_mut(0).for_each(|v| *v = *v * 2);
x.iterator_mut(0).for_each(|v| *v *= 2);
assert_eq!(vec![2, 4, 6], x);
}
+6 -6
View File
@@ -217,7 +217,7 @@ mod tests {
fn test_iterator() {
let a = arr2(&[[1, 2, 3], [4, 5, 6]]);
let v: Vec<i32> = a.iterator(0).map(|&v| v).collect();
let v: Vec<i32> = a.iterator(0).copied().collect();
assert_eq!(v, vec!(1, 2, 3, 4, 5, 6));
}
@@ -236,7 +236,7 @@ mod tests {
let x = arr2(&[[1, 2, 3], [4, 5, 6]]);
let x_slice = Array2::slice(&x, 0..2, 1..2);
assert_eq!((2, 1), x_slice.shape());
let v: Vec<i32> = x_slice.iterator(0).map(|&v| v).collect();
let v: Vec<i32> = x_slice.iterator(0).copied().collect();
assert_eq!(v, [2, 5]);
}
@@ -245,11 +245,11 @@ mod tests {
let x = arr2(&[[1, 2, 3], [4, 5, 6]]);
let x_slice = Array2::slice(&x, 0..2, 0..3);
assert_eq!(
x_slice.iterator(0).map(|&v| v).collect::<Vec<i32>>(),
x_slice.iterator(0).copied().collect::<Vec<i32>>(),
vec![1, 2, 3, 4, 5, 6]
);
assert_eq!(
x_slice.iterator(1).map(|&v| v).collect::<Vec<i32>>(),
x_slice.iterator(1).copied().collect::<Vec<i32>>(),
vec![1, 4, 2, 5, 3, 6]
);
}
@@ -279,8 +279,8 @@ mod tests {
fn test_c_from_iterator() {
let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
let a: NDArray2<i32> = Array2::from_iterator(data.clone().into_iter(), 4, 3, 0);
println!("{}", a);
println!("{a}");
let a: NDArray2<i32> = Array2::from_iterator(data.into_iter(), 4, 3, 1);
println!("{}", a);
println!("{a}");
}
}
+1 -1
View File
@@ -152,7 +152,7 @@ mod tests {
fn test_iterator() {
let a = arr1(&[1, 2, 3]);
let v: Vec<i32> = a.iterator(0).map(|&v| v).collect();
let v: Vec<i32> = a.iterator(0).copied().collect();
assert_eq!(v, vec!(1, 2, 3));
}
+9 -13
View File
@@ -66,7 +66,7 @@ pub trait EVDDecomposable<T: Number + RealNumber>: Array2<T> {
fn evd_mut(mut self, symmetric: bool) -> Result<EVD<T, Self>, Failed> {
let (nrows, ncols) = self.shape();
if ncols != nrows {
panic!("Matrix is not square: {} x {}", nrows, ncols);
panic!("Matrix is not square: {nrows} x {ncols}");
}
let n = nrows;
@@ -837,10 +837,8 @@ mod tests {
evd.V.abs(),
epsilon = 1e-4
));
for i in 0..eigen_values.len() {
assert!((eigen_values[i] - evd.d[i]).abs() < 1e-4);
}
for i in 0..eigen_values.len() {
for (i, eigen_values_i) in eigen_values.iter().enumerate() {
assert!((eigen_values_i - evd.d[i]).abs() < 1e-4);
assert!((0f64 - evd.e[i]).abs() < std::f64::EPSILON);
}
}
@@ -871,10 +869,8 @@ mod tests {
evd.V.abs(),
epsilon = 1e-4
));
for i in 0..eigen_values.len() {
assert!((eigen_values[i] - evd.d[i]).abs() < 1e-4);
}
for i in 0..eigen_values.len() {
for (i, eigen_values_i) in eigen_values.iter().enumerate() {
assert!((eigen_values_i - evd.d[i]).abs() < 1e-4);
assert!((0f64 - evd.e[i]).abs() < std::f64::EPSILON);
}
}
@@ -908,11 +904,11 @@ mod tests {
evd.V.abs(),
epsilon = 1e-4
));
for i in 0..eigen_values_d.len() {
assert!((eigen_values_d[i] - evd.d[i]).abs() < 1e-4);
for (i, eigen_values_d_i) in eigen_values_d.iter().enumerate() {
assert!((eigen_values_d_i - evd.d[i]).abs() < 1e-4);
}
for i in 0..eigen_values_e.len() {
assert!((eigen_values_e[i] - evd.e[i]).abs() < 1e-4);
for (i, eigen_values_e_i) in eigen_values_e.iter().enumerate() {
assert!((eigen_values_e_i - evd.e[i]).abs() < 1e-4);
}
}
}
+2 -5
View File
@@ -126,7 +126,7 @@ impl<T: Number + RealNumber, M: Array2<T>> LU<T, M> {
let (m, n) = self.LU.shape();
if m != n {
panic!("Matrix is not square: {}x{}", m, n);
panic!("Matrix is not square: {m}x{n}");
}
let mut inv = M::zeros(n, n);
@@ -143,10 +143,7 @@ impl<T: Number + RealNumber, M: Array2<T>> LU<T, M> {
let (b_m, b_n) = b.shape();
if b_m != m {
panic!(
"Row dimensions do not agree: A is {} x {}, but B is {} x {}",
m, n, b_m, b_n
);
panic!("Row dimensions do not agree: A is {m} x {n}, but B is {b_m} x {b_n}");
}
if self.singular {
+1 -4
View File
@@ -102,10 +102,7 @@ impl<T: Number + RealNumber, M: Array2<T>> QR<T, M> {
let (b_nrows, b_ncols) = b.shape();
if b_nrows != m {
panic!(
"Row dimensions do not agree: A is {} x {}, but B is {} x {}",
m, n, b_nrows, b_ncols
);
panic!("Row dimensions do not agree: A is {m} x {n}, but B is {b_nrows} x {b_ncols}");
}
if self.singular {
+1 -1
View File
@@ -286,7 +286,7 @@ mod tests {
}
{
let mut m = m.clone();
let mut m = m;
m.standard_scale_mut(&m.mean(1), &m.std(1), 1);
assert_eq!(&m, &expected_1);
}
+4 -4
View File
@@ -509,8 +509,8 @@ mod tests {
assert!(relative_eq!(V.abs(), svd.V.abs(), epsilon = 1e-4));
assert!(relative_eq!(U.abs(), svd.U.abs(), epsilon = 1e-4));
for i in 0..s.len() {
assert!((s[i] - svd.s[i]).abs() < 1e-4);
for (i, s_i) in s.iter().enumerate() {
assert!((s_i - svd.s[i]).abs() < 1e-4);
}
}
#[cfg_attr(
@@ -713,8 +713,8 @@ mod tests {
assert!(relative_eq!(V.abs(), svd.V.abs(), epsilon = 1e-4));
assert!(relative_eq!(U.abs(), svd.U.abs(), epsilon = 1e-4));
for i in 0..s.len() {
assert!((s[i] - svd.s[i]).abs() < 1e-4);
for (i, s_i) in s.iter().enumerate() {
assert!((s_i - svd.s[i]).abs() < 1e-4);
}
}
#[cfg_attr(
+1 -4
View File
@@ -425,10 +425,7 @@ impl<TX: FloatNumber + RealNumber, TY: Number, X: Array2<TX>, Y: Array1<TY>>
for (i, col_std_i) in col_std.iter().enumerate() {
if (*col_std_i - TX::zero()).abs() < TX::epsilon() {
return Err(Failed::fit(&format!(
"Cannot rescale constant column {}",
i
)));
return Err(Failed::fit(&format!("Cannot rescale constant column {i}")));
}
}
+1 -4
View File
@@ -356,10 +356,7 @@ impl<TX: FloatNumber + RealNumber, TY: Number, X: Array2<TX>, Y: Array1<TY>> Las
for (i, col_std_i) in col_std.iter().enumerate() {
if (*col_std_i - TX::zero()).abs() < TX::epsilon() {
return Err(Failed::fit(&format!(
"Cannot rescale constant column {}",
i
)));
return Err(Failed::fit(&format!("Cannot rescale constant column {i}")));
}
}
+9 -15
View File
@@ -71,19 +71,14 @@ use crate::optimization::line_search::Backtracking;
use crate::optimization::FunctionOrder;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone, Eq, PartialEq, Default)]
/// Solver options for Logistic regression. Right now only LBFGS solver is supported.
pub enum LogisticRegressionSolverName {
/// Limited-memory BroydenFletcherGoldfarbShanno method, see [LBFGS paper](http://users.iems.northwestern.edu/~nocedal/lbfgsb.html)
#[default]
LBFGS,
}
impl Default for LogisticRegressionSolverName {
fn default() -> Self {
LogisticRegressionSolverName::LBFGS
}
}
/// Logistic Regression parameters
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
@@ -449,8 +444,7 @@ impl<TX: Number + FloatNumber + RealNumber, TY: Number + Ord, X: Array2<TX>, Y:
match k.cmp(&2) {
Ordering::Less => Err(Failed::fit(&format!(
"incorrect number of classes: {}. Should be >= 2.",
k
"incorrect number of classes: {k}. Should be >= 2."
))),
Ordering::Equal => {
let x0 = Vec::zeros(num_attributes + 1);
@@ -636,19 +630,19 @@ mod tests {
assert!((g[0] + 33.000068218163484).abs() < std::f64::EPSILON);
let f = objective.f(&vec![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);
let objective_reg = MultiClassObjectiveFunction {
x: &x,
y: y.clone(),
y,
k: 3,
alpha: 1.0,
_phantom_t: PhantomData,
};
let f = objective_reg.f(&vec![1., 2., 3., 4., 5., 6., 7., 8., 9.]);
let f = objective_reg.f(&[1., 2., 3., 4., 5., 6., 7., 8., 9.]);
assert!((f - 487.5052).abs() < 1e-4);
objective_reg.df(&mut g, &vec![1., 2., 3., 4., 5., 6., 7., 8., 9.]);
@@ -697,18 +691,18 @@ mod tests {
assert!((g[1] - 10.239000702928523).abs() < std::f64::EPSILON);
assert!((g[2] - 3.869294270156324).abs() < std::f64::EPSILON);
let f = objective.f(&vec![1., 2., 3.]);
let f = objective.f(&[1., 2., 3.]);
assert!((f - 59.76994756647412).abs() < std::f64::EPSILON);
let objective_reg = BinaryObjectiveFunction {
x: &x,
y: y.clone(),
y,
alpha: 1.0,
_phantom_t: PhantomData,
};
let f = objective_reg.f(&vec![1., 2., 3.]);
let f = objective_reg.f(&[1., 2., 3.]);
assert!((f - 62.2699).abs() < 1e-4);
objective_reg.df(&mut g, &vec![1., 2., 3.]);
+3 -11
View File
@@ -71,21 +71,16 @@ use crate::numbers::basenum::Number;
use crate::numbers::realnum::RealNumber;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone, Eq, PartialEq, Default)]
/// Approach to use for estimation of regression coefficients. Cholesky is more efficient but SVD is more stable.
pub enum RidgeRegressionSolverName {
/// Cholesky decomposition, see [Cholesky](../../linalg/cholesky/index.html)
#[default]
Cholesky,
/// SVD decomposition, see [SVD](../../linalg/svd/index.html)
SVD,
}
impl Default for RidgeRegressionSolverName {
fn default() -> Self {
RidgeRegressionSolverName::Cholesky
}
}
/// Ridge Regression parameters
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
@@ -384,10 +379,7 @@ impl<
for (i, col_std_i) in col_std.iter().enumerate() {
if (*col_std_i - TX::zero()).abs() < TX::epsilon() {
return Err(Failed::fit(&format!(
"Cannot rescale constant column {}",
i
)));
return Err(Failed::fit(&format!("Cannot rescale constant column {i}")));
}
}
+3 -3
View File
@@ -98,8 +98,8 @@ mod tests {
let mut scores = HCVScore::new();
scores.compute(&v1, &v2);
assert!((0.2548 - scores.homogeneity.unwrap() as f64).abs() < 1e-4);
assert!((0.5440 - scores.completeness.unwrap() as f64).abs() < 1e-4);
assert!((0.3471 - scores.v_measure.unwrap() as f64).abs() < 1e-4);
assert!((0.2548 - scores.homogeneity.unwrap()).abs() < 1e-4);
assert!((0.5440 - scores.completeness.unwrap()).abs() < 1e-4);
assert!((0.3471 - scores.v_measure.unwrap()).abs() < 1e-4);
}
}
+1 -1
View File
@@ -125,7 +125,7 @@ mod tests {
fn entropy_test() {
let v1 = vec![0, 0, 1, 1, 2, 0, 4];
assert!((1.2770 - entropy(&v1).unwrap() as f64).abs() < 1e-4);
assert!((1.2770 - entropy(&v1).unwrap()).abs() < 1e-4);
}
#[cfg_attr(
+2 -2
View File
@@ -95,8 +95,8 @@ mod tests {
let score1: f64 = F1::new_with(beta).get_score(&y_true, &y_pred);
let score2: f64 = F1::new_with(beta).get_score(&y_true, &y_true);
println!("{:?}", score1);
println!("{:?}", score2);
println!("{score1:?}");
println!("{score2:?}");
assert!((score1 - 0.57142857).abs() < 1e-8);
assert!((score2 - 1.0).abs() < 1e-8);
+4 -4
View File
@@ -213,17 +213,17 @@ mod tests {
for t in &test_masks[0][0..11] {
// TODO: this can be prob done better
assert_eq!(*t, true)
assert!(*t)
}
for t in &test_masks[0][11..22] {
assert_eq!(*t, false)
assert!(!*t)
}
for t in &test_masks[1][0..11] {
assert_eq!(*t, false)
assert!(!*t)
}
for t in &test_masks[1][11..22] {
assert_eq!(*t, true)
assert!(*t)
}
}
+2 -2
View File
@@ -169,7 +169,7 @@ pub fn train_test_split<
let n_test = ((n as f32) * test_size) as usize;
if n_test < 1 {
panic!("number of sample is too small {}", n);
panic!("number of sample is too small {n}");
}
let mut indices: Vec<usize> = (0..n).collect();
@@ -553,6 +553,6 @@ mod tests {
&accuracy,
)
.unwrap();
println!("{:?}", results);
println!("{results:?}");
}
}
+4 -8
View File
@@ -271,21 +271,18 @@ impl<TY: Number + Ord + Unsigned> BernoulliNBDistribution<TY> {
let y_samples = y.shape();
if y_samples != n_samples {
return Err(Failed::fit(&format!(
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
n_samples, y_samples
"Size of x should equal size of y; |x|=[{n_samples}], |y|=[{y_samples}]"
)));
}
if n_samples == 0 {
return Err(Failed::fit(&format!(
"Size of x and y should greater than 0; |x|=[{}]",
n_samples
"Size of x and y should greater than 0; |x|=[{n_samples}]"
)));
}
if alpha < 0f64 {
return Err(Failed::fit(&format!(
"Alpha should be greater than 0; |alpha|=[{}]",
alpha
"Alpha should be greater than 0; |alpha|=[{alpha}]"
)));
}
@@ -318,8 +315,7 @@ impl<TY: Number + Ord + Unsigned> BernoulliNBDistribution<TY> {
feature_in_class_counter[class_index][idx] +=
row_i.to_usize().ok_or_else(|| {
Failed::fit(&format!(
"Elements of the matrix should be 1.0 or 0.0 |found|=[{}]",
row_i
"Elements of the matrix should be 1.0 or 0.0 |found|=[{row_i}]"
))
})?;
}
+4 -9
View File
@@ -158,8 +158,7 @@ impl<T: Number + Unsigned> CategoricalNBDistribution<T> {
pub fn fit<X: Array2<T>, Y: Array1<T>>(x: &X, y: &Y, alpha: f64) -> Result<Self, Failed> {
if alpha < 0f64 {
return Err(Failed::fit(&format!(
"alpha should be >= 0, alpha=[{}]",
alpha
"alpha should be >= 0, alpha=[{alpha}]"
)));
}
@@ -167,15 +166,13 @@ impl<T: Number + Unsigned> CategoricalNBDistribution<T> {
let y_samples = y.shape();
if y_samples != n_samples {
return Err(Failed::fit(&format!(
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
n_samples, y_samples
"Size of x should equal size of y; |x|=[{n_samples}], |y|=[{y_samples}]"
)));
}
if n_samples == 0 {
return Err(Failed::fit(&format!(
"Size of x and y should greater than 0; |x|=[{}]",
n_samples
"Size of x and y should greater than 0; |x|=[{n_samples}]"
)));
}
let y: Vec<usize> = y.iterator(0).map(|y_i| y_i.to_usize().unwrap()).collect();
@@ -202,8 +199,7 @@ impl<T: Number + Unsigned> CategoricalNBDistribution<T> {
.max()
.ok_or_else(|| {
Failed::fit(&format!(
"Failed to get the categories for feature = {}",
feature
"Failed to get the categories for feature = {feature}"
))
})?;
n_categories.push(feature_max + 1);
@@ -429,7 +425,6 @@ mod tests {
fn search_parameters() {
let parameters = CategoricalNBSearchParameters {
alpha: vec![1., 2.],
..Default::default()
};
let mut iter = parameters.into_iter();
let next = iter.next().unwrap();
+2 -5
View File
@@ -185,15 +185,13 @@ impl<TY: Number + Ord + Unsigned> GaussianNBDistribution<TY> {
let y_samples = y.shape();
if y_samples != n_samples {
return Err(Failed::fit(&format!(
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
n_samples, y_samples
"Size of x should equal size of y; |x|=[{n_samples}], |y|=[{y_samples}]"
)));
}
if n_samples == 0 {
return Err(Failed::fit(&format!(
"Size of x and y should greater than 0; |x|=[{}]",
n_samples
"Size of x and y should greater than 0; |x|=[{n_samples}]"
)));
}
let (class_labels, indices) = y.unique_with_indices();
@@ -375,7 +373,6 @@ mod tests {
fn search_parameters() {
let parameters = GaussianNBSearchParameters {
priors: vec![Some(vec![1.]), Some(vec![2.])],
..Default::default()
};
let mut iter = parameters.into_iter();
let next = iter.next().unwrap();
+4 -8
View File
@@ -220,21 +220,18 @@ impl<TY: Number + Ord + Unsigned> MultinomialNBDistribution<TY> {
let y_samples = y.shape();
if y_samples != n_samples {
return Err(Failed::fit(&format!(
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
n_samples, y_samples
"Size of x should equal size of y; |x|=[{n_samples}], |y|=[{y_samples}]"
)));
}
if n_samples == 0 {
return Err(Failed::fit(&format!(
"Size of x and y should greater than 0; |x|=[{}]",
n_samples
"Size of x and y should greater than 0; |x|=[{n_samples}]"
)));
}
if alpha < 0f64 {
return Err(Failed::fit(&format!(
"Alpha should be greater than 0; |alpha|=[{}]",
alpha
"Alpha should be greater than 0; |alpha|=[{alpha}]"
)));
}
@@ -266,8 +263,7 @@ impl<TY: Number + Ord + Unsigned> MultinomialNBDistribution<TY> {
feature_in_class_counter[class_index][idx] +=
row_i.to_usize().ok_or_else(|| {
Failed::fit(&format!(
"Elements of the matrix should be convertible to usize |found|=[{}]",
row_i
"Elements of the matrix should be convertible to usize |found|=[{row_i}]"
))
})?;
}
+1 -2
View File
@@ -236,8 +236,7 @@ impl<TX: Number, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>, D: Distance<Vec
if x_n != y_n {
return Err(Failed::fit(&format!(
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
x_n, y_n
"Size of x should equal size of y; |x|=[{x_n}], |y|=[{y_n}]"
)));
}
+1 -2
View File
@@ -224,8 +224,7 @@ impl<TX: Number, TY: Number, X: Array2<TX>, Y: Array1<TY>, D: Distance<Vec<TX>>>
if x_n != y_n {
return Err(Failed::fit(&format!(
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
x_n, y_n
"Size of x should equal size of y; |x|=[{x_n}], |y|=[{y_n}]"
)));
}
+2 -7
View File
@@ -49,20 +49,15 @@ pub type KNNAlgorithmName = crate::algorithm::neighbour::KNNAlgorithmName;
/// Weight function that is used to determine estimated value.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub enum KNNWeightFunction {
/// All k nearest points are weighted equally
#[default]
Uniform,
/// k nearest points are weighted by the inverse of their distance. Closer neighbors will have a greater influence than neighbors which are further away.
Distance,
}
impl Default for KNNWeightFunction {
fn default() -> Self {
KNNWeightFunction::Uniform
}
}
impl KNNWeightFunction {
fn calc_weights(&self, distances: Vec<f64>) -> std::vec::Vec<f64> {
match *self {
+26 -3
View File
@@ -2,9 +2,13 @@
//! Most algorithms in `smartcore` rely on basic linear algebra operations like dot product, matrix decomposition and other subroutines that are defined for a set of real numbers, .
//! This module defines real number and some useful functions that are used in [Linear Algebra](../../linalg/index.html) module.
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
use num_traits::Float;
use crate::numbers::basenum::Number;
use crate::rand_custom::get_rng_impl;
/// Defines real number
/// <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_CHTML"></script>
@@ -63,8 +67,12 @@ impl RealNumber for f64 {
}
fn rand() -> f64 {
// TODO: to be implemented, see issue smartcore#214
1.0
let mut small_rng = get_rng_impl(None);
let mut rngs: Vec<SmallRng> = (0..3)
.map(|_| SmallRng::from_rng(&mut small_rng).unwrap())
.collect();
rngs[0].gen::<f64>()
}
fn two() -> Self {
@@ -108,7 +116,12 @@ impl RealNumber for f32 {
}
fn rand() -> f32 {
1.0
let mut small_rng = get_rng_impl(None);
let mut rngs: Vec<SmallRng> = (0..3)
.map(|_| SmallRng::from_rng(&mut small_rng).unwrap())
.collect();
rngs[0].gen::<f32>()
}
fn two() -> Self {
@@ -149,4 +162,14 @@ mod tests {
fn f64_from_string() {
assert_eq!(f64::from_str("1.111111111").unwrap(), 1.111111111)
}
#[test]
fn f64_rand() {
f64::rand();
}
#[test]
fn f32_rand() {
f32::rand();
}
}
@@ -113,12 +113,13 @@ mod tests {
g[1] = 200. * (x[1] - x[0].powf(2.));
};
let mut ls: Backtracking<f64> = Default::default();
ls.order = FunctionOrder::THIRD;
let ls: Backtracking<f64> = Backtracking::<f64> {
order: FunctionOrder::THIRD,
..Default::default()
};
let optimizer: GradientDescent = Default::default();
let result = optimizer.optimize(&f, &df, &x0, &ls);
println!("{:?}", result);
assert!((result.f_x - 0.0).abs() < 1e-5);
assert!((result.x[0] - 1.0).abs() < 1e-2);
+6 -4
View File
@@ -196,9 +196,9 @@ impl LBFGS {
}
///
fn update_hessian<'a, T: FloatNumber, X: Array1<T>>(
fn update_hessian<T: FloatNumber, X: Array1<T>>(
&self,
_: &'a DF<'_, X>,
_: &DF<'_, X>,
state: &mut LBFGSState<T, X>,
) {
state.dg = state.x_df.sub(&state.x_df_prev);
@@ -291,8 +291,10 @@ mod tests {
g[0] = -2. * (1. - x[0]) - 400. * (x[1] - x[0].powf(2.)) * x[0];
g[1] = 200. * (x[1] - x[0].powf(2.));
};
let mut ls: Backtracking<f64> = Default::default();
ls.order = FunctionOrder::THIRD;
let ls: Backtracking<f64> = Backtracking::<f64> {
order: FunctionOrder::THIRD,
..Default::default()
};
let optimizer: LBFGS = Default::default();
let result = optimizer.optimize(&f, &df, &x0, &ls);
+4 -9
View File
@@ -132,8 +132,7 @@ impl OneHotEncoder {
data.copy_col_as_vec(idx, &mut col_buf);
if !validate_col_is_categorical(&col_buf) {
let msg = format!(
"Column {} of data matrix containts non categorizable (integer) values",
idx
"Column {idx} of data matrix containts non categorizable (integer) values"
);
return Err(Failed::fit(&msg[..]));
}
@@ -182,7 +181,7 @@ impl OneHotEncoder {
match oh_vec {
None => {
// Since we support T types, bad value in a series causes in to be invalid
let msg = format!("At least one value in column {} doesn't conform to category definition", old_cidx);
let msg = format!("At least one value in column {old_cidx} doesn't conform to category definition");
return Err(Failed::transform(&msg[..]));
}
Some(v) => {
@@ -338,11 +337,7 @@ mod tests {
]);
let params = OneHotEncoderParams::from_cat_idx(&[1]);
match OneHotEncoder::fit(&m, params) {
Err(_) => {
assert!(true);
}
_ => assert!(false),
}
let result = OneHotEncoder::fit(&m, params);
assert!(result.is_err());
}
}
+1 -1
View File
@@ -294,7 +294,7 @@ mod tests {
&[0.5708488802, 0.1846414616, 0.9590802982, 0.5591871046],
&[0.8387612750, 0.5754861361, 0.5537109852, 0.1077646442],
]));
println!("{}", transformed_values);
println!("{transformed_values}");
assert!(transformed_values.approximate_eq(
&DenseMatrix::from_2d_array(&[
&[-1.1154020653, -0.4031985330, 0.9284605204, -0.4271473866],
+4 -4
View File
@@ -206,7 +206,7 @@ mod tests {
#[test]
fn from_categories() {
let fake_categories: Vec<usize> = vec![1, 2, 3, 4, 5, 3, 5, 3, 1, 2, 4];
let it = fake_categories.iter().map(|&a| a);
let it = fake_categories.iter().copied();
let enc = CategoryMapper::<usize>::fit_to_iter(it);
let oh_vec: Vec<f64> = match enc.get_one_hot(&1) {
None => panic!("Wrong categories"),
@@ -218,8 +218,8 @@ mod tests {
fn build_fake_str_enc<'a>() -> CategoryMapper<&'a str> {
let fake_category_pos = vec!["background", "dog", "cat"];
let enc = CategoryMapper::<&str>::from_positional_category_vec(fake_category_pos);
enc
CategoryMapper::<&str>::from_positional_category_vec(fake_category_pos)
}
#[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
@@ -275,7 +275,7 @@ mod tests {
let lab = enc.invert_one_hot(res).unwrap();
assert_eq!(lab, "dog");
if let Err(e) = enc.invert_one_hot(vec![0.0, 0.0, 0.0]) {
let pos_entries = format!("Expected a single positive entry, 0 entires found");
let pos_entries = "Expected a single positive entry, 0 entires found".to_string();
assert_eq!(e, Failed::transform(&pos_entries[..]));
};
}
+2 -2
View File
@@ -167,7 +167,7 @@ where
}
/// Ensure that a string containing a csv row conforms to a specified row format.
fn validate_csv_row<'a>(row: &'a str, row_format: &CSVRowFormat<'_>) -> Result<(), ReadingError> {
fn validate_csv_row(row: &str, row_format: &CSVRowFormat<'_>) -> Result<(), ReadingError> {
let actual_number_of_fields = row.split(row_format.field_seperator).count();
if row_format.n_fields == actual_number_of_fields {
Ok(())
@@ -208,7 +208,7 @@ where
match value_string.parse::<T>().ok() {
Some(value) => Ok(value),
None => Err(ReadingError::InvalidField {
msg: format!("Value '{}' could not be read.", value_string,),
msg: format!("Value '{value_string}' could not be read.",),
}),
}
}
+2 -10
View File
@@ -983,11 +983,7 @@ mod tests {
.unwrap();
let acc = accuracy(&y, &(y_hat.iter().map(|e| e.to_i32().unwrap()).collect()));
assert!(
acc >= 0.9,
"accuracy ({}) is not larger or equal to 0.9",
acc
);
assert!(acc >= 0.9, "accuracy ({acc}) is not larger or equal to 0.9");
}
#[cfg_attr(
@@ -1076,11 +1072,7 @@ mod tests {
let acc = accuracy(&y, &(y_hat.iter().map(|e| e.to_i32().unwrap()).collect()));
assert!(
acc >= 0.9,
"accuracy ({}) is not larger or equal to 0.9",
acc
);
assert!(acc >= 0.9, "accuracy ({acc}) is not larger or equal to 0.9");
}
#[cfg_attr(
+1 -1
View File
@@ -662,7 +662,7 @@ mod tests {
.unwrap();
let t = mean_squared_error(&y_hat, &y);
println!("{:?}", t);
println!("{t:?}");
assert!(t < 2.5);
}
+22 -15
View File
@@ -137,16 +137,17 @@ impl<TX: Number + PartialOrd, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>>
self.classes.as_ref()
}
/// Get depth of tree
fn depth(&self) -> u16 {
pub fn depth(&self) -> u16 {
self.depth
}
}
/// The function to measure the quality of a split.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub enum SplitCriterion {
/// [Gini index](../decision_tree_classifier/index.html)
#[default]
Gini,
/// [Entropy](../decision_tree_classifier/index.html)
Entropy,
@@ -154,12 +155,6 @@ pub enum SplitCriterion {
ClassificationError,
}
impl Default for SplitCriterion {
fn default() -> Self {
SplitCriterion::Gini
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
struct Node {
@@ -543,6 +538,10 @@ impl<TX: Number + PartialOrd, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>>
parameters: DecisionTreeClassifierParameters,
) -> Result<DecisionTreeClassifier<TX, TY, X, Y>, Failed> {
let (x_nrows, num_attributes) = x.shape();
if x_nrows != y.shape() {
return Err(Failed::fit("Size of x should equal size of y"));
}
let samples = vec![1; x_nrows];
DecisionTreeClassifier::fit_weak_learner(x, y, samples, num_attributes, parameters)
}
@@ -560,8 +559,7 @@ impl<TX: Number + PartialOrd, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>>
let k = classes.len();
if k < 2 {
return Err(Failed::fit(&format!(
"Incorrect number of classes: {}. Should be >= 2.",
k
"Incorrect number of classes: {k}. Should be >= 2."
)));
}
@@ -901,15 +899,13 @@ mod tests {
)]
#[test]
fn gini_impurity() {
assert!((impurity(&SplitCriterion::Gini, &[7, 3], 10) - 0.42).abs() < std::f64::EPSILON);
assert!(
(impurity(&SplitCriterion::Gini, &vec![7, 3], 10) - 0.42).abs() < std::f64::EPSILON
);
assert!(
(impurity(&SplitCriterion::Entropy, &vec![7, 3], 10) - 0.8812908992306927).abs()
(impurity(&SplitCriterion::Entropy, &[7, 3], 10) - 0.8812908992306927).abs()
< std::f64::EPSILON
);
assert!(
(impurity(&SplitCriterion::ClassificationError, &vec![7, 3], 10) - 0.3).abs()
(impurity(&SplitCriterion::ClassificationError, &[7, 3], 10) - 0.3).abs()
< std::f64::EPSILON
);
}
@@ -971,6 +967,17 @@ mod tests {
);
}
#[test]
fn test_random_matrix_with_wrong_rownum() {
let x_rand: DenseMatrix<f64> = DenseMatrix::<f64>::rand(21, 200);
let y: Vec<u32> = vec![0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
let fail = DecisionTreeClassifier::fit(&x_rand, &y, Default::default());
assert!(fail.is_err());
}
#[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
+4 -1
View File
@@ -18,7 +18,6 @@
//! Example:
//!
//! ```
//! use rand::thread_rng;
//! use smartcore::linalg::basic::matrix::DenseMatrix;
//! use smartcore::tree::decision_tree_regressor::*;
//!
@@ -422,6 +421,10 @@ impl<TX: Number + PartialOrd, TY: Number, X: Array2<TX>, Y: Array1<TY>>
parameters: DecisionTreeRegressorParameters,
) -> Result<DecisionTreeRegressor<TX, TY, X, Y>, Failed> {
let (x_nrows, num_attributes) = x.shape();
if x_nrows != y.shape() {
return Err(Failed::fit("Size of x should equal size of y"));
}
let samples = vec![1; x_nrows];
DecisionTreeRegressor::fit_weak_learner(x, y, samples, num_attributes, parameters)
}