Merge branch 'development' into prdct-prb

This commit is contained in:
Lorenzo
2022-11-03 11:59:46 +00:00
committed by GitHub
78 changed files with 1237 additions and 710 deletions
+44 -3
View File
@@ -19,19 +19,20 @@ jobs:
{ os: "ubuntu", target: "i686-unknown-linux-gnu" }, { os: "ubuntu", target: "i686-unknown-linux-gnu" },
{ os: "ubuntu", target: "wasm32-unknown-unknown" }, { os: "ubuntu", target: "wasm32-unknown-unknown" },
{ os: "macos", target: "aarch64-apple-darwin" }, { os: "macos", target: "aarch64-apple-darwin" },
{ os: "ubuntu", target: "wasm32-wasi" },
] ]
env: env:
TZ: "/usr/share/zoneinfo/your/location" TZ: "/usr/share/zoneinfo/your/location"
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v3
- name: Cache .cargo and target - name: Cache .cargo and target
uses: actions/cache@v2 uses: actions/cache@v2
with: with:
path: | path: |
~/.cargo ~/.cargo
./target ./target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }} key: ${{ runner.os }}-cargo-${{ matrix.platform.target }}-${{ hashFiles('**/Cargo.toml') }}
restore-keys: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }} restore-keys: ${{ runner.os }}-cargo-${{ matrix.platform.target }}-${{ hashFiles('**/Cargo.toml') }}
- name: Install Rust toolchain - name: Install Rust toolchain
uses: actions-rs/toolchain@v1 uses: actions-rs/toolchain@v1
with: with:
@@ -42,6 +43,9 @@ jobs:
- name: Install test runner for wasm - name: Install test runner for wasm
if: matrix.platform.target == 'wasm32-unknown-unknown' if: matrix.platform.target == 'wasm32-unknown-unknown'
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- name: Install test runner for wasi
if: matrix.platform.target == 'wasm32-wasi'
run: curl https://wasmtime.dev/install.sh -sSf | bash
- name: Stable Build - name: Stable Build
uses: actions-rs/cargo@v1 uses: actions-rs/cargo@v1
with: with:
@@ -56,3 +60,40 @@ jobs:
- name: Tests in WASM - name: Tests in WASM
if: matrix.platform.target == 'wasm32-unknown-unknown' if: matrix.platform.target == 'wasm32-unknown-unknown'
run: wasm-pack test --node -- --all-features run: wasm-pack test --node -- --all-features
- name: Tests in WASI
if: matrix.platform.target == 'wasm32-wasi'
run: |
export WASMTIME_HOME="$HOME/.wasmtime"
export PATH="$WASMTIME_HOME/bin:$PATH"
cargo install cargo-wasi && cargo wasi test
check_features:
runs-on: "${{ matrix.platform.os }}-latest"
strategy:
matrix:
platform: [{ os: "ubuntu" }]
features: ["--features serde", "--features datasets", ""]
env:
TZ: "/usr/share/zoneinfo/your/location"
steps:
- uses: actions/checkout@v3
- name: Cache .cargo and target
uses: actions/cache@v2
with:
path: |
~/.cargo
./target
key: ${{ runner.os }}-cargo-features-${{ hashFiles('**/Cargo.toml') }}
restore-keys: ${{ runner.os }}-cargo-features-${{ hashFiles('**/Cargo.toml') }}
- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: ${{ matrix.platform.target }}
profile: minimal
default: true
- name: Stable Build
uses: actions-rs/cargo@v1
with:
command: build
args: --no-default-features ${{ matrix.features }}
+13 -13
View File
@@ -12,38 +12,38 @@ readme = "README.md"
keywords = ["machine-learning", "statistical", "ai", "optimization", "linear-algebra"] keywords = ["machine-learning", "statistical", "ai", "optimization", "linear-algebra"]
categories = ["science"] categories = ["science"]
[features]
default = ["datasets", "serde"]
ndarray-bindings = ["ndarray"]
datasets = ["rand_distr", "std"]
std = ["rand/std", "rand/std_rng"]
# wasm32 only
js = ["getrandom/js"]
[dependencies] [dependencies]
approx = "0.5.1" approx = "0.5.1"
cfg-if = "1.0.0" cfg-if = "1.0.0"
ndarray = { version = "0.15", optional = true } ndarray = { version = "0.15", optional = true }
num-traits = "0.2.12" num-traits = "0.2.12"
num = "0.4" num = "0.4"
rand = { version = "0.8", default-features = false, features = ["small_rng"] } rand = { version = "0.8.5", default-features = false, features = ["small_rng"] }
rand_distr = { version = "0.4", optional = true } rand_distr = { version = "0.4", optional = true }
serde = { version = "1", features = ["derive"], optional = true } serde = { version = "1", features = ["derive"], optional = true }
[features]
default = ["serde", "datasets"]
serde = ["dep:serde"]
ndarray-bindings = ["dep:ndarray"]
datasets = ["dep:rand_distr", "std"]
std = ["rand/std_rng", "rand/std"]
# wasm32 only
js = ["getrandom/js"]
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", optional = true } getrandom = { version = "0.2", optional = true }
[dev-dependencies] [dev-dependencies]
itertools = "*"
criterion = { version = "0.4", default-features = false } criterion = { version = "0.4", default-features = false }
serde_json = "1.0" serde_json = "1.0"
bincode = "1.3.1" bincode = "1.3.1"
[target.'cfg(target_arch = "wasm32")'.dev-dependencies] [target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dev-dependencies]
wasm-bindgen-test = "0.3" wasm-bindgen-test = "0.3"
[profile.bench] [workspace]
debug = true
resolver = "2" resolver = "2"
[profile.test] [profile.test]
+4 -1
View File
@@ -316,7 +316,10 @@ mod tests {
use super::*; use super::*;
use crate::linalg::basic::matrix::DenseMatrix; use crate::linalg::basic::matrix::DenseMatrix;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn bbdtree_iris() { fn bbdtree_iris() {
let data = DenseMatrix::from_2d_array(&[ let data = DenseMatrix::from_2d_array(&[
+12 -3
View File
@@ -468,7 +468,10 @@ mod tests {
} }
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn cover_tree_test() { fn cover_tree_test() {
let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9]; let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
@@ -485,7 +488,10 @@ mod tests {
let knn: Vec<i32> = knn.iter().map(|v| *v.2).collect(); let knn: Vec<i32> = knn.iter().map(|v| *v.2).collect();
assert_eq!(vec!(3, 4, 5, 6, 7), knn); assert_eq!(vec!(3, 4, 5, 6, 7), knn);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn cover_tree_test1() { fn cover_tree_test1() {
let data = vec![ let data = vec![
@@ -504,7 +510,10 @@ mod tests {
assert_eq!(vec!(0, 1, 2), knn); assert_eq!(vec!(0, 1, 2), knn);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
fn serde() { fn serde() {
-48
View File
@@ -1,48 +0,0 @@
//!
//! Dissimilarities for vector-vector distance
//!
//! Representing distances as pairwise dissimilarities, so to build a
//! graph of closest neighbours. This representation can be reused for
//! different implementations (initially used in this library for FastPair).
use std::cmp::{Eq, Ordering, PartialOrd};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::numbers::realnum::RealNumber;
///
/// The edge of the subgraph is defined by `PairwiseDistance`.
/// The calling algorithm can store a list of distsances as
/// a list of these structures.
///
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy)]
pub struct PairwiseDistance<T: RealNumber> {
/// index of the vector in the original `Matrix` or list
pub node: usize,
/// index of the closest neighbor in the original `Matrix` or same list
pub neighbour: Option<usize>,
/// measure of distance, according to the algorithm distance function
/// if the distance is None, the edge has value "infinite" or max distance
/// each algorithm has to match
pub distance: Option<T>,
}
impl<T: RealNumber> Eq for PairwiseDistance<T> {}
impl<T: RealNumber> PartialEq for PairwiseDistance<T> {
fn eq(&self, other: &Self) -> bool {
self.node == other.node
&& self.neighbour == other.neighbour
&& self.distance == other.distance
}
}
impl<T: RealNumber> PartialOrd for PairwiseDistance<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.distance.partial_cmp(&other.distance)
}
}
+77 -50
View File
@@ -1,5 +1,5 @@
/// ///
/// # FastPair: Data-structure for the dynamic closest-pair problem. /// ### FastPair: Data-structure for the dynamic closest-pair problem.
/// ///
/// Reference: /// Reference:
/// Eppstein, David: Fast hierarchical clustering and other applications of /// Eppstein, David: Fast hierarchical clustering and other applications of
@@ -7,8 +7,8 @@
/// ///
/// Example: /// Example:
/// ``` /// ```
/// use smartcore::algorithm::neighbour::distances::PairwiseDistance; /// use smartcore::metrics::distance::PairwiseDistance;
/// use smartcore::linalg::naive::dense_matrix::DenseMatrix; /// use smartcore::linalg::basic::matrix::DenseMatrix;
/// use smartcore::algorithm::neighbour::fastpair::FastPair; /// use smartcore::algorithm::neighbour::fastpair::FastPair;
/// let x = DenseMatrix::<f64>::from_2d_array(&[ /// let x = DenseMatrix::<f64>::from_2d_array(&[
/// &[5.1, 3.5, 1.4, 0.2], /// &[5.1, 3.5, 1.4, 0.2],
@@ -25,12 +25,14 @@
/// <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script> /// <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
use std::collections::HashMap; use std::collections::HashMap;
use crate::algorithm::neighbour::distances::PairwiseDistance; use num::Bounded;
use crate::error::{Failed, FailedError}; use crate::error::{Failed, FailedError};
use crate::linalg::basic::arrays::Array2; use crate::linalg::basic::arrays::{Array1, Array2};
use crate::metrics::distance::euclidian::Euclidian; use crate::metrics::distance::euclidian::Euclidian;
use crate::numbers::realnum::RealNumber; use crate::metrics::distance::PairwiseDistance;
use crate::numbers::floatnum::FloatNumber; use crate::numbers::floatnum::FloatNumber;
use crate::numbers::realnum::RealNumber;
/// ///
/// Inspired by Python implementation: /// Inspired by Python implementation:
@@ -98,7 +100,7 @@ impl<'a, T: RealNumber + FloatNumber, M: Array2<T>> FastPair<'a, T, M> {
PairwiseDistance { PairwiseDistance {
node: index_row_i, node: index_row_i,
neighbour: Option::None, neighbour: Option::None,
distance: Some(T::MAX), distance: Some(<T as Bounded>::max_value()),
}, },
); );
} }
@@ -119,13 +121,19 @@ impl<'a, T: RealNumber + FloatNumber, M: Array2<T>> FastPair<'a, T, M> {
); );
let d = Euclidian::squared_distance( let d = Euclidian::squared_distance(
&(self.samples.get_row_as_vec(index_row_i)), &Vec::from_iterator(
&(self.samples.get_row_as_vec(index_row_j)), self.samples.get_row(index_row_i).iterator(0).copied(),
self.samples.shape().1,
),
&Vec::from_iterator(
self.samples.get_row(index_row_j).iterator(0).copied(),
self.samples.shape().1,
),
); );
if d < nbd.unwrap() { if d < nbd.unwrap().to_f64().unwrap() {
// set this j-value to be the closest neighbour // set this j-value to be the closest neighbour
index_closest = index_row_j; index_closest = index_row_j;
nbd = Some(d); nbd = Some(T::from(d).unwrap());
} }
} }
@@ -138,7 +146,7 @@ impl<'a, T: RealNumber + FloatNumber, M: Array2<T>> FastPair<'a, T, M> {
// No more neighbors, terminate conga line. // No more neighbors, terminate conga line.
// Last person on the line has no neigbors // Last person on the line has no neigbors
distances.get_mut(&max_index).unwrap().neighbour = Some(max_index); distances.get_mut(&max_index).unwrap().neighbour = Some(max_index);
distances.get_mut(&(len - 1)).unwrap().distance = Some(T::max_value()); distances.get_mut(&(len - 1)).unwrap().distance = Some(<T as Bounded>::max_value());
// compute sparse matrix (connectivity matrix) // compute sparse matrix (connectivity matrix)
let mut sparse_matrix = M::zeros(len, len); let mut sparse_matrix = M::zeros(len, len);
@@ -171,33 +179,6 @@ impl<'a, T: RealNumber + FloatNumber, M: Array2<T>> FastPair<'a, T, M> {
} }
} }
///
/// Brute force algorithm, used only for comparison and testing
///
#[cfg(feature = "fp_bench")]
pub fn closest_pair_brute(&self) -> PairwiseDistance<T> {
use itertools::Itertools;
let m = self.samples.shape().0;
let mut closest_pair = PairwiseDistance {
node: 0,
neighbour: Option::None,
distance: Some(T::max_value()),
};
for pair in (0..m).combinations(2) {
let d = Euclidian::squared_distance(
&(self.samples.get_row_as_vec(pair[0])),
&(self.samples.get_row_as_vec(pair[1])),
);
if d < closest_pair.distance.unwrap() {
closest_pair.node = pair[0];
closest_pair.neighbour = Some(pair[1]);
closest_pair.distance = Some(d);
}
}
closest_pair
}
// //
// 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
@@ -210,10 +191,19 @@ impl<'a, T: RealNumber + FloatNumber, M: Array2<T>> FastPair<'a, T, M> {
distances.push(PairwiseDistance { distances.push(PairwiseDistance {
node: index_row, node: index_row,
neighbour: Some(*other), neighbour: Some(*other),
distance: Some(Euclidian::squared_distance( distance: Some(
&(self.samples.get_row_as_vec(index_row)), T::from(Euclidian::squared_distance(
&(self.samples.get_row_as_vec(*other)), &Vec::from_iterator(
)), self.samples.get_row(index_row).iterator(0).copied(),
self.samples.shape().1,
),
&Vec::from_iterator(
self.samples.get_row(*other).iterator(0).copied(),
self.samples.shape().1,
),
))
.unwrap(),
),
}) })
} }
} }
@@ -225,7 +215,39 @@ impl<'a, T: RealNumber + FloatNumber, M: Array2<T>> FastPair<'a, T, M> {
mod tests_fastpair { mod tests_fastpair {
use super::*; use super::*;
use crate::linalg::naive::dense_matrix::*; use crate::linalg::basic::{arrays::Array, matrix::DenseMatrix};
///
/// Brute force algorithm, used only for comparison and testing
///
pub fn closest_pair_brute(fastpair: &FastPair<f64, DenseMatrix<f64>>) -> PairwiseDistance<f64> {
use itertools::Itertools;
let m = fastpair.samples.shape().0;
let mut closest_pair = PairwiseDistance {
node: 0,
neighbour: Option::None,
distance: Some(f64::max_value()),
};
for pair in (0..m).combinations(2) {
let d = Euclidian::squared_distance(
&Vec::from_iterator(
fastpair.samples.get_row(pair[0]).iterator(0).copied(),
fastpair.samples.shape().1,
),
&Vec::from_iterator(
fastpair.samples.get_row(pair[1]).iterator(0).copied(),
fastpair.samples.shape().1,
),
);
if d < closest_pair.distance.unwrap() {
closest_pair.node = pair[0];
closest_pair.neighbour = Some(pair[1]);
closest_pair.distance = Some(d);
}
}
closest_pair
}
#[test] #[test]
fn fastpair_init() { fn fastpair_init() {
@@ -284,7 +306,7 @@ mod tests_fastpair {
}; };
assert_eq!(closest_pair, expected_closest_pair); assert_eq!(closest_pair, expected_closest_pair);
let closest_pair_brute = fastpair.closest_pair_brute(); let closest_pair_brute = closest_pair_brute(&fastpair);
assert_eq!(closest_pair_brute, expected_closest_pair); assert_eq!(closest_pair_brute, expected_closest_pair);
} }
@@ -302,7 +324,7 @@ mod tests_fastpair {
neighbour: Some(3), neighbour: Some(3),
distance: Some(4.0), distance: Some(4.0),
}; };
assert_eq!(closest_pair, fastpair.closest_pair_brute()); assert_eq!(closest_pair, closest_pair_brute(&fastpair));
assert_eq!(closest_pair, expected_closest_pair); assert_eq!(closest_pair, expected_closest_pair);
} }
@@ -459,11 +481,16 @@ mod tests_fastpair {
let expected: HashMap<_, _> = dissimilarities.into_iter().collect(); let expected: HashMap<_, _> = dissimilarities.into_iter().collect();
for i in 0..(x.shape().0 - 1) { for i in 0..(x.shape().0 - 1) {
let input_node = result.samples.get_row_as_vec(i);
let input_neighbour: usize = expected.get(&i).unwrap().neighbour.unwrap(); let input_neighbour: usize = expected.get(&i).unwrap().neighbour.unwrap();
let distance = Euclidian::squared_distance( let distance = Euclidian::squared_distance(
&input_node, &Vec::from_iterator(
&result.samples.get_row_as_vec(input_neighbour), result.samples.get_row(i).iterator(0).copied(),
result.samples.shape().1,
),
&Vec::from_iterator(
result.samples.get_row(input_neighbour).iterator(0).copied(),
result.samples.shape().1,
),
); );
assert_eq!(i, expected.get(&i).unwrap().node); assert_eq!(i, expected.get(&i).unwrap().node);
@@ -518,7 +545,7 @@ mod tests_fastpair {
let result = fastpair.unwrap(); let result = fastpair.unwrap();
let dissimilarity1 = result.closest_pair(); let dissimilarity1 = result.closest_pair();
let dissimilarity2 = result.closest_pair_brute(); let dissimilarity2 = closest_pair_brute(&result);
assert_eq!(dissimilarity1, dissimilarity2); assert_eq!(dissimilarity1, dissimilarity2);
} }
+8 -2
View File
@@ -143,7 +143,10 @@ mod tests {
} }
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn knn_find() { fn knn_find() {
let data1 = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; let data1 = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
@@ -190,7 +193,10 @@ mod tests {
assert_eq!(vec!(1, 2, 3), found_idxs2); assert_eq!(vec!(1, 2, 3), found_idxs2);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn knn_point_eq() { fn knn_point_eq() {
let point1 = KNNPoint { let point1 = KNNPoint {
+1 -3
View File
@@ -41,10 +41,8 @@ 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;
/// dissimilarities for vector-vector distance. Linkage algorithms used in fastpair
pub mod distances;
/// fastpair closest neighbour algorithm /// fastpair 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;
+20 -5
View File
@@ -95,14 +95,20 @@ impl<T: PartialOrd + Debug> HeapSelection<T> {
mod tests { mod tests {
use super::*; use super::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn with_capacity() { fn with_capacity() {
let heap = HeapSelection::<i32>::with_capacity(3); let heap = HeapSelection::<i32>::with_capacity(3);
assert_eq!(3, heap.k); assert_eq!(3, heap.k);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn test_add() { fn test_add() {
let mut heap = HeapSelection::with_capacity(3); let mut heap = HeapSelection::with_capacity(3);
@@ -120,7 +126,10 @@ mod tests {
assert_eq!(vec![2, 0, -5], heap.get()); assert_eq!(vec![2, 0, -5], heap.get());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn test_add1() { fn test_add1() {
let mut heap = HeapSelection::with_capacity(3); let mut heap = HeapSelection::with_capacity(3);
@@ -135,7 +144,10 @@ mod tests {
assert_eq!(vec![0f64, -1f64, -5f64], heap.get()); assert_eq!(vec![0f64, -1f64, -5f64], heap.get());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn test_add2() { fn test_add2() {
let mut heap = HeapSelection::with_capacity(3); let mut heap = HeapSelection::with_capacity(3);
@@ -148,7 +160,10 @@ mod tests {
assert_eq!(vec![5.6568, 2.8284, 0.0], heap.get()); assert_eq!(vec![5.6568, 2.8284, 0.0], heap.get());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn test_add_ordered() { fn test_add_ordered() {
let mut heap = HeapSelection::with_capacity(3); let mut heap = HeapSelection::with_capacity(3);
+4 -1
View File
@@ -113,7 +113,10 @@ impl<T: Num + PartialOrd + Copy> QuickArgSort for Vec<T> {
mod tests { mod tests {
use super::*; use super::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn with_capacity() { fn with_capacity() {
let arr1 = vec![0.3, 0.1, 0.2, 0.4, 0.9, 0.5, 0.7, 0.6, 0.8]; let arr1 = vec![0.3, 0.1, 0.2, 0.4, 0.9, 0.5, 0.7, 0.6, 0.8];
+11 -3
View File
@@ -425,7 +425,10 @@ mod tests {
assert!(iter.next().is_none()); assert!(iter.next().is_none());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn fit_predict_dbscan() { fn fit_predict_dbscan() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
@@ -457,7 +460,10 @@ mod tests {
assert_eq!(expected_labels, predicted_labels); assert_eq!(expected_labels, predicted_labels);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
fn serde() { fn serde() {
@@ -491,10 +497,12 @@ mod tests {
assert_eq!(dbscan, deserialized_dbscan); assert_eq!(dbscan, deserialized_dbscan);
} }
use crate::dataset::generator;
#[cfg(feature = "datasets")]
#[test] #[test]
fn from_vec() { fn from_vec() {
use crate::dataset::generator;
// Generate three blobs // Generate three blobs
let blobs = generator::make_blobs(100, 2, 3); let blobs = generator::make_blobs(100, 2, 3);
let x: DenseMatrix<f32> = DenseMatrix::from_iterator(blobs.data.into_iter(), 100, 2, 0); let x: DenseMatrix<f32> = DenseMatrix::from_iterator(blobs.data.into_iter(), 100, 2, 0);
+12 -3
View File
@@ -418,7 +418,10 @@ mod tests {
use super::*; use super::*;
use crate::linalg::basic::matrix::DenseMatrix; use crate::linalg::basic::matrix::DenseMatrix;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn invalid_k() { fn invalid_k() {
let x = DenseMatrix::from_2d_array(&[&[1, 2, 3], &[4, 5, 6]]); let x = DenseMatrix::from_2d_array(&[&[1, 2, 3], &[4, 5, 6]]);
@@ -462,7 +465,10 @@ mod tests {
assert!(iter.next().is_none()); assert!(iter.next().is_none());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn fit_predict_iris() { fn fit_predict_iris() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
@@ -497,7 +503,10 @@ mod tests {
} }
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
fn serde() { fn serde() {
+4 -1
View File
@@ -69,7 +69,10 @@ mod tests {
assert!(serialize_data(&dataset, "boston.xy").is_ok()); assert!(serialize_data(&dataset, "boston.xy").is_ok());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn boston_dataset() { fn boston_dataset() {
let dataset = load_dataset(); let dataset = load_dataset();
+4 -1
View File
@@ -83,7 +83,10 @@ mod tests {
// assert!(serialize_data(&dataset, "breast_cancer.xy").is_ok()); // assert!(serialize_data(&dataset, "breast_cancer.xy").is_ok());
// } // }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn cancer_dataset() { fn cancer_dataset() {
let dataset = load_dataset(); let dataset = load_dataset();
+4 -1
View File
@@ -67,7 +67,10 @@ mod tests {
// assert!(serialize_data(&dataset, "diabetes.xy").is_ok()); // assert!(serialize_data(&dataset, "diabetes.xy").is_ok());
// } // }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn boston_dataset() { fn boston_dataset() {
let dataset = load_dataset(); let dataset = load_dataset();
+4 -1
View File
@@ -57,7 +57,10 @@ mod tests {
let dataset = load_dataset(); let dataset = load_dataset();
assert!(serialize_data(&dataset, "digits.xy").is_ok()); assert!(serialize_data(&dataset, "digits.xy").is_ok());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn digits_dataset() { fn digits_dataset() {
let dataset = load_dataset(); let dataset = load_dataset();
+12 -3
View File
@@ -137,7 +137,10 @@ mod tests {
use super::*; use super::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn test_make_blobs() { fn test_make_blobs() {
let dataset = make_blobs(10, 2, 3); let dataset = make_blobs(10, 2, 3);
@@ -150,7 +153,10 @@ mod tests {
assert_eq!(dataset.num_samples, 10); assert_eq!(dataset.num_samples, 10);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn test_make_circles() { fn test_make_circles() {
let dataset = make_circles(10, 0.5, 0.05); let dataset = make_circles(10, 0.5, 0.05);
@@ -163,7 +169,10 @@ mod tests {
assert_eq!(dataset.num_samples, 10); assert_eq!(dataset.num_samples, 10);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn test_make_moons() { fn test_make_moons() {
let dataset = make_moons(10, 0.05); let dataset = make_moons(10, 0.05);
+4 -1
View File
@@ -70,7 +70,10 @@ mod tests {
// assert!(serialize_data(&dataset, "iris.xy").is_ok()); // assert!(serialize_data(&dataset, "iris.xy").is_ok());
// } // }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn iris_dataset() { fn iris_dataset() {
let dataset = load_dataset(); let dataset = load_dataset();
+4 -1
View File
@@ -121,7 +121,10 @@ pub(crate) fn deserialize_data(
mod tests { mod tests {
use super::*; use super::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn as_matrix() { fn as_matrix() {
let dataset = Dataset { let dataset = Dataset {
+13 -4
View File
@@ -446,7 +446,10 @@ mod tests {
&[6.8, 161.0, 60.0, 15.6], &[6.8, 161.0, 60.0, 15.6],
]) ])
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn pca_components() { fn pca_components() {
let us_arrests = us_arrests_data(); let us_arrests = us_arrests_data();
@@ -466,7 +469,10 @@ mod tests {
epsilon = 1e-3 epsilon = 1e-3
)); ));
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn decompose_covariance() { fn decompose_covariance() {
let us_arrests = us_arrests_data(); let us_arrests = us_arrests_data();
@@ -579,7 +585,10 @@ mod tests {
)); ));
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn decompose_correlation() { fn decompose_correlation() {
let us_arrests = us_arrests_data(); let us_arrests = us_arrests_data();
@@ -700,7 +709,7 @@ mod tests {
// Disable this test for now // Disable this test for now
// TODO: implement deserialization for new DenseMatrix // TODO: implement deserialization for new DenseMatrix
// #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] // #[cfg_attr(all(target_arch = "wasm32", not(target_os = "wasi")), wasm_bindgen_test::wasm_bindgen_test)]
// #[test] // #[test]
// #[cfg(feature = "serde")] // #[cfg(feature = "serde")]
// fn pca_serde() { // fn pca_serde() {
+5 -2
View File
@@ -237,7 +237,10 @@ mod tests {
assert!(iter.next().is_none()); assert!(iter.next().is_none());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn svd_decompose() { fn svd_decompose() {
// https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/USArrests.html // https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/USArrests.html
@@ -316,7 +319,7 @@ mod tests {
// Disable this test for now // Disable this test for now
// TODO: implement deserialization for new DenseMatrix // TODO: implement deserialization for new DenseMatrix
// #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] // #[cfg_attr(all(target_arch = "wasm32", not(target_os = "wasi")), wasm_bindgen_test::wasm_bindgen_test)]
// #[test] // #[test]
// #[cfg(feature = "serde")] // #[cfg(feature = "serde")]
// fn serde() { // fn serde() {
+12 -3
View File
@@ -664,7 +664,10 @@ mod tests {
assert!(iter.next().is_none()); assert!(iter.next().is_none());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn fit_predict_iris() { fn fit_predict_iris() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
@@ -710,7 +713,10 @@ mod tests {
assert!(accuracy(&y, &classifier.predict(&x).unwrap()) >= 0.95); assert!(accuracy(&y, &classifier.predict(&x).unwrap()) >= 0.95);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn fit_predict_iris_oob() { fn fit_predict_iris_oob() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
@@ -759,7 +765,10 @@ mod tests {
); );
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
fn serde() { fn serde() {
+12 -3
View File
@@ -550,7 +550,10 @@ mod tests {
assert!(iter.next().is_none()); assert!(iter.next().is_none());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn fit_longley() { fn fit_longley() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
@@ -595,7 +598,10 @@ mod tests {
assert!(mean_absolute_error(&y, &y_hat) < 1.0); assert!(mean_absolute_error(&y, &y_hat) < 1.0);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn fit_predict_longley_oob() { fn fit_predict_longley_oob() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
@@ -645,7 +651,10 @@ mod tests {
assert!(mean_absolute_error(&y, &y_hat) < mean_absolute_error(&y, &y_hat_oob)); assert!(mean_absolute_error(&y, &y_hat) < mean_absolute_error(&y, &y_hat_oob));
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
fn serde() { fn serde() {
+23 -16
View File
@@ -10,34 +10,30 @@
//! # SmartCore //! # SmartCore
//! //!
//! Welcome to SmartCore, the most advanced machine learning library in Rust! //! Welcome to SmartCore, machine learning in Rust!
//! //!
//! SmartCore features various classification, regression and clustering algorithms including support vector machines, random forests, k-means and DBSCAN, //! SmartCore features various classification, regression and clustering algorithms including support vector machines, random forests, k-means and DBSCAN,
//! as well as tools for model selection and model evaluation. //! as well as tools for model selection and model evaluation.
//! //!
//! SmartCore is well integrated with a with wide variaty of libraries that provide support for large, multi-dimensional arrays and matrices. At this moment, //! SmartCore provides its own traits system that extends Rust standard library, to deal with linear algebra and common
//! all Smartcore's algorithms work with ordinary Rust vectors, as well as matrices and vectors defined in these packages: //! computational models. Its API is designed using well recognizable patterns. Extra features (like support for [ndarray](https://docs.rs/ndarray)
//! * [ndarray](https://docs.rs/ndarray) //! structures) is available via optional features.
//! //!
//! ## Getting Started //! ## Getting Started
//! //!
//! To start using SmartCore simply add the following to your Cargo.toml file: //! To start using SmartCore simply add the following to your Cargo.toml file:
//! ```ignore //! ```ignore
//! [dependencies] //! [dependencies]
//! smartcore = { git = "https://github.com/smartcorelib/smartcore", branch = "v0.5-wip" } //! smartcore = { git = "https://github.com/smartcorelib/smartcore", branch = "development" }
//! ``` //! ```
//! //!
//! All machine learning algorithms in SmartCore are grouped into these broad categories: //! ## Using Jupyter
//! * [Clustering](cluster/index.html), unsupervised clustering of unlabeled data. //! For quick introduction, Jupyter Notebooks are available [here](https://github.com/smartcorelib/smartcore-jupyter/tree/main/notebooks).
//! * [Matrix Decomposition](decomposition/index.html), various methods for matrix decomposition. //! You can set up a local environment to run Rust notebooks using [EVCXR](https://github.com/google/evcxr)
//! * [Linear Models](linear/index.html), regression and classification methods where output is assumed to have linear relation to explanatory variables //! following [these instructions](https://depth-first.com/articles/2020/09/21/interactive-rust-in-a-repl-and-jupyter-notebook-with-evcxr/).
//! * [Ensemble Models](ensemble/index.html), variety of regression and classification ensemble models
//! * [Tree-based Models](tree/index.html), classification and regression trees
//! * [Nearest Neighbors](neighbors/index.html), K Nearest Neighbors for classification and regression
//! * [Naive Bayes](naive_bayes/index.html), statistical classification technique based on Bayes Theorem
//! * [SVM](svm/index.html), support vector machines
//! //!
//! //!
//! ## First Example
//! For example, you can use this code to fit a [K Nearest Neighbors classifier](neighbors/knn_classifier/index.html) to a dataset that is defined as standard Rust vector: //! For example, you can use this code to fit a [K Nearest Neighbors classifier](neighbors/knn_classifier/index.html) to a dataset that is defined as standard Rust vector:
//! //!
//! ``` //! ```
@@ -48,14 +44,14 @@
//! // Various distance metrics //! // Various distance metrics
//! use smartcore::metrics::distance::*; //! use smartcore::metrics::distance::*;
//! //!
//! // Turn Rust vectors with samples into a matrix //! // Turn Rust vector-slices with samples into a matrix
//! let x = DenseMatrix::from_2d_array(&[ //! let x = DenseMatrix::from_2d_array(&[
//! &[1., 2.], //! &[1., 2.],
//! &[3., 4.], //! &[3., 4.],
//! &[5., 6.], //! &[5., 6.],
//! &[7., 8.], //! &[7., 8.],
//! &[9., 10.]]); //! &[9., 10.]]);
//! // Our classes are defined as a Vector //! // Our classes are defined as a vector
//! let y = vec![2, 2, 2, 3, 3]; //! let y = vec![2, 2, 2, 3, 3];
//! //!
//! // Train classifier //! // Train classifier
@@ -64,6 +60,17 @@
//! // Predict classes //! // Predict classes
//! let y_hat = knn.predict(&x).unwrap(); //! let y_hat = knn.predict(&x).unwrap();
//! ``` //! ```
//!
//! ## Overview
//! All machine learning algorithms in SmartCore are grouped into these broad categories:
//! * [Clustering](cluster/index.html), unsupervised clustering of unlabeled data.
//! * [Matrix Decomposition](decomposition/index.html), various methods for matrix decomposition.
//! * [Linear Models](linear/index.html), regression and classification methods where output is assumed to have linear relation to explanatory variables
//! * [Ensemble Models](ensemble/index.html), variety of regression and classification ensemble models
//! * [Tree-based Models](tree/index.html), classification and regression trees
//! * [Nearest Neighbors](neighbors/index.html), K Nearest Neighbors for classification and regression
//! * [Naive Bayes](naive_bayes/index.html), statistical classification technique based on Bayes Theorem
//! * [SVM](svm/index.html), support vector machines
/// Foundamental numbers traits /// Foundamental numbers traits
pub mod numbers; pub mod numbers;
+3 -1
View File
@@ -4,6 +4,7 @@ use std::ops::Range;
use std::slice::Iter; use std::slice::Iter;
use approx::{AbsDiffEq, RelativeEq}; use approx::{AbsDiffEq, RelativeEq};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::linalg::basic::arrays::{ use crate::linalg::basic::arrays::{
@@ -19,7 +20,8 @@ use crate::numbers::basenum::Number;
use crate::numbers::realnum::RealNumber; use crate::numbers::realnum::RealNumber;
/// Dense matrix /// Dense matrix
#[derive(Debug, Clone, Serialize, Deserialize)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct DenseMatrix<T> { pub struct DenseMatrix<T> {
ncols: usize, ncols: usize,
nrows: usize, nrows: usize,
+8 -2
View File
@@ -169,7 +169,10 @@ mod tests {
use super::*; use super::*;
use crate::linalg::basic::matrix::DenseMatrix; use crate::linalg::basic::matrix::DenseMatrix;
use approx::relative_eq; use approx::relative_eq;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn cholesky_decompose() { fn cholesky_decompose() {
let a = DenseMatrix::from_2d_array(&[&[25., 15., -5.], &[15., 18., 0.], &[-5., 0., 11.]]); let a = DenseMatrix::from_2d_array(&[&[25., 15., -5.], &[15., 18., 0.], &[-5., 0., 11.]]);
@@ -188,7 +191,10 @@ mod tests {
)); ));
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn cholesky_solve_mut() { fn cholesky_solve_mut() {
let a = DenseMatrix::from_2d_array(&[&[25., 15., -5.], &[15., 18., 0.], &[-5., 0., 11.]]); let a = DenseMatrix::from_2d_array(&[&[25., 15., -5.], &[15., 18., 0.], &[-5., 0., 11.]]);
+12 -3
View File
@@ -810,7 +810,10 @@ mod tests {
use crate::linalg::basic::matrix::DenseMatrix; use crate::linalg::basic::matrix::DenseMatrix;
use approx::relative_eq; use approx::relative_eq;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn decompose_symmetric() { fn decompose_symmetric() {
let A = DenseMatrix::from_2d_array(&[ let A = DenseMatrix::from_2d_array(&[
@@ -841,7 +844,10 @@ mod tests {
assert!((0f64 - evd.e[i]).abs() < std::f64::EPSILON); assert!((0f64 - evd.e[i]).abs() < std::f64::EPSILON);
} }
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn decompose_asymmetric() { fn decompose_asymmetric() {
let A = DenseMatrix::from_2d_array(&[ let A = DenseMatrix::from_2d_array(&[
@@ -872,7 +878,10 @@ mod tests {
assert!((0f64 - evd.e[i]).abs() < std::f64::EPSILON); assert!((0f64 - evd.e[i]).abs() < std::f64::EPSILON);
} }
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn decompose_complex() { fn decompose_complex() {
let A = DenseMatrix::from_2d_array(&[ let A = DenseMatrix::from_2d_array(&[
+8 -2
View File
@@ -260,7 +260,10 @@ mod tests {
use crate::linalg::basic::matrix::DenseMatrix; use crate::linalg::basic::matrix::DenseMatrix;
use approx::relative_eq; use approx::relative_eq;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn decompose() { fn decompose() {
let a = DenseMatrix::from_2d_array(&[&[1., 2., 3.], &[0., 1., 5.], &[5., 6., 0.]]); let a = DenseMatrix::from_2d_array(&[&[1., 2., 3.], &[0., 1., 5.], &[5., 6., 0.]]);
@@ -275,7 +278,10 @@ mod tests {
assert!(relative_eq!(lu.U(), expected_U, epsilon = 1e-4)); assert!(relative_eq!(lu.U(), expected_U, epsilon = 1e-4));
assert!(relative_eq!(lu.pivot(), expected_pivot, epsilon = 1e-4)); assert!(relative_eq!(lu.pivot(), expected_pivot, epsilon = 1e-4));
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn inverse() { fn inverse() {
let a = DenseMatrix::from_2d_array(&[&[1., 2., 3.], &[0., 1., 5.], &[5., 6., 0.]]); let a = DenseMatrix::from_2d_array(&[&[1., 2., 3.], &[0., 1., 5.], &[5., 6., 0.]]);
+8 -2
View File
@@ -198,7 +198,10 @@ mod tests {
use super::*; use super::*;
use crate::linalg::basic::matrix::DenseMatrix; use crate::linalg::basic::matrix::DenseMatrix;
use approx::relative_eq; use approx::relative_eq;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn decompose() { fn decompose() {
let a = DenseMatrix::from_2d_array(&[&[0.9, 0.4, 0.7], &[0.4, 0.5, 0.3], &[0.7, 0.3, 0.8]]); let a = DenseMatrix::from_2d_array(&[&[0.9, 0.4, 0.7], &[0.4, 0.5, 0.3], &[0.7, 0.3, 0.8]]);
@@ -217,7 +220,10 @@ mod tests {
assert!(relative_eq!(qr.R().abs(), r.abs(), epsilon = 1e-4)); assert!(relative_eq!(qr.R().abs(), r.abs(), epsilon = 1e-4));
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn qr_solve_mut() { fn qr_solve_mut() {
let a = DenseMatrix::from_2d_array(&[&[0.9, 0.4, 0.7], &[0.4, 0.5, 0.3], &[0.7, 0.3, 0.8]]); let a = DenseMatrix::from_2d_array(&[&[0.9, 0.4, 0.7], &[0.4, 0.5, 0.3], &[0.7, 0.3, 0.8]]);
+3 -3
View File
@@ -71,8 +71,8 @@ pub trait MatrixStats<T: RealNumber>: ArrayView2<T> + Array2<T> {
x x
} }
/// (reference)[http://en.wikipedia.org/wiki/Arithmetic_mean] /// <http://en.wikipedia.org/wiki/Arithmetic_mean>
/// Taken from statistical /// Taken from `statistical`
/// The MIT License (MIT) /// The MIT License (MIT)
/// Copyright (c) 2015 Jeff Belgum /// Copyright (c) 2015 Jeff Belgum
fn _mean_of_vector(v: &[T]) -> T { fn _mean_of_vector(v: &[T]) -> T {
@@ -97,7 +97,7 @@ pub trait MatrixStats<T: RealNumber>: ArrayView2<T> + Array2<T> {
sum sum
} }
/// (Sample variance)[http://en.wikipedia.org/wiki/Variance#Sample_variance] /// <http://en.wikipedia.org/wiki/Variance#Sample_variance>
/// Taken from statistical /// Taken from statistical
/// The MIT License (MIT) /// The MIT License (MIT)
/// Copyright (c) 2015 Jeff Belgum /// Copyright (c) 2015 Jeff Belgum
+16 -4
View File
@@ -479,7 +479,10 @@ mod tests {
use crate::linalg::basic::matrix::DenseMatrix; use crate::linalg::basic::matrix::DenseMatrix;
use approx::relative_eq; use approx::relative_eq;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn decompose_symmetric() { fn decompose_symmetric() {
let A = DenseMatrix::from_2d_array(&[ let A = DenseMatrix::from_2d_array(&[
@@ -510,7 +513,10 @@ mod tests {
assert!((s[i] - svd.s[i]).abs() < 1e-4); assert!((s[i] - svd.s[i]).abs() < 1e-4);
} }
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn decompose_asymmetric() { fn decompose_asymmetric() {
let A = DenseMatrix::from_2d_array(&[ let A = DenseMatrix::from_2d_array(&[
@@ -711,7 +717,10 @@ mod tests {
assert!((s[i] - svd.s[i]).abs() < 1e-4); assert!((s[i] - svd.s[i]).abs() < 1e-4);
} }
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn solve() { fn solve() {
let a = DenseMatrix::from_2d_array(&[&[0.9, 0.4, 0.7], &[0.4, 0.5, 0.3], &[0.7, 0.3, 0.8]]); let a = DenseMatrix::from_2d_array(&[&[0.9, 0.4, 0.7], &[0.4, 0.5, 0.3], &[0.7, 0.3, 0.8]]);
@@ -722,7 +731,10 @@ mod tests {
assert!(relative_eq!(w, expected_w, epsilon = 1e-2)); assert!(relative_eq!(w, expected_w, epsilon = 1e-2));
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn decompose_restore() { fn decompose_restore() {
let a = DenseMatrix::from_2d_array(&[&[1.0, 2.0, 3.0, 4.0], &[5.0, 6.0, 7.0, 8.0]]); let a = DenseMatrix::from_2d_array(&[&[1.0, 2.0, 3.0, 4.0], &[5.0, 6.0, 7.0, 8.0]]);
+9 -3
View File
@@ -491,7 +491,10 @@ mod tests {
assert!(iter.next().is_none()); assert!(iter.next().is_none());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn elasticnet_longley() { fn elasticnet_longley() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
@@ -535,7 +538,10 @@ mod tests {
assert!(mean_absolute_error(&y_hat, &y) < 30.0); assert!(mean_absolute_error(&y_hat, &y) < 30.0);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn elasticnet_fit_predict1() { fn elasticnet_fit_predict1() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
@@ -603,7 +609,7 @@ mod tests {
} }
// TODO: serialization for the new DenseMatrix needs to be implemented // TODO: serialization for the new DenseMatrix needs to be implemented
// #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] // #[cfg_attr(all(target_arch = "wasm32", not(target_os = "wasi")), wasm_bindgen_test::wasm_bindgen_test)]
// #[test] // #[test]
// #[cfg(feature = "serde")] // #[cfg(feature = "serde")]
// fn serde() { // fn serde() {
+5 -2
View File
@@ -398,7 +398,10 @@ mod tests {
assert!(iter.next().is_none()); assert!(iter.next().is_none());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn lasso_fit_predict() { fn lasso_fit_predict() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
@@ -448,7 +451,7 @@ mod tests {
} }
// TODO: serialization for the new DenseMatrix needs to be implemented // TODO: serialization for the new DenseMatrix needs to be implemented
// #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] // #[cfg_attr(all(target_arch = "wasm32", not(target_os = "wasi")), wasm_bindgen_test::wasm_bindgen_test)]
// #[test] // #[test]
// #[cfg(feature = "serde")] // #[cfg(feature = "serde")]
// fn serde() { // fn serde() {
+5 -2
View File
@@ -325,7 +325,10 @@ mod tests {
assert!(iter.next().is_none()); assert!(iter.next().is_none());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn ols_fit_predict() { fn ols_fit_predict() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
@@ -372,7 +375,7 @@ mod tests {
} }
// TODO: serialization for the new DenseMatrix needs to be implemented // TODO: serialization for the new DenseMatrix needs to be implemented
// #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] // #[cfg_attr(all(target_arch = "wasm32", not(target_os = "wasi")), wasm_bindgen_test::wasm_bindgen_test)]
// #[test] // #[test]
// #[cfg(feature = "serde")] // #[cfg(feature = "serde")]
// fn serde() { // fn serde() {
+29 -7
View File
@@ -577,6 +577,8 @@ impl<TX: Number + FloatNumber + RealNumber, TY: Number + Ord, X: Array2<TX>, Y:
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
#[cfg(feature = "datasets")]
use crate::dataset::generator::make_blobs; use crate::dataset::generator::make_blobs;
use crate::linalg::basic::arrays::Array; use crate::linalg::basic::arrays::Array;
use crate::linalg::basic::matrix::DenseMatrix; use crate::linalg::basic::matrix::DenseMatrix;
@@ -596,7 +598,10 @@ mod tests {
assert!(iter.next().is_none()); assert!(iter.next().is_none());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn multiclass_objective_f() { fn multiclass_objective_f() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
@@ -653,7 +658,10 @@ mod tests {
assert!((g[0].abs() - 32.0).abs() < 1e-4); assert!((g[0].abs() - 32.0).abs() < 1e-4);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn binary_objective_f() { fn binary_objective_f() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
@@ -712,7 +720,10 @@ mod tests {
assert!((g[2] - 3.8693).abs() < 1e-4); assert!((g[2] - 3.8693).abs() < 1e-4);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn lr_fit_predict() { fn lr_fit_predict() {
let x: DenseMatrix<f64> = DenseMatrix::from_2d_array(&[ let x: DenseMatrix<f64> = DenseMatrix::from_2d_array(&[
@@ -751,7 +762,11 @@ mod tests {
assert_eq!(y_hat, vec![0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]); assert_eq!(y_hat, vec![0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg(feature = "datasets")]
#[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn lr_fit_predict_multiclass() { fn lr_fit_predict_multiclass() {
let blobs = make_blobs(15, 4, 3); let blobs = make_blobs(15, 4, 3);
@@ -778,7 +793,11 @@ mod tests {
assert!(reg_coeff_sum < coeff); assert!(reg_coeff_sum < coeff);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg(feature = "datasets")]
#[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn lr_fit_predict_binary() { fn lr_fit_predict_binary() {
let blobs = make_blobs(20, 4, 2); let blobs = make_blobs(20, 4, 2);
@@ -809,7 +828,7 @@ mod tests {
} }
// TODO: serialization for the new DenseMatrix needs to be implemented // TODO: serialization for the new DenseMatrix needs to be implemented
// #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] // #[cfg_attr(all(target_arch = "wasm32", not(target_os = "wasi")), wasm_bindgen_test::wasm_bindgen_test)]
// #[test] // #[test]
// #[cfg(feature = "serde")] // #[cfg(feature = "serde")]
// fn serde() { // fn serde() {
@@ -840,7 +859,10 @@ mod tests {
// assert_eq!(lr, deserialized_lr); // assert_eq!(lr, deserialized_lr);
// } // }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn lr_fit_predict_iris() { fn lr_fit_predict_iris() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
+5 -2
View File
@@ -443,7 +443,10 @@ mod tests {
assert!(iter.next().is_none()); assert!(iter.next().is_none());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn ridge_fit_predict() { fn ridge_fit_predict() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
@@ -500,7 +503,7 @@ mod tests {
} }
// TODO: implement serialization for new DenseMatrix // TODO: implement serialization for new DenseMatrix
// #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] // #[cfg_attr(all(target_arch = "wasm32", not(target_os = "wasi")), wasm_bindgen_test::wasm_bindgen_test)]
// #[test] // #[test]
// #[cfg(feature = "serde")] // #[cfg(feature = "serde")]
// fn serde() { // fn serde() {
+8 -2
View File
@@ -83,7 +83,10 @@ impl<T: Number> Metrics<T> for Accuracy<T> {
mod tests { mod tests {
use super::*; use super::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn accuracy_float() { fn accuracy_float() {
let y_pred: Vec<f64> = vec![0., 2., 1., 3.]; let y_pred: Vec<f64> = vec![0., 2., 1., 3.];
@@ -96,7 +99,10 @@ mod tests {
assert!((score2 - 1.0).abs() < 1e-8); assert!((score2 - 1.0).abs() < 1e-8);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn accuracy_int() { fn accuracy_int() {
let y_pred: Vec<i32> = vec![0, 2, 1, 3]; let y_pred: Vec<i32> = vec![0, 2, 1, 3];
+14 -14
View File
@@ -26,8 +26,8 @@ use std::marker::PhantomData;
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::linalg::basic::arrays::{Array1, ArrayView1, MutArrayView1}; use crate::linalg::basic::arrays::{Array1, ArrayView1};
use crate::numbers::basenum::Number; use crate::numbers::floatnum::FloatNumber;
use crate::metrics::Metrics; use crate::metrics::Metrics;
@@ -38,14 +38,14 @@ pub struct AUC<T> {
_phantom: PhantomData<T>, _phantom: PhantomData<T>,
} }
impl<T: Number + Ord> Metrics<T> for AUC<T> { impl<T: FloatNumber + PartialOrd> Metrics<T> for AUC<T> {
/// create a typed object to call AUC functions /// create a typed object to call AUC functions
fn new() -> Self { fn new() -> Self {
Self { Self {
_phantom: PhantomData, _phantom: PhantomData,
} }
} }
fn new_with(_parameter: T) -> Self { fn new_with(_parameter: f64) -> Self {
Self { Self {
_phantom: PhantomData, _phantom: PhantomData,
} }
@@ -53,11 +53,7 @@ impl<T: Number + Ord> Metrics<T> for AUC<T> {
/// AUC score. /// AUC score.
/// * `y_true` - ground truth (correct) labels. /// * `y_true` - ground truth (correct) labels.
/// * `y_pred_prob` - probability estimates, as returned by a classifier. /// * `y_pred_prob` - probability estimates, as returned by a classifier.
fn get_score( fn get_score(&self, y_true: &dyn ArrayView1<T>, y_pred_prob: &dyn ArrayView1<T>) -> f64 {
&self,
y_true: &dyn ArrayView1<T>,
y_pred_prob: &dyn ArrayView1<T>,
) -> f64 {
let mut pos = T::zero(); let mut pos = T::zero();
let mut neg = T::zero(); let mut neg = T::zero();
@@ -76,9 +72,10 @@ impl<T: Number + Ord> Metrics<T> for AUC<T> {
} }
} }
let y_pred = y_pred_prob.clone(); let y_pred: Vec<T> =
Array1::<T>::from_iterator(y_pred_prob.iterator(0).copied(), y_pred_prob.shape());
let label_idx = y_pred.argsort(); // TODO: try to use `crate::algorithm::sort::quick_sort` here
let label_idx: Vec<usize> = y_pred.argsort();
let mut rank = vec![0f64; n]; let mut rank = vec![0f64; n];
let mut i = 0; let mut i = 0;
@@ -108,7 +105,7 @@ impl<T: Number + Ord> Metrics<T> for AUC<T> {
let pos = pos.to_f64().unwrap(); let pos = pos.to_f64().unwrap();
let neg = neg.to_f64().unwrap(); let neg = neg.to_f64().unwrap();
T::from(auc - (pos * (pos + 1f64) / 2.0)).unwrap() / T::from(pos * neg).unwrap() (auc - (pos * (pos + 1f64) / 2f64)) / (pos * neg)
} }
} }
@@ -116,7 +113,10 @@ impl<T: Number + Ord> Metrics<T> for AUC<T> {
mod tests { mod tests {
use super::*; use super::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn auc() { fn auc() {
let y_true: Vec<f64> = vec![0., 0., 1., 1.]; let y_true: Vec<f64> = vec![0., 0., 1., 1.];
+4 -1
View File
@@ -87,7 +87,10 @@ impl<T: Number + Ord> Metrics<T> for HCVScore<T> {
mod tests { mod tests {
use super::*; use super::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn homogeneity_score() { fn homogeneity_score() {
let v1 = vec![0, 0, 1, 1, 2, 0, 4]; let v1 = vec![0, 0, 1, 1, 2, 0, 4];
+12 -3
View File
@@ -102,7 +102,10 @@ pub fn mutual_info_score(contingency: &[Vec<usize>]) -> f64 {
mod tests { mod tests {
use super::*; use super::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn contingency_matrix_test() { fn contingency_matrix_test() {
let v1 = vec![0, 0, 1, 1, 2, 0, 4]; let v1 = vec![0, 0, 1, 1, 2, 0, 4];
@@ -114,7 +117,10 @@ mod tests {
); );
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn entropy_test() { fn entropy_test() {
let v1 = vec![0, 0, 1, 1, 2, 0, 4]; let v1 = vec![0, 0, 1, 1, 2, 0, 4];
@@ -122,7 +128,10 @@ mod tests {
assert!((1.2770 - entropy(&v1).unwrap() as f64).abs() < 1e-4); assert!((1.2770 - entropy(&v1).unwrap() as f64).abs() < 1e-4);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn mutual_info_score_test() { fn mutual_info_score_test() {
let v1 = vec![0, 0, 1, 1, 2, 0, 4]; let v1 = vec![0, 0, 1, 1, 2, 0, 4];
+4 -1
View File
@@ -76,7 +76,10 @@ impl<T: Number, A: ArrayView1<T>> Distance<A> for Euclidian<T> {
mod tests { mod tests {
use super::*; use super::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn squared_distance() { fn squared_distance() {
let a = vec![1, 2, 3]; let a = vec![1, 2, 3];
+4 -1
View File
@@ -70,7 +70,10 @@ impl<T: Number, A: ArrayView1<T>> Distance<A> for Hamming<T> {
mod tests { mod tests {
use super::*; use super::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn hamming_distance() { fn hamming_distance() {
let a = vec![1, 0, 0, 1, 0, 0, 1]; let a = vec![1, 0, 0, 1, 0, 0, 1];
+4 -1
View File
@@ -139,7 +139,10 @@ mod tests {
use crate::linalg::basic::arrays::ArrayView2; use crate::linalg::basic::arrays::ArrayView2;
use crate::linalg::basic::matrix::DenseMatrix; use crate::linalg::basic::matrix::DenseMatrix;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn mahalanobis_distance() { fn mahalanobis_distance() {
let data = DenseMatrix::from_2d_array(&[ let data = DenseMatrix::from_2d_array(&[
+4 -1
View File
@@ -66,7 +66,10 @@ impl<T: Number, A: ArrayView1<T>> Distance<A> for Manhattan<T> {
mod tests { mod tests {
use super::*; use super::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn manhattan_distance() { fn manhattan_distance() {
let a = vec![1., 2., 3.]; let a = vec![1., 2., 3.];
+4 -1
View File
@@ -71,7 +71,10 @@ impl<T: Number, A: ArrayView1<T>> Distance<A> for Minkowski<T> {
mod tests { mod tests {
use super::*; use super::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn minkowski_distance() { fn minkowski_distance() {
let a = vec![1., 2., 3.]; let a = vec![1., 2., 3.];
+48
View File
@@ -24,9 +24,15 @@ pub mod manhattan;
/// A generalization of both the Euclidean distance and the Manhattan distance. /// A generalization of both the Euclidean distance and the Manhattan distance.
pub mod minkowski; pub mod minkowski;
use std::cmp::{Eq, Ordering, PartialOrd};
use crate::linalg::basic::arrays::Array2; use crate::linalg::basic::arrays::Array2;
use crate::linalg::traits::lu::LUDecomposable; use crate::linalg::traits::lu::LUDecomposable;
use crate::numbers::basenum::Number; use crate::numbers::basenum::Number;
use crate::numbers::realnum::RealNumber;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
/// Distance metric, a function that calculates distance between two points /// Distance metric, a function that calculates distance between two points
pub trait Distance<T>: Clone { pub trait Distance<T>: Clone {
@@ -66,3 +72,45 @@ impl Distances {
mahalanobis::Mahalanobis::new(data) mahalanobis::Mahalanobis::new(data)
} }
} }
///
/// ### Pairwise dissimilarities.
///
/// Representing distances as pairwise dissimilarities, so to build a
/// graph of closest neighbours. This representation can be reused for
/// different implementations
/// (initially used in this library for [FastPair](algorithm/neighbour/fastpair)).
/// The edge of the subgraph is defined by `PairwiseDistance`.
/// The calling algorithm can store a list of distances as
/// a list of these structures.
///
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy)]
pub struct PairwiseDistance<T: RealNumber> {
/// index of the vector in the original `Matrix` or list
pub node: usize,
/// index of the closest neighbor in the original `Matrix` or same list
pub neighbour: Option<usize>,
/// measure of distance, according to the algorithm distance function
/// if the distance is None, the edge has value "infinite" or max distance
/// each algorithm has to match
pub distance: Option<T>,
}
impl<T: RealNumber> Eq for PairwiseDistance<T> {}
impl<T: RealNumber> PartialEq for PairwiseDistance<T> {
fn eq(&self, other: &Self) -> bool {
self.node == other.node
&& self.neighbour == other.neighbour
&& self.distance == other.distance
}
}
impl<T: RealNumber> PartialOrd for PairwiseDistance<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.distance.partial_cmp(&other.distance)
}
}
+4 -1
View File
@@ -82,7 +82,10 @@ impl<T: Number + RealNumber + FloatNumber> Metrics<T> for F1<T> {
mod tests { mod tests {
use super::*; use super::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn f1() { fn f1() {
let y_pred: Vec<f64> = vec![0., 0., 1., 1., 1., 1.]; let y_pred: Vec<f64> = vec![0., 0., 1., 1., 1., 1.];
+4 -1
View File
@@ -76,7 +76,10 @@ impl<T: Number + FloatNumber> Metrics<T> for MeanAbsoluteError<T> {
mod tests { mod tests {
use super::*; use super::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn mean_absolute_error() { fn mean_absolute_error() {
let y_true: Vec<f64> = vec![3., -0.5, 2., 7.]; let y_true: Vec<f64> = vec![3., -0.5, 2., 7.];
+4 -1
View File
@@ -76,7 +76,10 @@ impl<T: Number + FloatNumber> Metrics<T> for MeanSquareError<T> {
mod tests { mod tests {
use super::*; use super::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn mean_squared_error() { fn mean_squared_error() {
let y_true: Vec<f64> = vec![3., -0.5, 2., 7.]; let y_true: Vec<f64> = vec![3., -0.5, 2., 7.];
+19 -16
View File
@@ -55,7 +55,7 @@
pub mod accuracy; pub mod accuracy;
// TODO: reimplement AUC // TODO: reimplement AUC
// /// Computes Area Under the Receiver Operating Characteristic Curve (ROC AUC) from prediction scores. // /// Computes Area Under the Receiver Operating Characteristic Curve (ROC AUC) from prediction scores.
// pub mod auc; pub mod auc;
/// Compute the homogeneity, completeness and V-Measure scores. /// Compute the homogeneity, completeness and V-Measure scores.
pub mod cluster_hcv; pub mod cluster_hcv;
pub(crate) mod cluster_helpers; pub(crate) mod cluster_helpers;
@@ -84,7 +84,7 @@ use std::marker::PhantomData;
/// A trait to be implemented by all metrics /// A trait to be implemented by all metrics
pub trait Metrics<T> { pub trait Metrics<T> {
/// instantiate a new Metrics trait-object /// instantiate a new Metrics trait-object
/// https://doc.rust-lang.org/error-index.html#E0038 /// <https://doc.rust-lang.org/error-index.html#E0038>
fn new() -> Self fn new() -> Self
where where
Self: Sized; Self: Sized;
@@ -133,10 +133,10 @@ impl<T: Number + RealNumber + FloatNumber> ClassificationMetrics<T> {
f1::F1::new_with(beta) f1::F1::new_with(beta)
} }
// /// Area Under the Receiver Operating Characteristic Curve (ROC AUC), see [AUC](auc/index.html). /// Area Under the Receiver Operating Characteristic Curve (ROC AUC), see [AUC](auc/index.html).
// pub fn roc_auc_score() -> auc::AUC<T> { pub fn roc_auc_score() -> auc::AUC<T> {
// auc::AUC::<T>::new() auc::AUC::<T>::new()
// } }
} }
impl<T: Number + Ord> ClassificationMetricsOrd<T> { impl<T: Number + Ord> ClassificationMetricsOrd<T> {
@@ -212,16 +212,19 @@ pub fn f1<T: Number + RealNumber + FloatNumber, V: ArrayView1<T>>(
obj.get_score(y_true, y_pred) obj.get_score(y_true, y_pred)
} }
// /// AUC score, see [AUC](auc/index.html). /// AUC score, see [AUC](auc/index.html).
// /// * `y_true` - cround truth (correct) labels. /// * `y_true` - cround truth (correct) labels.
// /// * `y_pred_probabilities` - probability estimates, as returned by a classifier. /// * `y_pred_probabilities` - probability estimates, as returned by a classifier.
// pub fn roc_auc_score<T: Number + PartialOrd, V: ArrayView1<T> + Array1<T> + Array1<T>>( pub fn roc_auc_score<
// y_true: &V, T: Number + RealNumber + FloatNumber + PartialOrd,
// y_pred_probabilities: &V, V: ArrayView1<T> + Array1<T> + Array1<T>,
// ) -> T { >(
// let obj = ClassificationMetrics::<T>::roc_auc_score(); y_true: &V,
// obj.get_score(y_true, y_pred_probabilities) y_pred_probabilities: &V,
// } ) -> f64 {
let obj = ClassificationMetrics::<T>::roc_auc_score();
obj.get_score(y_true, y_pred_probabilities)
}
/// Computes mean squared error, see [mean squared error](mean_squared_error/index.html). /// Computes mean squared error, see [mean squared error](mean_squared_error/index.html).
/// * `y_true` - Ground truth (correct) target values. /// * `y_true` - Ground truth (correct) target values.
+8 -2
View File
@@ -95,7 +95,10 @@ impl<T: RealNumber> Metrics<T> for Precision<T> {
mod tests { mod tests {
use super::*; use super::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn precision() { fn precision() {
let y_true: Vec<f64> = vec![0., 1., 1., 0.]; let y_true: Vec<f64> = vec![0., 1., 1., 0.];
@@ -114,7 +117,10 @@ mod tests {
assert!((score3 - 0.5).abs() < 1e-8); assert!((score3 - 0.5).abs() < 1e-8);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn precision_multiclass() { fn precision_multiclass() {
let y_true: Vec<f64> = vec![0., 0., 0., 1., 1., 1., 2., 2., 2.]; let y_true: Vec<f64> = vec![0., 0., 0., 1., 1., 1., 2., 2., 2.];
+4 -1
View File
@@ -81,7 +81,10 @@ impl<T: Number> Metrics<T> for R2<T> {
mod tests { mod tests {
use super::*; use super::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn r2() { fn r2() {
let y_true: Vec<f64> = vec![3., -0.5, 2., 7.]; let y_true: Vec<f64> = vec![3., -0.5, 2., 7.];
+8 -2
View File
@@ -96,7 +96,10 @@ impl<T: RealNumber> Metrics<T> for Recall<T> {
mod tests { mod tests {
use super::*; use super::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn recall() { fn recall() {
let y_true: Vec<f64> = vec![0., 1., 1., 0.]; let y_true: Vec<f64> = vec![0., 1., 1., 0.];
@@ -115,7 +118,10 @@ mod tests {
assert!((score3 - 0.6666666666666666).abs() < 1e-8); assert!((score3 - 0.6666666666666666).abs() < 1e-8);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn recall_multiclass() { fn recall_multiclass() {
let y_true: Vec<f64> = vec![0., 0., 0., 1., 1., 1., 2., 2., 2.]; let y_true: Vec<f64> = vec![0., 0., 0., 1., 1., 1., 2., 2., 2.];
+28 -7
View File
@@ -159,7 +159,10 @@ mod tests {
use super::*; use super::*;
use crate::linalg::basic::matrix::DenseMatrix; use crate::linalg::basic::matrix::DenseMatrix;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn run_kfold_return_test_indices_simple() { fn run_kfold_return_test_indices_simple() {
let k = KFold { let k = KFold {
@@ -175,7 +178,10 @@ mod tests {
assert_eq!(test_indices[2], (22..33).collect::<Vec<usize>>()); assert_eq!(test_indices[2], (22..33).collect::<Vec<usize>>());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn run_kfold_return_test_indices_odd() { fn run_kfold_return_test_indices_odd() {
let k = KFold { let k = KFold {
@@ -191,7 +197,10 @@ mod tests {
assert_eq!(test_indices[2], (23..34).collect::<Vec<usize>>()); assert_eq!(test_indices[2], (23..34).collect::<Vec<usize>>());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn run_kfold_return_test_mask_simple() { fn run_kfold_return_test_mask_simple() {
let k = KFold { let k = KFold {
@@ -218,7 +227,10 @@ mod tests {
} }
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn run_kfold_return_split_simple() { fn run_kfold_return_split_simple() {
let k = KFold { let k = KFold {
@@ -235,7 +247,10 @@ mod tests {
assert_eq!(train_test_splits[1].1, (11..22).collect::<Vec<usize>>()); assert_eq!(train_test_splits[1].1, (11..22).collect::<Vec<usize>>());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn run_kfold_return_split_simple_shuffle() { fn run_kfold_return_split_simple_shuffle() {
let k = KFold { let k = KFold {
@@ -251,7 +266,10 @@ mod tests {
assert_eq!(train_test_splits[1].1.len(), 11_usize); assert_eq!(train_test_splits[1].1.len(), 11_usize);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn numpy_parity_test() { fn numpy_parity_test() {
let k = KFold { let k = KFold {
@@ -273,7 +291,10 @@ mod tests {
} }
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn numpy_parity_test_shuffle() { fn numpy_parity_test_shuffle() {
let k = KFold { let k = KFold {
+16 -4
View File
@@ -321,7 +321,10 @@ mod tests {
use crate::neighbors::knn_regressor::{KNNRegressor, KNNRegressorParameters}; use crate::neighbors::knn_regressor::{KNNRegressor, KNNRegressorParameters};
use crate::neighbors::KNNWeightFunction; use crate::neighbors::KNNWeightFunction;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn run_train_test_split() { fn run_train_test_split() {
let n = 123; let n = 123;
@@ -346,7 +349,10 @@ mod tests {
struct BiasedParameters {} struct BiasedParameters {}
impl NoParameters for BiasedParameters {} impl NoParameters for BiasedParameters {}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn test_cross_validate_biased() { fn test_cross_validate_biased() {
struct BiasedEstimator {} struct BiasedEstimator {}
@@ -412,7 +418,10 @@ mod tests {
assert_eq!(0.4, results.mean_train_score()); assert_eq!(0.4, results.mean_train_score());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn test_cross_validate_knn() { fn test_cross_validate_knn() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
@@ -457,7 +466,10 @@ mod tests {
assert!(results.mean_train_score() < results.mean_test_score()); assert!(results.mean_train_score() < results.mean_test_score());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn test_cross_val_predict_knn() { fn test_cross_val_predict_knn() {
let x: DenseMatrix<f64> = DenseMatrix::from_2d_array(&[ let x: DenseMatrix<f64> = DenseMatrix::from_2d_array(&[
+12 -3
View File
@@ -496,7 +496,10 @@ mod tests {
assert!(iter.next().is_none()); assert!(iter.next().is_none());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn run_bernoulli_naive_bayes() { fn run_bernoulli_naive_bayes() {
// Tests that BernoulliNB when alpha=1.0 gives the same values as // Tests that BernoulliNB when alpha=1.0 gives the same values as
@@ -551,7 +554,10 @@ mod tests {
assert_eq!(y_hat, &[1]); assert_eq!(y_hat, &[1]);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn bernoulli_nb_scikit_parity() { fn bernoulli_nb_scikit_parity() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
@@ -612,7 +618,10 @@ mod tests {
assert_eq!(y_hat, vec!(2, 2, 0, 0, 0, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0)); assert_eq!(y_hat, vec!(2, 2, 0, 0, 0, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0));
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
fn serde() { fn serde() {
+12 -3
View File
@@ -428,7 +428,10 @@ mod tests {
assert!(iter.next().is_none()); assert!(iter.next().is_none());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn run_categorical_naive_bayes() { fn run_categorical_naive_bayes() {
let x = DenseMatrix::<u32>::from_2d_array(&[ let x = DenseMatrix::<u32>::from_2d_array(&[
@@ -509,7 +512,10 @@ mod tests {
assert_eq!(y_hat, vec![0, 1]); assert_eq!(y_hat, vec![0, 1]);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn run_categorical_naive_bayes2() { fn run_categorical_naive_bayes2() {
let x = DenseMatrix::<u32>::from_2d_array(&[ let x = DenseMatrix::<u32>::from_2d_array(&[
@@ -535,7 +541,10 @@ mod tests {
assert_eq!(y_hat, vec![0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1]); assert_eq!(y_hat, vec![0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1]);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
fn serde() { fn serde() {
+12 -3
View File
@@ -372,7 +372,10 @@ mod tests {
assert!(iter.next().is_none()); assert!(iter.next().is_none());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn run_gaussian_naive_bayes() { fn run_gaussian_naive_bayes() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
@@ -409,7 +412,10 @@ mod tests {
); );
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn run_gaussian_naive_bayes_with_priors() { fn run_gaussian_naive_bayes_with_priors() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
@@ -429,7 +435,10 @@ mod tests {
assert_eq!(gnb.class_priors(), &priors); assert_eq!(gnb.class_priors(), &priors);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
fn serde() { fn serde() {
+12 -3
View File
@@ -403,7 +403,10 @@ mod tests {
assert!(iter.next().is_none()); assert!(iter.next().is_none());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn run_multinomial_naive_bayes() { fn run_multinomial_naive_bayes() {
// Tests that MultinomialNB when alpha=1.0 gives the same values as // Tests that MultinomialNB when alpha=1.0 gives the same values as
@@ -461,7 +464,10 @@ mod tests {
assert_eq!(y_hat, &[0]); assert_eq!(y_hat, &[0]);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn multinomial_nb_scikit_parity() { fn multinomial_nb_scikit_parity() {
let x = DenseMatrix::<u32>::from_2d_array(&[ let x = DenseMatrix::<u32>::from_2d_array(&[
@@ -524,7 +530,10 @@ mod tests {
assert_eq!(y_hat, vec!(2, 2, 0, 0, 0, 2, 2, 1, 0, 1, 0, 2, 0, 0, 2)); assert_eq!(y_hat, vec!(2, 2, 0, 0, 0, 2, 2, 1, 0, 1, 0, 2, 0, 0, 2));
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
fn serde() { fn serde() {
+12 -3
View File
@@ -305,7 +305,10 @@ mod tests {
use super::*; use super::*;
use crate::linalg::basic::matrix::DenseMatrix; use crate::linalg::basic::matrix::DenseMatrix;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn knn_fit_predict() { fn knn_fit_predict() {
let x = let x =
@@ -317,7 +320,10 @@ mod tests {
assert_eq!(y.to_vec(), y_hat); assert_eq!(y.to_vec(), y_hat);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn knn_fit_predict_weighted() { fn knn_fit_predict_weighted() {
let x = DenseMatrix::from_2d_array(&[&[1.], &[2.], &[3.], &[4.], &[5.]]); let x = DenseMatrix::from_2d_array(&[&[1.], &[2.], &[3.], &[4.], &[5.]]);
@@ -335,7 +341,10 @@ mod tests {
assert_eq!(vec![3], y_hat); assert_eq!(vec![3], y_hat);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
fn serde() { fn serde() {
+12 -3
View File
@@ -289,7 +289,10 @@ mod tests {
use crate::linalg::basic::matrix::DenseMatrix; use crate::linalg::basic::matrix::DenseMatrix;
use crate::metrics::distance::Distances; use crate::metrics::distance::Distances;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn knn_fit_predict_weighted() { fn knn_fit_predict_weighted() {
let x = let x =
@@ -313,7 +316,10 @@ mod tests {
} }
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn knn_fit_predict_uniform() { fn knn_fit_predict_uniform() {
let x = let x =
@@ -328,7 +334,10 @@ mod tests {
} }
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
fn serde() { fn serde() {
+5 -5
View File
@@ -1,8 +1,6 @@
use rand::Rng;
use num_traits::{Float, Signed}; use num_traits::{Float, Signed};
use crate::numbers::basenum::Number; use crate::{numbers::basenum::Number, rand_custom::get_rng_impl};
/// Defines float number /// Defines float number
/// <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_CHTML"></script> /// <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_CHTML"></script>
@@ -58,7 +56,8 @@ impl FloatNumber for f64 {
} }
fn rand() -> f64 { fn rand() -> f64 {
let mut rng = rand::thread_rng(); use rand::Rng;
let mut rng = get_rng_impl(None);
rng.gen() rng.gen()
} }
@@ -99,7 +98,8 @@ impl FloatNumber for f32 {
} }
fn rand() -> f32 { fn rand() -> f32 {
let mut rng = rand::thread_rng(); use rand::Rng;
let mut rng = get_rng_impl(None);
rng.gen() rng.gen()
} }
+1
View File
@@ -63,6 +63,7 @@ impl RealNumber for f64 {
} }
fn rand() -> f64 { fn rand() -> f64 {
// TODO: to be implemented, see issue smartcore#214
1.0 1.0
} }
@@ -99,7 +99,10 @@ mod tests {
use crate::optimization::line_search::Backtracking; use crate::optimization::line_search::Backtracking;
use crate::optimization::FunctionOrder; use crate::optimization::FunctionOrder;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn gradient_descent() { fn gradient_descent() {
let x0 = vec![-1., 1.]; let x0 = vec![-1., 1.];
+4 -1
View File
@@ -278,7 +278,10 @@ mod tests {
use crate::optimization::line_search::Backtracking; use crate::optimization::line_search::Backtracking;
use crate::optimization::FunctionOrder; use crate::optimization::FunctionOrder;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn lbfgs() { fn lbfgs() {
let x0 = vec![0., 0.]; let x0 = vec![0., 0.];
+4 -1
View File
@@ -129,7 +129,10 @@ impl<T: Float> LineSearchMethod<T> for Backtracking<T> {
mod tests { mod tests {
use super::*; use super::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn backtracking() { fn backtracking() {
let f = |x: f64| -> f64 { x.powf(2.) + x }; let f = |x: f64| -> f64 { x.powf(2.) + x };
+20 -5
View File
@@ -224,7 +224,10 @@ mod tests {
use crate::linalg::basic::matrix::DenseMatrix; use crate::linalg::basic::matrix::DenseMatrix;
use crate::preprocessing::series_encoder::CategoryMapper; use crate::preprocessing::series_encoder::CategoryMapper;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn adjust_idxs() { fn adjust_idxs() {
assert_eq!(find_new_idxs(0, &[], &[]), Vec::<usize>::new()); assert_eq!(find_new_idxs(0, &[], &[]), Vec::<usize>::new());
@@ -269,7 +272,10 @@ mod tests {
(orig, oh_enc) (orig, oh_enc)
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn hash_encode_f64_series() { fn hash_encode_f64_series() {
let series = vec![3.0, 1.0, 2.0, 1.0]; let series = vec![3.0, 1.0, 2.0, 1.0];
@@ -280,7 +286,10 @@ mod tests {
let orig_val: f64 = inv.unwrap().into(); let orig_val: f64 = inv.unwrap().into();
assert_eq!(orig_val, 2.0); assert_eq!(orig_val, 2.0);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn test_fit() { fn test_fit() {
let (x, _) = build_fake_matrix(); let (x, _) = build_fake_matrix();
@@ -296,7 +305,10 @@ mod tests {
assert_eq!(num_cat, vec![2, 4]); assert_eq!(num_cat, vec![2, 4]);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn matrix_transform_test() { fn matrix_transform_test() {
let (x, expected_x) = build_fake_matrix(); let (x, expected_x) = build_fake_matrix();
@@ -312,7 +324,10 @@ mod tests {
assert_eq!(nm, expected_x); assert_eq!(nm, expected_x);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn fail_on_bad_category() { fn fail_on_bad_category() {
let m = DenseMatrix::from_2d_array(&[ let m = DenseMatrix::from_2d_array(&[
+4 -1
View File
@@ -420,7 +420,10 @@ mod tests {
/// Same as `fit_for_random_values` test, but using a `StandardScaler` that has been /// Same as `fit_for_random_values` test, but using a `StandardScaler` that has been
/// serialized and deserialized. /// serialized and deserialized.
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
fn serde_fit_for_random_values() { fn serde_fit_for_random_values() {
+24 -6
View File
@@ -199,7 +199,10 @@ where
mod tests { mod tests {
use super::*; use super::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn from_categories() { fn from_categories() {
let fake_categories: Vec<usize> = vec![1, 2, 3, 4, 5, 3, 5, 3, 1, 2, 4]; let fake_categories: Vec<usize> = vec![1, 2, 3, 4, 5, 3, 5, 3, 1, 2, 4];
@@ -218,14 +221,20 @@ mod tests {
let enc = CategoryMapper::<&str>::from_positional_category_vec(fake_category_pos); let enc = CategoryMapper::<&str>::from_positional_category_vec(fake_category_pos);
enc enc
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn ordinal_encoding() { fn ordinal_encoding() {
let enc = build_fake_str_enc(); let enc = build_fake_str_enc();
assert_eq!(1f64, enc.get_ordinal::<f64>(&"dog").unwrap()) assert_eq!(1f64, enc.get_ordinal::<f64>(&"dog").unwrap())
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn category_map_and_vec() { fn category_map_and_vec() {
let category_map: HashMap<&str, usize> = vec![("background", 0), ("dog", 1), ("cat", 2)] let category_map: HashMap<&str, usize> = vec![("background", 0), ("dog", 1), ("cat", 2)]
@@ -240,7 +249,10 @@ mod tests {
assert_eq!(oh_vec, res); assert_eq!(oh_vec, res);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn positional_categories_vec() { fn positional_categories_vec() {
let enc = build_fake_str_enc(); let enc = build_fake_str_enc();
@@ -252,7 +264,10 @@ mod tests {
assert_eq!(oh_vec, res); assert_eq!(oh_vec, res);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn invert_label_test() { fn invert_label_test() {
let enc = build_fake_str_enc(); let enc = build_fake_str_enc();
@@ -265,7 +280,10 @@ mod tests {
}; };
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn test_many_categorys() { fn test_many_categorys() {
let enc = build_fake_str_enc(); let enc = build_fake_str_enc();
+21 -5
View File
@@ -22,6 +22,8 @@
//! //!
//! <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script> //! <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
//! <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script> //! <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
/// search parameters
pub mod search;
pub mod svc; pub mod svc;
pub mod svr; pub mod svr;
@@ -52,6 +54,7 @@ impl<'a> Debug for dyn Kernel<'_> + 'a {
} }
} }
#[cfg(feature = "serde")]
impl<'a> Serialize for dyn Kernel<'_> + 'a { impl<'a> Serialize for dyn Kernel<'_> + 'a {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
@@ -64,7 +67,8 @@ impl<'a> Serialize for dyn Kernel<'_> + 'a {
} }
/// Pre-defined kernel functions /// Pre-defined kernel functions
#[derive(Debug, Clone, Serialize, Deserialize)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct Kernels {} pub struct Kernels {}
impl<'a> Kernels { impl<'a> Kernels {
@@ -267,7 +271,10 @@ mod tests {
use super::*; use super::*;
use crate::svm::Kernels; use crate::svm::Kernels;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn linear_kernel() { fn linear_kernel() {
let v1 = vec![1., 2., 3.]; let v1 = vec![1., 2., 3.];
@@ -276,7 +283,10 @@ mod tests {
assert_eq!(32f64, Kernels::linear().apply(&v1, &v2).unwrap()); assert_eq!(32f64, Kernels::linear().apply(&v1, &v2).unwrap());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn rbf_kernel() { fn rbf_kernel() {
let v1 = vec![1., 2., 3.]; let v1 = vec![1., 2., 3.];
@@ -291,7 +301,10 @@ mod tests {
assert!((0.2265f64 - result) < 1e-4); assert!((0.2265f64 - result) < 1e-4);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn polynomial_kernel() { fn polynomial_kernel() {
let v1 = vec![1., 2., 3.]; let v1 = vec![1., 2., 3.];
@@ -306,7 +319,10 @@ mod tests {
assert!((4913f64 - result) < std::f64::EPSILON); assert!((4913f64 - result) < std::f64::EPSILON);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn sigmoid_kernel() { fn sigmoid_kernel() {
let v1 = vec![1., 2., 3.]; let v1 = vec![1., 2., 3.];
+4
View File
@@ -0,0 +1,4 @@
/// SVC search parameters
pub mod svc_params;
/// SVC search parameters
pub mod svr_params;
+183
View File
@@ -0,0 +1,183 @@
// /// SVC grid search parameters
// #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
// #[derive(Debug, Clone)]
// pub struct SVCSearchParameters<
// TX: Number + RealNumber,
// TY: Number + Ord,
// X: Array2<TX>,
// Y: Array1<TY>,
// K: Kernel,
// > {
// #[cfg_attr(feature = "serde", serde(default))]
// /// Number of epochs.
// pub epoch: Vec<usize>,
// #[cfg_attr(feature = "serde", serde(default))]
// /// Regularization parameter.
// pub c: Vec<TX>,
// #[cfg_attr(feature = "serde", serde(default))]
// /// Tolerance for stopping epoch.
// pub tol: Vec<TX>,
// #[cfg_attr(feature = "serde", serde(default))]
// /// The kernel function.
// pub kernel: Vec<K>,
// #[cfg_attr(feature = "serde", serde(default))]
// /// Unused parameter.
// m: PhantomData<(X, Y, TY)>,
// #[cfg_attr(feature = "serde", serde(default))]
// /// Controls the pseudo random number generation for shuffling the data for probability estimates
// seed: Vec<Option<u64>>,
// }
// /// SVC grid search iterator
// pub struct SVCSearchParametersIterator<
// TX: Number + RealNumber,
// TY: Number + Ord,
// X: Array2<TX>,
// Y: Array1<TY>,
// K: Kernel,
// > {
// svc_search_parameters: SVCSearchParameters<TX, TY, X, Y, K>,
// current_epoch: usize,
// current_c: usize,
// current_tol: usize,
// current_kernel: usize,
// current_seed: usize,
// }
// impl<TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>, K: Kernel>
// IntoIterator for SVCSearchParameters<TX, TY, X, Y, K>
// {
// type Item = SVCParameters<'a, TX, TY, X, Y>;
// type IntoIter = SVCSearchParametersIterator<TX, TY, X, Y, K>;
// fn into_iter(self) -> Self::IntoIter {
// SVCSearchParametersIterator {
// svc_search_parameters: self,
// current_epoch: 0,
// current_c: 0,
// current_tol: 0,
// current_kernel: 0,
// current_seed: 0,
// }
// }
// }
// impl<TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>, K: Kernel>
// Iterator for SVCSearchParametersIterator<TX, TY, X, Y, K>
// {
// type Item = SVCParameters<TX, TY, X, Y>;
// fn next(&mut self) -> Option<Self::Item> {
// if self.current_epoch == self.svc_search_parameters.epoch.len()
// && self.current_c == self.svc_search_parameters.c.len()
// && self.current_tol == self.svc_search_parameters.tol.len()
// && self.current_kernel == self.svc_search_parameters.kernel.len()
// && self.current_seed == self.svc_search_parameters.seed.len()
// {
// return None;
// }
// let next = SVCParameters {
// epoch: self.svc_search_parameters.epoch[self.current_epoch],
// c: self.svc_search_parameters.c[self.current_c],
// tol: self.svc_search_parameters.tol[self.current_tol],
// kernel: self.svc_search_parameters.kernel[self.current_kernel].clone(),
// m: PhantomData,
// seed: self.svc_search_parameters.seed[self.current_seed],
// };
// if self.current_epoch + 1 < self.svc_search_parameters.epoch.len() {
// self.current_epoch += 1;
// } else if self.current_c + 1 < self.svc_search_parameters.c.len() {
// self.current_epoch = 0;
// self.current_c += 1;
// } else if self.current_tol + 1 < self.svc_search_parameters.tol.len() {
// self.current_epoch = 0;
// self.current_c = 0;
// self.current_tol += 1;
// } else if self.current_kernel + 1 < self.svc_search_parameters.kernel.len() {
// self.current_epoch = 0;
// self.current_c = 0;
// self.current_tol = 0;
// self.current_kernel += 1;
// } else if self.current_seed + 1 < self.svc_search_parameters.seed.len() {
// self.current_epoch = 0;
// self.current_c = 0;
// self.current_tol = 0;
// self.current_kernel = 0;
// self.current_seed += 1;
// } else {
// self.current_epoch += 1;
// self.current_c += 1;
// self.current_tol += 1;
// self.current_kernel += 1;
// self.current_seed += 1;
// }
// Some(next)
// }
// }
// impl<TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>, K: Kernel> Default
// for SVCSearchParameters<TX, TY, X, Y, K>
// {
// fn default() -> Self {
// let default_params: SVCParameters<TX, TY, X, Y> = SVCParameters::default();
// SVCSearchParameters {
// epoch: vec![default_params.epoch],
// c: vec![default_params.c],
// tol: vec![default_params.tol],
// kernel: vec![default_params.kernel],
// m: PhantomData,
// seed: vec![default_params.seed],
// }
// }
// }
// #[cfg(test)]
// mod tests {
// use num::ToPrimitive;
// use super::*;
// use crate::linalg::basic::matrix::DenseMatrix;
// use crate::metrics::accuracy;
// #[cfg(feature = "serde")]
// use crate::svm::*;
// #[test]
// fn search_parameters() {
// let parameters: SVCSearchParameters<f64, DenseMatrix<f64>, LinearKernel> =
// SVCSearchParameters {
// epoch: vec![10, 100],
// kernel: vec![LinearKernel {}],
// ..Default::default()
// };
// let mut iter = parameters.into_iter();
// let next = iter.next().unwrap();
// assert_eq!(next.epoch, 10);
// assert_eq!(next.kernel, LinearKernel {});
// let next = iter.next().unwrap();
// assert_eq!(next.epoch, 100);
// assert_eq!(next.kernel, LinearKernel {});
// assert!(iter.next().is_none());
// }
// #[test]
// fn search_parameters() {
// let parameters: SVCSearchParameters<f64, DenseMatrix<f64>, LinearKernel> =
// SVCSearchParameters {
// epoch: vec![10, 100],
// kernel: vec![LinearKernel {}],
// ..Default::default()
// };
// let mut iter = parameters.into_iter();
// let next = iter.next().unwrap();
// assert_eq!(next.epoch, 10);
// assert_eq!(next.kernel, LinearKernel {});
// let next = iter.next().unwrap();
// assert_eq!(next.epoch, 100);
// assert_eq!(next.kernel, LinearKernel {});
// assert!(iter.next().is_none());
// }
// }
+112
View File
@@ -0,0 +1,112 @@
// /// SVR grid search parameters
// #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
// #[derive(Debug, Clone)]
// pub struct SVRSearchParameters<T: Number + RealNumber, M: Matrix<T>, K: Kernel<T, M::RowVector>> {
// /// Epsilon in the epsilon-SVR model.
// pub eps: Vec<T>,
// /// Regularization parameter.
// pub c: Vec<T>,
// /// Tolerance for stopping eps.
// pub tol: Vec<T>,
// /// The kernel function.
// pub kernel: Vec<K>,
// /// Unused parameter.
// m: PhantomData<M>,
// }
// /// SVR grid search iterator
// pub struct SVRSearchParametersIterator<T: Number + RealNumber, M: Matrix<T>, K: Kernel<T, M::RowVector>> {
// svr_search_parameters: SVRSearchParameters<T, M, K>,
// current_eps: usize,
// current_c: usize,
// current_tol: usize,
// current_kernel: usize,
// }
// impl<T: Number + RealNumber, M: Matrix<T>, K: Kernel<T, M::RowVector>> IntoIterator
// for SVRSearchParameters<T, M, K>
// {
// type Item = SVRParameters<T, M, K>;
// type IntoIter = SVRSearchParametersIterator<T, M, K>;
// fn into_iter(self) -> Self::IntoIter {
// SVRSearchParametersIterator {
// svr_search_parameters: self,
// current_eps: 0,
// current_c: 0,
// current_tol: 0,
// current_kernel: 0,
// }
// }
// }
// impl<T: Number + RealNumber, M: Matrix<T>, K: Kernel<T, M::RowVector>> Iterator
// for SVRSearchParametersIterator<T, M, K>
// {
// type Item = SVRParameters<T, M, K>;
// fn next(&mut self) -> Option<Self::Item> {
// if self.current_eps == self.svr_search_parameters.eps.len()
// && self.current_c == self.svr_search_parameters.c.len()
// && self.current_tol == self.svr_search_parameters.tol.len()
// && self.current_kernel == self.svr_search_parameters.kernel.len()
// {
// return None;
// }
// let next = SVRParameters::<T, M, K> {
// eps: self.svr_search_parameters.eps[self.current_eps],
// c: self.svr_search_parameters.c[self.current_c],
// tol: self.svr_search_parameters.tol[self.current_tol],
// kernel: self.svr_search_parameters.kernel[self.current_kernel].clone(),
// m: PhantomData,
// };
// if self.current_eps + 1 < self.svr_search_parameters.eps.len() {
// self.current_eps += 1;
// } else if self.current_c + 1 < self.svr_search_parameters.c.len() {
// self.current_eps = 0;
// self.current_c += 1;
// } else if self.current_tol + 1 < self.svr_search_parameters.tol.len() {
// self.current_eps = 0;
// self.current_c = 0;
// self.current_tol += 1;
// } else if self.current_kernel + 1 < self.svr_search_parameters.kernel.len() {
// self.current_eps = 0;
// self.current_c = 0;
// self.current_tol = 0;
// self.current_kernel += 1;
// } else {
// self.current_eps += 1;
// self.current_c += 1;
// self.current_tol += 1;
// self.current_kernel += 1;
// }
// Some(next)
// }
// }
// impl<T: Number + RealNumber, M: Matrix<T>> Default for SVRSearchParameters<T, M, LinearKernel> {
// fn default() -> Self {
// let default_params: SVRParameters<T, M, LinearKernel> = SVRParameters::default();
// SVRSearchParameters {
// eps: vec![default_params.eps],
// c: vec![default_params.c],
// tol: vec![default_params.tol],
// kernel: vec![default_params.kernel],
// m: PhantomData,
// }
// }
// }
// #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
// #[derive(Debug)]
// #[cfg_attr(
// feature = "serde",
// serde(bound(
// serialize = "M::RowVector: Serialize, K: Serialize, T: Serialize",
// deserialize = "M::RowVector: Deserialize<'de>, K: Deserialize<'de>, T: Deserialize<'de>",
// ))
// )]
+17 -10
View File
@@ -100,22 +100,17 @@ pub struct SVCParameters<
X: Array2<TX>, X: Array2<TX>,
Y: Array1<TY>, Y: Array1<TY>,
> { > {
#[cfg_attr(feature = "serde", serde(default))]
/// Number of epochs. /// Number of epochs.
pub epoch: usize, pub epoch: usize,
#[cfg_attr(feature = "serde", serde(default))]
/// Regularization parameter. /// Regularization parameter.
pub c: TX, pub c: TX,
#[cfg_attr(feature = "serde", serde(default))]
/// Tolerance for stopping criterion. /// Tolerance for stopping criterion.
pub tol: TX, pub tol: TX,
#[cfg_attr(feature = "serde", serde(skip_deserializing))] #[cfg_attr(feature = "serde", serde(skip_deserializing))]
/// The kernel function. /// The kernel function.
pub kernel: Option<&'a dyn Kernel<'a>>, pub kernel: Option<&'a dyn Kernel<'a>>,
#[cfg_attr(feature = "serde", serde(default))]
/// Unused parameter. /// Unused parameter.
m: PhantomData<(X, Y, TY)>, m: PhantomData<(X, Y, TY)>,
#[cfg_attr(feature = "serde", serde(default))]
/// Controls the pseudo random number generation for shuffling the data for probability estimates /// Controls the pseudo random number generation for shuffling the data for probability estimates
seed: Option<u64>, seed: Option<u64>,
} }
@@ -133,7 +128,7 @@ pub struct SVCParameters<
pub struct SVC<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>> { pub struct SVC<'a, TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>> {
classes: Option<Vec<TY>>, classes: Option<Vec<TY>>,
instances: Option<Vec<Vec<TX>>>, instances: Option<Vec<Vec<TX>>>,
#[serde(skip)] #[cfg_attr(feature = "serde", serde(skip))]
parameters: Option<&'a SVCParameters<'a, TX, TY, X, Y>>, parameters: Option<&'a SVCParameters<'a, TX, TY, X, Y>>,
w: Option<Vec<TX>>, w: Option<Vec<TX>>,
b: Option<TX>, b: Option<TX>,
@@ -948,7 +943,10 @@ mod tests {
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use crate::svm::*; use crate::svm::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn svc_fit_predict() { fn svc_fit_predict() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
@@ -996,7 +994,10 @@ mod tests {
); );
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn svc_fit_decision_function() { fn svc_fit_decision_function() {
let x = DenseMatrix::from_2d_array(&[&[4.0, 0.0], &[0.0, 4.0], &[8.0, 0.0], &[0.0, 8.0]]); let x = DenseMatrix::from_2d_array(&[&[4.0, 0.0], &[0.0, 4.0], &[8.0, 0.0], &[0.0, 8.0]]);
@@ -1034,7 +1035,10 @@ mod tests {
assert!(num::Float::abs(y_hat[0]) <= 0.1); assert!(num::Float::abs(y_hat[0]) <= 0.1);
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn svc_fit_predict_rbf() { fn svc_fit_predict_rbf() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
@@ -1083,7 +1087,10 @@ mod tests {
); );
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
fn svc_serde() { fn svc_serde() {
-184
View File
@@ -1,184 +0,0 @@
/// SVC grid search parameters
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct SVCSearchParameters<
TX: Number + RealNumber,
TY: Number + Ord,
X: Array2<TX>,
Y: Array1<TY>,
K: Kernel,
> {
#[cfg_attr(feature = "serde", serde(default))]
/// Number of epochs.
pub epoch: Vec<usize>,
#[cfg_attr(feature = "serde", serde(default))]
/// Regularization parameter.
pub c: Vec<TX>,
#[cfg_attr(feature = "serde", serde(default))]
/// Tolerance for stopping epoch.
pub tol: Vec<TX>,
#[cfg_attr(feature = "serde", serde(default))]
/// The kernel function.
pub kernel: Vec<K>,
#[cfg_attr(feature = "serde", serde(default))]
/// Unused parameter.
m: PhantomData<(X, Y, TY)>,
#[cfg_attr(feature = "serde", serde(default))]
/// Controls the pseudo random number generation for shuffling the data for probability estimates
seed: Vec<Option<u64>>,
}
/// SVC grid search iterator
pub struct SVCSearchParametersIterator<
TX: Number + RealNumber,
TY: Number + Ord,
X: Array2<TX>,
Y: Array1<TY>,
K: Kernel,
> {
svc_search_parameters: SVCSearchParameters<TX, TY, X, Y, K>,
current_epoch: usize,
current_c: usize,
current_tol: usize,
current_kernel: usize,
current_seed: usize,
}
impl<TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>, K: Kernel>
IntoIterator for SVCSearchParameters<TX, TY, X, Y, K>
{
type Item = SVCParameters<'a, TX, TY, X, Y>;
type IntoIter = SVCSearchParametersIterator<TX, TY, X, Y, K>;
fn into_iter(self) -> Self::IntoIter {
SVCSearchParametersIterator {
svc_search_parameters: self,
current_epoch: 0,
current_c: 0,
current_tol: 0,
current_kernel: 0,
current_seed: 0,
}
}
}
impl<TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>, K: Kernel>
Iterator for SVCSearchParametersIterator<TX, TY, X, Y, K>
{
type Item = SVCParameters<TX, TY, X, Y>;
fn next(&mut self) -> Option<Self::Item> {
if self.current_epoch == self.svc_search_parameters.epoch.len()
&& self.current_c == self.svc_search_parameters.c.len()
&& self.current_tol == self.svc_search_parameters.tol.len()
&& self.current_kernel == self.svc_search_parameters.kernel.len()
&& self.current_seed == self.svc_search_parameters.seed.len()
{
return None;
}
let next = SVCParameters {
epoch: self.svc_search_parameters.epoch[self.current_epoch],
c: self.svc_search_parameters.c[self.current_c],
tol: self.svc_search_parameters.tol[self.current_tol],
kernel: self.svc_search_parameters.kernel[self.current_kernel].clone(),
m: PhantomData,
seed: self.svc_search_parameters.seed[self.current_seed],
};
if self.current_epoch + 1 < self.svc_search_parameters.epoch.len() {
self.current_epoch += 1;
} else if self.current_c + 1 < self.svc_search_parameters.c.len() {
self.current_epoch = 0;
self.current_c += 1;
} else if self.current_tol + 1 < self.svc_search_parameters.tol.len() {
self.current_epoch = 0;
self.current_c = 0;
self.current_tol += 1;
} else if self.current_kernel + 1 < self.svc_search_parameters.kernel.len() {
self.current_epoch = 0;
self.current_c = 0;
self.current_tol = 0;
self.current_kernel += 1;
} else if self.current_seed + 1 < self.svc_search_parameters.seed.len() {
self.current_epoch = 0;
self.current_c = 0;
self.current_tol = 0;
self.current_kernel = 0;
self.current_seed += 1;
} else {
self.current_epoch += 1;
self.current_c += 1;
self.current_tol += 1;
self.current_kernel += 1;
self.current_seed += 1;
}
Some(next)
}
}
impl<TX: Number + RealNumber, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>, K: Kernel> Default
for SVCSearchParameters<TX, TY, X, Y, K>
{
fn default() -> Self {
let default_params: SVCParameters<TX, TY, X, Y> = SVCParameters::default();
SVCSearchParameters {
epoch: vec![default_params.epoch],
c: vec![default_params.c],
tol: vec![default_params.tol],
kernel: vec![default_params.kernel],
m: PhantomData,
seed: vec![default_params.seed],
}
}
}
#[cfg(test)]
mod tests {
use num::ToPrimitive;
use super::*;
use crate::linalg::basic::matrix::DenseMatrix;
use crate::metrics::accuracy;
#[cfg(feature = "serde")]
use crate::svm::*;
#[test]
fn search_parameters() {
let parameters: SVCSearchParameters<f64, DenseMatrix<f64>, LinearKernel> =
SVCSearchParameters {
epoch: vec![10, 100],
kernel: vec![LinearKernel {}],
..Default::default()
};
let mut iter = parameters.into_iter();
let next = iter.next().unwrap();
assert_eq!(next.epoch, 10);
assert_eq!(next.kernel, LinearKernel {});
let next = iter.next().unwrap();
assert_eq!(next.epoch, 100);
assert_eq!(next.kernel, LinearKernel {});
assert!(iter.next().is_none());
}
#[test]
fn search_parameters() {
let parameters: SVCSearchParameters<f64, DenseMatrix<f64>, LinearKernel> =
SVCSearchParameters {
epoch: vec![10, 100],
kernel: vec![LinearKernel {}],
..Default::default()
};
let mut iter = parameters.into_iter();
let next = iter.next().unwrap();
assert_eq!(next.epoch, 10);
assert_eq!(next.kernel, LinearKernel {});
let next = iter.next().unwrap();
assert_eq!(next.epoch, 100);
assert_eq!(next.kernel, LinearKernel {});
assert!(iter.next().is_none());
}
}
+114 -197
View File
@@ -79,140 +79,30 @@ use crate::api::{PredictorBorrow, SupervisedEstimatorBorrow};
use crate::error::{Failed, FailedError}; use crate::error::{Failed, FailedError};
use crate::linalg::basic::arrays::{Array1, Array2, MutArray}; use crate::linalg::basic::arrays::{Array1, Array2, MutArray};
use crate::numbers::basenum::Number; use crate::numbers::basenum::Number;
use crate::numbers::realnum::RealNumber; use crate::numbers::floatnum::FloatNumber;
use crate::svm::Kernel; use crate::svm::Kernel;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
/// SVR Parameters /// SVR Parameters
pub struct SVRParameters<'a, T: Number + RealNumber> { pub struct SVRParameters<'a, T: Number + FloatNumber + PartialOrd> {
/// Epsilon in the epsilon-SVR model. /// Epsilon in the epsilon-SVR model.
pub eps: T, pub eps: T,
/// Regularization parameter. /// Regularization parameter.
pub c: T, pub c: T,
/// Tolerance for stopping criterion. /// Tolerance for stopping criterion.
pub tol: T, pub tol: T,
#[serde(skip_deserializing)] #[cfg_attr(feature = "serde", serde(skip_deserializing))]
/// The kernel function. /// The kernel function.
pub kernel: Option<&'a dyn Kernel<'a>>, pub kernel: Option<&'a dyn Kernel<'a>>,
} }
// /// SVR grid search parameters #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
// #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug)]
// #[derive(Debug, Clone)]
// pub struct SVRSearchParameters<T: Number + RealNumber, M: Matrix<T>, K: Kernel<T, M::RowVector>> {
// /// Epsilon in the epsilon-SVR model.
// pub eps: Vec<T>,
// /// Regularization parameter.
// pub c: Vec<T>,
// /// Tolerance for stopping eps.
// pub tol: Vec<T>,
// /// The kernel function.
// pub kernel: Vec<K>,
// /// Unused parameter.
// m: PhantomData<M>,
// }
// /// SVR grid search iterator
// pub struct SVRSearchParametersIterator<T: Number + RealNumber, M: Matrix<T>, K: Kernel<T, M::RowVector>> {
// svr_search_parameters: SVRSearchParameters<T, M, K>,
// current_eps: usize,
// current_c: usize,
// current_tol: usize,
// current_kernel: usize,
// }
// impl<T: Number + RealNumber, M: Matrix<T>, K: Kernel<T, M::RowVector>> IntoIterator
// for SVRSearchParameters<T, M, K>
// {
// type Item = SVRParameters<T, M, K>;
// type IntoIter = SVRSearchParametersIterator<T, M, K>;
// fn into_iter(self) -> Self::IntoIter {
// SVRSearchParametersIterator {
// svr_search_parameters: self,
// current_eps: 0,
// current_c: 0,
// current_tol: 0,
// current_kernel: 0,
// }
// }
// }
// impl<T: Number + RealNumber, M: Matrix<T>, K: Kernel<T, M::RowVector>> Iterator
// for SVRSearchParametersIterator<T, M, K>
// {
// type Item = SVRParameters<T, M, K>;
// fn next(&mut self) -> Option<Self::Item> {
// if self.current_eps == self.svr_search_parameters.eps.len()
// && self.current_c == self.svr_search_parameters.c.len()
// && self.current_tol == self.svr_search_parameters.tol.len()
// && self.current_kernel == self.svr_search_parameters.kernel.len()
// {
// return None;
// }
// let next = SVRParameters::<T, M, K> {
// eps: self.svr_search_parameters.eps[self.current_eps],
// c: self.svr_search_parameters.c[self.current_c],
// tol: self.svr_search_parameters.tol[self.current_tol],
// kernel: self.svr_search_parameters.kernel[self.current_kernel].clone(),
// m: PhantomData,
// };
// if self.current_eps + 1 < self.svr_search_parameters.eps.len() {
// self.current_eps += 1;
// } else if self.current_c + 1 < self.svr_search_parameters.c.len() {
// self.current_eps = 0;
// self.current_c += 1;
// } else if self.current_tol + 1 < self.svr_search_parameters.tol.len() {
// self.current_eps = 0;
// self.current_c = 0;
// self.current_tol += 1;
// } else if self.current_kernel + 1 < self.svr_search_parameters.kernel.len() {
// self.current_eps = 0;
// self.current_c = 0;
// self.current_tol = 0;
// self.current_kernel += 1;
// } else {
// self.current_eps += 1;
// self.current_c += 1;
// self.current_tol += 1;
// self.current_kernel += 1;
// }
// Some(next)
// }
// }
// impl<T: Number + RealNumber, M: Matrix<T>> Default for SVRSearchParameters<T, M, LinearKernel> {
// fn default() -> Self {
// let default_params: SVRParameters<T, M, LinearKernel> = SVRParameters::default();
// SVRSearchParameters {
// eps: vec![default_params.eps],
// c: vec![default_params.c],
// tol: vec![default_params.tol],
// kernel: vec![default_params.kernel],
// m: PhantomData,
// }
// }
// }
// #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
// #[derive(Debug)]
// #[cfg_attr(
// feature = "serde",
// serde(bound(
// serialize = "M::RowVector: Serialize, K: Serialize, T: Serialize",
// deserialize = "M::RowVector: Deserialize<'de>, K: Deserialize<'de>, T: Deserialize<'de>",
// ))
// )]
/// Epsilon-Support Vector Regression /// Epsilon-Support Vector Regression
pub struct SVR<'a, T: Number + RealNumber, X: Array2<T>, Y: Array1<T>> { pub struct SVR<'a, T: Number + FloatNumber + PartialOrd, X: Array2<T>, Y: Array1<T>> {
instances: Option<Vec<Vec<f64>>>, instances: Option<Vec<Vec<f64>>>,
#[cfg_attr(feature = "serde", serde(skip_deserializing))]
parameters: Option<&'a SVRParameters<'a, T>>, parameters: Option<&'a SVRParameters<'a, T>>,
w: Option<Vec<T>>, w: Option<Vec<T>>,
b: T, b: T,
@@ -230,7 +120,7 @@ struct SupportVector<T> {
} }
/// Sequential Minimal Optimization algorithm /// Sequential Minimal Optimization algorithm
struct Optimizer<'a, T: Number + RealNumber> { struct Optimizer<'a, T: Number + FloatNumber + PartialOrd> {
tol: T, tol: T,
c: T, c: T,
parameters: Option<&'a SVRParameters<'a, T>>, parameters: Option<&'a SVRParameters<'a, T>>,
@@ -242,13 +132,15 @@ struct Optimizer<'a, T: Number + RealNumber> {
gmaxindex: usize, gmaxindex: usize,
tau: T, tau: T,
sv: Vec<SupportVector<T>>, sv: Vec<SupportVector<T>>,
/// avoid infinite loop if SMO does not converge
max_iterations: usize,
} }
struct Cache<T: Clone> { struct Cache<T: Clone> {
data: Vec<RefCell<Option<Vec<T>>>>, data: Vec<RefCell<Option<Vec<T>>>>,
} }
impl<'a, T: Number + RealNumber> SVRParameters<'a, T> { impl<'a, T: Number + FloatNumber + PartialOrd> SVRParameters<'a, T> {
/// Epsilon in the epsilon-SVR model. /// Epsilon in the epsilon-SVR model.
pub fn with_eps(mut self, eps: T) -> Self { pub fn with_eps(mut self, eps: T) -> Self {
self.eps = eps; self.eps = eps;
@@ -271,7 +163,7 @@ impl<'a, T: Number + RealNumber> SVRParameters<'a, T> {
} }
} }
impl<'a, T: Number + RealNumber> Default for SVRParameters<'a, T> { impl<'a, T: Number + FloatNumber + PartialOrd> Default for SVRParameters<'a, T> {
fn default() -> Self { fn default() -> Self {
SVRParameters { SVRParameters {
eps: T::from_f64(0.1).unwrap(), eps: T::from_f64(0.1).unwrap(),
@@ -282,7 +174,7 @@ impl<'a, T: Number + RealNumber> Default for SVRParameters<'a, T> {
} }
} }
impl<'a, T: Number + RealNumber, X: Array2<T>, Y: Array1<T>> impl<'a, T: Number + FloatNumber + PartialOrd, X: Array2<T>, Y: Array1<T>>
SupervisedEstimatorBorrow<'a, X, Y, SVRParameters<'a, T>> for SVR<'a, T, X, Y> SupervisedEstimatorBorrow<'a, X, Y, SVRParameters<'a, T>> for SVR<'a, T, X, Y>
{ {
fn new() -> Self { fn new() -> Self {
@@ -299,7 +191,7 @@ impl<'a, T: Number + RealNumber, X: Array2<T>, Y: Array1<T>>
} }
} }
impl<'a, T: Number + RealNumber, X: Array2<T>, Y: Array1<T>> PredictorBorrow<'a, X, T> impl<'a, T: Number + FloatNumber + PartialOrd, X: Array2<T>, Y: Array1<T>> PredictorBorrow<'a, X, T>
for SVR<'a, T, X, Y> for SVR<'a, T, X, Y>
{ {
fn predict(&self, x: &'a X) -> Result<Vec<T>, Failed> { fn predict(&self, x: &'a X) -> Result<Vec<T>, Failed> {
@@ -307,7 +199,7 @@ impl<'a, T: Number + RealNumber, X: Array2<T>, Y: Array1<T>> PredictorBorrow<'a,
} }
} }
impl<'a, T: Number + RealNumber, X: Array2<T>, Y: Array1<T>> SVR<'a, T, X, Y> { impl<'a, T: Number + FloatNumber + PartialOrd, X: Array2<T>, Y: Array1<T>> SVR<'a, T, X, Y> {
/// Fits SVR to your data. /// Fits SVR to your data.
/// * `x` - _NxM_ matrix with _N_ observations and _M_ features in each observation. /// * `x` - _NxM_ matrix with _N_ observations and _M_ features in each observation.
/// * `y` - target values /// * `y` - target values
@@ -388,7 +280,9 @@ impl<'a, T: Number + RealNumber, X: Array2<T>, Y: Array1<T>> SVR<'a, T, X, Y> {
} }
} }
impl<'a, T: Number + RealNumber, X: Array2<T>, Y: Array1<T>> PartialEq for SVR<'a, T, X, Y> { impl<'a, T: Number + FloatNumber + PartialOrd, X: Array2<T>, Y: Array1<T>> PartialEq
for SVR<'a, 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()
|| self.w.as_ref().unwrap().len() != other.w.as_ref().unwrap().len() || self.w.as_ref().unwrap().len() != other.w.as_ref().unwrap().len()
@@ -414,7 +308,7 @@ impl<'a, T: Number + RealNumber, X: Array2<T>, Y: Array1<T>> PartialEq for SVR<'
} }
} }
impl<T: Number + RealNumber> SupportVector<T> { impl<T: Number + FloatNumber + PartialOrd> SupportVector<T> {
fn new(i: usize, x: Vec<f64>, y: T, eps: T, k: f64) -> SupportVector<T> { fn new(i: usize, x: Vec<f64>, y: T, eps: T, k: f64) -> SupportVector<T> {
SupportVector { SupportVector {
index: i, index: i,
@@ -426,7 +320,7 @@ impl<T: Number + RealNumber> SupportVector<T> {
} }
} }
impl<'a, T: Number + RealNumber> Optimizer<'a, T> { impl<'a, T: Number + FloatNumber + PartialOrd> Optimizer<'a, T> {
fn new<X: Array2<T>, Y: Array1<T>>( fn new<X: Array2<T>, Y: Array1<T>>(
x: &'a X, x: &'a X,
y: &'a Y, y: &'a Y,
@@ -468,12 +362,13 @@ impl<'a, T: Number + RealNumber> Optimizer<'a, T> {
gmaxindex: 0, gmaxindex: 0,
tau: T::from_f64(1e-12).unwrap(), tau: T::from_f64(1e-12).unwrap(),
sv: support_vectors, sv: support_vectors,
max_iterations: 49999,
} }
} }
fn find_min_max_gradient(&mut self) { fn find_min_max_gradient(&mut self) {
// self.gmin = <T as Bounded>::max_value()(); self.gmin = <T as Bounded>::max_value();
// self.gmax = <T as Bounded>::min_value(); self.gmax = <T as Bounded>::min_value();
for i in 0..self.sv.len() { for i in 0..self.sv.len() {
let v = &self.sv[i]; let v = &self.sv[i];
@@ -511,10 +406,13 @@ impl<'a, T: Number + RealNumber> Optimizer<'a, T> {
/// * hyperplane parameters: w and b (computed with T) /// * hyperplane parameters: w and b (computed with T)
fn smo(mut self) -> (Vec<Vec<f64>>, Vec<T>, T) { fn smo(mut self) -> (Vec<Vec<f64>>, Vec<T>, T) {
let cache: Cache<f64> = Cache::new(self.sv.len()); let cache: Cache<f64> = Cache::new(self.sv.len());
let mut n_iteration = 0usize;
self.find_min_max_gradient(); self.find_min_max_gradient();
while self.gmax - self.gmin > self.tol { while self.gmax - self.gmin > self.tol {
if n_iteration > self.max_iterations {
break;
}
let v1 = self.svmax; let v1 = self.svmax;
let i = self.gmaxindex; let i = self.gmaxindex;
let old_alpha_i = self.sv[v1].alpha[i]; let old_alpha_i = self.sv[v1].alpha[i];
@@ -659,6 +557,7 @@ impl<'a, T: Number + RealNumber> Optimizer<'a, T> {
} }
self.find_min_max_gradient(); self.find_min_max_gradient();
n_iteration += 1;
} }
let b = -(self.gmax + self.gmin) / T::two(); let b = -(self.gmax + self.gmin) / T::two();
@@ -694,11 +593,11 @@ impl<T: Clone> Cache<T> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
// use super::*; use super::*;
// use crate::linalg::basic::matrix::DenseMatrix; use crate::linalg::basic::matrix::DenseMatrix;
// use crate::metrics::mean_squared_error; use crate::metrics::mean_squared_error;
// #[cfg(feature = "serde")] #[cfg(feature = "serde")]
// use crate::svm::*; use crate::svm::Kernels;
// #[test] // #[test]
// fn search_parameters() { // fn search_parameters() {
@@ -719,78 +618,96 @@ mod tests {
// } // }
//TODO: had to disable this test as it runs for too long //TODO: had to disable this test as it runs for too long
// #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
// #[test] all(target_arch = "wasm32", not(target_os = "wasi")),
// fn svr_fit_predict() { wasm_bindgen_test::wasm_bindgen_test
// let x = DenseMatrix::from_2d_array(&[ )]
// &[234.289, 235.6, 159.0, 107.608, 1947., 60.323], #[test]
// &[259.426, 232.5, 145.6, 108.632, 1948., 61.122], fn svr_fit_predict() {
// &[258.054, 368.2, 161.6, 109.773, 1949., 60.171], let x = DenseMatrix::from_2d_array(&[
// &[284.599, 335.1, 165.0, 110.929, 1950., 61.187], &[234.289, 235.6, 159.0, 107.608, 1947., 60.323],
// &[328.975, 209.9, 309.9, 112.075, 1951., 63.221], &[259.426, 232.5, 145.6, 108.632, 1948., 61.122],
// &[346.999, 193.2, 359.4, 113.270, 1952., 63.639], &[258.054, 368.2, 161.6, 109.773, 1949., 60.171],
// &[365.385, 187.0, 354.7, 115.094, 1953., 64.989], &[284.599, 335.1, 165.0, 110.929, 1950., 61.187],
// &[363.112, 357.8, 335.0, 116.219, 1954., 63.761], &[328.975, 209.9, 309.9, 112.075, 1951., 63.221],
// &[397.469, 290.4, 304.8, 117.388, 1955., 66.019], &[346.999, 193.2, 359.4, 113.270, 1952., 63.639],
// &[419.180, 282.2, 285.7, 118.734, 1956., 67.857], &[365.385, 187.0, 354.7, 115.094, 1953., 64.989],
// &[442.769, 293.6, 279.8, 120.445, 1957., 68.169], &[363.112, 357.8, 335.0, 116.219, 1954., 63.761],
// &[444.546, 468.1, 263.7, 121.950, 1958., 66.513], &[397.469, 290.4, 304.8, 117.388, 1955., 66.019],
// &[482.704, 381.3, 255.2, 123.366, 1959., 68.655], &[419.180, 282.2, 285.7, 118.734, 1956., 67.857],
// &[502.601, 393.1, 251.4, 125.368, 1960., 69.564], &[442.769, 293.6, 279.8, 120.445, 1957., 68.169],
// &[518.173, 480.6, 257.2, 127.852, 1961., 69.331], &[444.546, 468.1, 263.7, 121.950, 1958., 66.513],
// &[554.894, 400.7, 282.7, 130.081, 1962., 70.551], &[482.704, 381.3, 255.2, 123.366, 1959., 68.655],
// ]); &[502.601, 393.1, 251.4, 125.368, 1960., 69.564],
&[518.173, 480.6, 257.2, 127.852, 1961., 69.331],
&[554.894, 400.7, 282.7, 130.081, 1962., 70.551],
]);
// let y: Vec<f64> = vec![ let y: Vec<f64> = 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, 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, 114.2, 115.7, 116.9,
// ]; ];
// let knl = Kernels::linear(); let knl = Kernels::linear();
// let y_hat = SVR::fit(&x, &y, &SVRParameters::default() let y_hat = SVR::fit(
// .with_eps(2.0) &x,
// .with_c(10.0) &y,
// .with_kernel(&knl) &SVRParameters::default()
// ) .with_eps(2.0)
// .and_then(|lr| lr.predict(&x)) .with_c(10.0)
// .unwrap(); .with_kernel(&knl),
)
.and_then(|lr| lr.predict(&x))
.unwrap();
// assert!(mean_squared_error(&y_hat, &y) < 2.5); let t = mean_squared_error(&y_hat, &y);
// } println!("{:?}", t);
assert!(t < 2.5);
}
// #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
// #[test] all(target_arch = "wasm32", not(target_os = "wasi")),
// #[cfg(feature = "serde")] wasm_bindgen_test::wasm_bindgen_test
// fn svr_serde() { )]
// let x = DenseMatrix::from_2d_array(&[ #[test]
// &[234.289, 235.6, 159.0, 107.608, 1947., 60.323], #[cfg(feature = "serde")]
// &[259.426, 232.5, 145.6, 108.632, 1948., 61.122], fn svr_serde() {
// &[258.054, 368.2, 161.6, 109.773, 1949., 60.171], let x = DenseMatrix::from_2d_array(&[
// &[284.599, 335.1, 165.0, 110.929, 1950., 61.187], &[234.289, 235.6, 159.0, 107.608, 1947., 60.323],
// &[328.975, 209.9, 309.9, 112.075, 1951., 63.221], &[259.426, 232.5, 145.6, 108.632, 1948., 61.122],
// &[346.999, 193.2, 359.4, 113.270, 1952., 63.639], &[258.054, 368.2, 161.6, 109.773, 1949., 60.171],
// &[365.385, 187.0, 354.7, 115.094, 1953., 64.989], &[284.599, 335.1, 165.0, 110.929, 1950., 61.187],
// &[363.112, 357.8, 335.0, 116.219, 1954., 63.761], &[328.975, 209.9, 309.9, 112.075, 1951., 63.221],
// &[397.469, 290.4, 304.8, 117.388, 1955., 66.019], &[346.999, 193.2, 359.4, 113.270, 1952., 63.639],
// &[419.180, 282.2, 285.7, 118.734, 1956., 67.857], &[365.385, 187.0, 354.7, 115.094, 1953., 64.989],
// &[442.769, 293.6, 279.8, 120.445, 1957., 68.169], &[363.112, 357.8, 335.0, 116.219, 1954., 63.761],
// &[444.546, 468.1, 263.7, 121.950, 1958., 66.513], &[397.469, 290.4, 304.8, 117.388, 1955., 66.019],
// &[482.704, 381.3, 255.2, 123.366, 1959., 68.655], &[419.180, 282.2, 285.7, 118.734, 1956., 67.857],
// &[502.601, 393.1, 251.4, 125.368, 1960., 69.564], &[442.769, 293.6, 279.8, 120.445, 1957., 68.169],
// &[518.173, 480.6, 257.2, 127.852, 1961., 69.331], &[444.546, 468.1, 263.7, 121.950, 1958., 66.513],
// &[554.894, 400.7, 282.7, 130.081, 1962., 70.551], &[482.704, 381.3, 255.2, 123.366, 1959., 68.655],
// ]); &[502.601, 393.1, 251.4, 125.368, 1960., 69.564],
&[518.173, 480.6, 257.2, 127.852, 1961., 69.331],
&[554.894, 400.7, 282.7, 130.081, 1962., 70.551],
]);
// let y: Vec<f64> = vec![ let y: Vec<f64> = 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, 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, 114.2, 115.7, 116.9,
// ]; ];
// let svr = SVR::fit(&x, &y, Default::default()).unwrap(); let knl = Kernels::rbf().with_gamma(0.7);
let params = SVRParameters::default().with_kernel(&knl);
let svr = SVR::fit(&x, &y, &params).unwrap();
let serialized = &serde_json::to_string(&svr).unwrap();
println!("{}", &serialized);
// let deserialized_svr: SVR<f64, DenseMatrix<f64>, LinearKernel> = // let deserialized_svr: SVR<f64, DenseMatrix<f64>, LinearKernel> =
// 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);
// } }
} }
+16 -4
View File
@@ -899,7 +899,10 @@ mod tests {
assert!(iter.next().is_none()); assert!(iter.next().is_none());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn gini_impurity() { fn gini_impurity() {
assert!( assert!(
@@ -915,7 +918,10 @@ mod tests {
); );
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn fit_predict_iris() { fn fit_predict_iris() {
let x: DenseMatrix<f64> = DenseMatrix::from_2d_array(&[ let x: DenseMatrix<f64> = DenseMatrix::from_2d_array(&[
@@ -968,7 +974,10 @@ mod tests {
); );
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn fit_predict_baloons() { fn fit_predict_baloons() {
let x: DenseMatrix<f64> = DenseMatrix::from_2d_array(&[ let x: DenseMatrix<f64> = DenseMatrix::from_2d_array(&[
@@ -1003,7 +1012,10 @@ mod tests {
); );
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
fn serde() { fn serde() {
+8 -2
View File
@@ -731,7 +731,10 @@ mod tests {
assert!(iter.next().is_none()); assert!(iter.next().is_none());
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
fn fit_longley() { fn fit_longley() {
let x = DenseMatrix::from_2d_array(&[ let x = DenseMatrix::from_2d_array(&[
@@ -808,7 +811,10 @@ mod tests {
} }
} }
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(
all(target_arch = "wasm32", not(target_os = "wasi")),
wasm_bindgen_test::wasm_bindgen_test
)]
#[test] #[test]
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
fn serde() { fn serde() {