diff --git a/Cargo.toml b/Cargo.toml index 215e3ec..32c7744 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,8 @@ keywords = ["machine-learning", "statistical", "ai", "optimization", "linear-alg categories = ["science"] [dependencies] -smartcore = { version = "0.3", features = ["ndarray-bindings"] } +smartcore = { path="../smartcore", features = ["ndarray-bindings", "datasets"] } +# smartcore = { version = "0.3", features = ["ndarray-bindings"] } itertools = { version = "0.10.3"} ndarray = "0.15" criterion = { version = "0.4", default-features = false } @@ -47,3 +48,7 @@ harness = false [[bench]] name = "linear" harness = false + +[[bench]] +name = "svc" +harness = false diff --git a/benches/fastpair.rs b/benches/fastpair.rs index 27e8d7b..0abb3d3 100644 --- a/benches/fastpair.rs +++ b/benches/fastpair.rs @@ -3,17 +3,17 @@ use itertools::Itertools; // to run this bench you have to change the declaraion in mod.rs ---> pub mod fastpair; use smartcore::algorithm::neighbour::fastpair::FastPair; -use smartcore::linalg::basic::arrays::{Array2, Array}; +use smartcore::linalg::basic::arrays::{Array, Array2}; use smartcore::linalg::basic::matrix::DenseMatrix; use smartcore::metrics::distance::PairwiseDistance; use std::time::Duration; /// Utilities substitutes for benches -/// +/// /// /// return sum of squared distancs -/// +/// fn squared_distance(x: Vec, y: Vec) -> f64 { if x.len() != y.len() { panic!("Input vector sizes are different."); @@ -31,12 +31,10 @@ fn squared_distance(x: Vec, y: Vec) -> f64 { sum } - /// /// Brute force algorithm, used only for comparison and testing /// -pub fn closest_pair_brute(samples: &DenseMatrix, n_samples: usize -) -> PairwiseDistance { +pub fn closest_pair_brute(samples: &DenseMatrix, n_samples: usize) -> PairwiseDistance { let mut closest_pair = PairwiseDistance { node: 0, neighbour: Option::None, @@ -44,8 +42,16 @@ pub fn closest_pair_brute(samples: &DenseMatrix, n_samples: usize }; for pair in (0..n_samples).combinations(2) { let d = squared_distance( - samples.get_row(pair[0]).iterator(0).copied().collect::>(), - samples.get_row(pair[1]).iterator(0).copied().collect::>(), + samples + .get_row(pair[0]) + .iterator(0) + .copied() + .collect::>(), + samples + .get_row(pair[1]) + .iterator(0) + .copied() + .collect::>(), ); if d < closest_pair.distance.unwrap() { closest_pair.node = pair[0]; diff --git a/benches/linear.rs b/benches/linear.rs index d500c25..fa60bf7 100644 --- a/benches/linear.rs +++ b/benches/linear.rs @@ -22,7 +22,8 @@ pub fn linear_regression_fit_benchmark(c: &mut Criterion) { n_samples, |b, _| { b.iter(|| { - LinearRegression::fit(black_box(&x), black_box(&y), Default::default()).unwrap(); + LinearRegression::fit(black_box(&x), black_box(&y), Default::default()) + .unwrap(); }) }, ); @@ -31,8 +32,5 @@ pub fn linear_regression_fit_benchmark(c: &mut Criterion) { group.finish(); } -criterion_group!( - benches, - linear_regression_fit_benchmark, -); +criterion_group!(benches, linear_regression_fit_benchmark,); criterion_main!(benches); diff --git a/benches/naive_bayes.rs b/benches/naive_bayes.rs index 9a656c8..13fded2 100644 --- a/benches/naive_bayes.rs +++ b/benches/naive_bayes.rs @@ -55,7 +55,6 @@ pub fn gaussian_naive_matrix_datastructure(c: &mut Criterion) { GaussianNB::fit(black_box(&x), black_box(y), Default::default()).unwrap(); }) }); - } criterion_group!( benches, diff --git a/benches/svc.rs b/benches/svc.rs new file mode 100644 index 0000000..beeadab --- /dev/null +++ b/benches/svc.rs @@ -0,0 +1,38 @@ +use criterion::BenchmarkId; +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use smartcore::linalg::basic::arrays::Array2 as BaseArray2; +use smartcore::linalg::basic::matrix::DenseMatrix; +use smartcore::svm::svc::{MultiClassSVC, SVCParameters}; +use smartcore::svm::Kernels; + +pub fn multiclass_svc_fit_benchmark(c: &mut Criterion) { + let mut group = c.benchmark_group("MultiClassSVC::fit"); + + for n_samples in [100_usize, 1000_usize, 10000_usize].iter() { + for n_features in [10_usize, 100_usize, 1000_usize].iter() { + let x = DenseMatrix::::rand(*n_samples, *n_features); + let y: Vec = (0..*n_samples) + .map(|i| (i % *n_samples / 5_usize) as usize) + .collect::>(); + let parameters = SVCParameters::default() + .with_c(1.0) + .with_kernel(Kernels::rbf().with_gamma(0.7)); + group.bench_with_input( + BenchmarkId::from_parameter(format!( + "n_samples: {}, n_features: {}", + n_samples, n_features + )), + n_samples, + |b, _| { + b.iter(|| { + MultiClassSVC::fit(black_box(&x), black_box(&y), ¶meters).unwrap(); + }) + }, + ); + } + } + group.finish(); +} + +criterion_group!(benches, multiclass_svc_fit_benchmark,); +criterion_main!(benches);