Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -47,3 +48,7 @@ harness = false
[[bench]]
name = "linear"
harness = false

[[bench]]
name = "svc"
harness = false
22 changes: 14 additions & 8 deletions benches/fastpair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64>, y: Vec<f64>) -> f64 {
if x.len() != y.len() {
panic!("Input vector sizes are different.");
Expand All @@ -31,21 +31,27 @@ fn squared_distance(x: Vec<f64>, y: Vec<f64>) -> f64 {
sum
}


///
/// Brute force algorithm, used only for comparison and testing
///
pub fn closest_pair_brute(samples: &DenseMatrix<f64>, n_samples: usize
) -> PairwiseDistance<f64> {
pub fn closest_pair_brute(samples: &DenseMatrix<f64>, n_samples: usize) -> PairwiseDistance<f64> {
let mut closest_pair = PairwiseDistance {
node: 0,
neighbour: Option::None,
distance: Some(f64::MAX),
};
for pair in (0..n_samples).combinations(2) {
let d = squared_distance(
samples.get_row(pair[0]).iterator(0).copied().collect::<Vec<f64>>(),
samples.get_row(pair[1]).iterator(0).copied().collect::<Vec<f64>>(),
samples
.get_row(pair[0])
.iterator(0)
.copied()
.collect::<Vec<f64>>(),
samples
.get_row(pair[1])
.iterator(0)
.copied()
.collect::<Vec<f64>>(),
);
if d < closest_pair.distance.unwrap() {
closest_pair.node = pair[0];
Expand Down
8 changes: 3 additions & 5 deletions benches/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})
},
);
Expand All @@ -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);
1 change: 0 additions & 1 deletion benches/naive_bayes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
38 changes: 38 additions & 0 deletions benches/svc.rs
Original file line number Diff line number Diff line change
@@ -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::<f64>::rand(*n_samples, *n_features);
let y: Vec<usize> = (0..*n_samples)
.map(|i| (i % *n_samples / 5_usize) as usize)
.collect::<Vec<usize>>();
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), &parameters).unwrap();
})
},
);
}
}
group.finish();
}

criterion_group!(benches, multiclass_svc_fit_benchmark,);
criterion_main!(benches);