Skip to content

Commit cfc953b

Browse files
Merge branch 'development' into prdct-prb
2 parents 5bf7102 + 239c004 commit cfc953b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1047
-566
lines changed

.github/CONTRIBUTING.md

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ $ rust-code-analysis-cli -p src/algorithm/neighbour/fastpair.rs --ls 22 --le 213
3737
```
3838
* find more information about what happens in your binary with [`twiggy`](https://rustwasm.github.io/twiggy/install.html). This need a compiled binary so create a brief `main {}` function using `smartcore` and then point `twiggy` to that file.
3939

40+
* Please take a look to the output of a profiler to spot most evident performance problems, see [this guide about using a profiler](http://www.codeofview.com/fix-rs/2017/01/24/how-to-optimize-rust-programs-on-linux/).
41+
4042
## Issue Report Process
4143

4244
1. Go to the project's issues.

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.4.0] - 2023-04-05
8+
9+
## Added
10+
- WARNING: Breaking changes!
11+
- `DenseMatrix` constructor now returns `Result` to avoid user instantiating inconsistent rows/cols count. Their return values need to be unwrapped with `unwrap()`, see tests
12+
713
## [0.3.0] - 2022-11-09
814

915
## Added

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "smartcore"
33
description = "Machine Learning in Rust."
44
homepage = "https://smartcorelib.org"
5-
version = "0.3.1"
5+
version = "0.4.0"
66
authors = ["smartcore Developers"]
77
edition = "2021"
88
license = "Apache-2.0"
@@ -48,7 +48,7 @@ getrandom = { version = "0.2.8", optional = true }
4848
wasm-bindgen-test = "0.3"
4949

5050
[dev-dependencies]
51-
itertools = "0.10.5"
51+
itertools = "0.12.0"
5252
serde_json = "1.0"
5353
bincode = "1.3.1"
5454

src/algorithm/neighbour/bbd_tree.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ impl BBDTreeNode {
4040

4141
impl BBDTree {
4242
pub fn new<T: Number, M: Array2<T>>(data: &M) -> BBDTree {
43-
let nodes = Vec::new();
43+
let nodes: Vec<BBDTreeNode> = Vec::new();
4444

4545
let (n, _) = data.shape();
4646

47-
let index = (0..n).collect::<Vec<_>>();
47+
let index = (0..n).collect::<Vec<usize>>();
4848

4949
let mut tree = BBDTree {
5050
nodes,
@@ -343,7 +343,8 @@ mod tests {
343343
&[4.9, 2.4, 3.3, 1.0],
344344
&[6.6, 2.9, 4.6, 1.3],
345345
&[5.2, 2.7, 3.9, 1.4],
346-
]);
346+
])
347+
.unwrap();
347348

348349
let tree = BBDTree::new(&data);
349350

src/algorithm/neighbour/fastpair.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/// &[4.6, 3.1, 1.5, 0.2],
1818
/// &[5.0, 3.6, 1.4, 0.2],
1919
/// &[5.4, 3.9, 1.7, 0.4],
20-
/// ]);
20+
/// ]).unwrap();
2121
/// let fastpair = FastPair::new(&x);
2222
/// let closest_pair: PairwiseDistance<f64> = fastpair.unwrap().closest_pair();
2323
/// ```
@@ -271,7 +271,7 @@ mod tests_fastpair {
271271
fn dataset_has_at_least_three_points() {
272272
// Create a dataset which consists of only two points:
273273
// A(0.0, 0.0) and B(1.0, 1.0).
274-
let dataset = DenseMatrix::<f64>::from_2d_array(&[&[0.0, 0.0], &[1.0, 1.0]]);
274+
let dataset = DenseMatrix::<f64>::from_2d_array(&[&[0.0, 0.0], &[1.0, 1.0]]).unwrap();
275275

276276
// We expect an error when we run `FastPair` on this dataset,
277277
// becuase `FastPair` currently only works on a minimum of 3
@@ -288,7 +288,7 @@ mod tests_fastpair {
288288

289289
#[test]
290290
fn one_dimensional_dataset_minimal() {
291-
let dataset = DenseMatrix::<f64>::from_2d_array(&[&[0.0], &[2.0], &[9.0]]);
291+
let dataset = DenseMatrix::<f64>::from_2d_array(&[&[0.0], &[2.0], &[9.0]]).unwrap();
292292

293293
let result = FastPair::new(&dataset);
294294
assert!(result.is_ok());
@@ -308,7 +308,8 @@ mod tests_fastpair {
308308

309309
#[test]
310310
fn one_dimensional_dataset_2() {
311-
let dataset = DenseMatrix::<f64>::from_2d_array(&[&[27.0], &[0.0], &[9.0], &[2.0]]);
311+
let dataset =
312+
DenseMatrix::<f64>::from_2d_array(&[&[27.0], &[0.0], &[9.0], &[2.0]]).unwrap();
312313

313314
let result = FastPair::new(&dataset);
314315
assert!(result.is_ok());
@@ -343,7 +344,8 @@ mod tests_fastpair {
343344
&[6.9, 3.1, 4.9, 1.5],
344345
&[5.5, 2.3, 4.0, 1.3],
345346
&[6.5, 2.8, 4.6, 1.5],
346-
]);
347+
])
348+
.unwrap();
347349
let fastpair = FastPair::new(&x);
348350
assert!(fastpair.is_ok());
349351

@@ -516,7 +518,8 @@ mod tests_fastpair {
516518
&[6.9, 3.1, 4.9, 1.5],
517519
&[5.5, 2.3, 4.0, 1.3],
518520
&[6.5, 2.8, 4.6, 1.5],
519-
]);
521+
])
522+
.unwrap();
520523
// compute
521524
let fastpair = FastPair::new(&x);
522525
assert!(fastpair.is_ok());
@@ -564,7 +567,8 @@ mod tests_fastpair {
564567
&[6.9, 3.1, 4.9, 1.5],
565568
&[5.5, 2.3, 4.0, 1.3],
566569
&[6.5, 2.8, 4.6, 1.5],
567-
]);
570+
])
571+
.unwrap();
568572
// compute
569573
let fastpair = FastPair::new(&x);
570574
assert!(fastpair.is_ok());

src/cluster/dbscan.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,7 @@ impl<TX: Number, TY: Number, X: Array2<TX>, Y: Array1<TY>, D: Distance<Vec<TX>>>
315315
}
316316
}
317317

318-
while !neighbors.is_empty() {
319-
let neighbor = neighbors.pop().unwrap();
318+
while let Some(neighbor) = neighbors.pop() {
320319
let index = neighbor.0;
321320

322321
if y[index] == outlier {
@@ -443,7 +442,8 @@ mod tests {
443442
&[2.2, 1.2],
444443
&[1.8, 0.8],
445444
&[3.0, 5.0],
446-
]);
445+
])
446+
.unwrap();
447447

448448
let expected_labels = vec![1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 0];
449449

@@ -488,7 +488,8 @@ mod tests {
488488
&[4.9, 2.4, 3.3, 1.0],
489489
&[6.6, 2.9, 4.6, 1.3],
490490
&[5.2, 2.7, 3.9, 1.4],
491-
]);
491+
])
492+
.unwrap();
492493

493494
let dbscan = DBSCAN::fit(&x, Default::default()).unwrap();
494495

src/cluster/kmeans.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
//! &[4.9, 2.4, 3.3, 1.0],
4242
//! &[6.6, 2.9, 4.6, 1.3],
4343
//! &[5.2, 2.7, 3.9, 1.4],
44-
//! ]);
44+
//! ]).unwrap();
4545
//!
4646
//! let kmeans = KMeans::fit(&x, KMeansParameters::default().with_k(2)).unwrap(); // Fit to data, 2 clusters
4747
//! let y_hat: Vec<u8> = kmeans.predict(&x).unwrap(); // use the same points for prediction
@@ -249,7 +249,7 @@ impl<TX: Number, TY: Number, X: Array2<TX>, Y: Array1<TY>> Predictor<X, Y>
249249

250250
impl<TX: Number, TY: Number, X: Array2<TX>, Y: Array1<TY>> KMeans<TX, TY, X, Y> {
251251
/// Fit algorithm to _NxM_ matrix where _N_ is number of samples and _M_ is number of features.
252-
/// * `data` - training instances to cluster
252+
/// * `data` - training instances to cluster
253253
/// * `parameters` - cluster parameters
254254
pub fn fit(data: &X, parameters: KMeansParameters) -> Result<KMeans<TX, TY, X, Y>, Failed> {
255255
let bbd = BBDTree::new(data);
@@ -424,7 +424,7 @@ mod tests {
424424
)]
425425
#[test]
426426
fn invalid_k() {
427-
let x = DenseMatrix::from_2d_array(&[&[1, 2, 3], &[4, 5, 6]]);
427+
let x = DenseMatrix::from_2d_array(&[&[1, 2, 3], &[4, 5, 6]]).unwrap();
428428

429429
assert!(KMeans::<i32, i32, DenseMatrix<i32>, Vec<i32>>::fit(
430430
&x,
@@ -492,7 +492,8 @@ mod tests {
492492
&[4.9, 2.4, 3.3, 1.0],
493493
&[6.6, 2.9, 4.6, 1.3],
494494
&[5.2, 2.7, 3.9, 1.4],
495-
]);
495+
])
496+
.unwrap();
496497

497498
let kmeans = KMeans::fit(&x, Default::default()).unwrap();
498499

@@ -531,7 +532,8 @@ mod tests {
531532
&[4.9, 2.4, 3.3, 1.0],
532533
&[6.6, 2.9, 4.6, 1.3],
533534
&[5.2, 2.7, 3.9, 1.4],
534-
]);
535+
])
536+
.unwrap();
535537

536538
let kmeans: KMeans<f32, f32, DenseMatrix<f32>, Vec<f32>> =
537539
KMeans::fit(&x, Default::default()).unwrap();

src/dataset/diabetes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn load_dataset() -> Dataset<f32, u32> {
4040
target: y,
4141
num_samples,
4242
num_features,
43-
feature_names: vec![
43+
feature_names: [
4444
"Age", "Sex", "BMI", "BP", "S1", "S2", "S3", "S4", "S5", "S6",
4545
]
4646
.iter()

src/dataset/digits.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,14 @@ pub fn load_dataset() -> Dataset<f32, f32> {
2525
target: y,
2626
num_samples,
2727
num_features,
28-
feature_names: vec![
29-
"sepal length (cm)",
28+
feature_names: ["sepal length (cm)",
3029
"sepal width (cm)",
3130
"petal length (cm)",
32-
"petal width (cm)",
33-
]
31+
"petal width (cm)"]
3432
.iter()
3533
.map(|s| s.to_string())
3634
.collect(),
37-
target_names: vec!["setosa", "versicolor", "virginica"]
35+
target_names: ["setosa", "versicolor", "virginica"]
3836
.iter()
3937
.map(|s| s.to_string())
4038
.collect(),

src/dataset/iris.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub fn load_dataset() -> Dataset<f32, u32> {
3636
target: y,
3737
num_samples,
3838
num_features,
39-
feature_names: vec![
39+
feature_names: [
4040
"sepal length (cm)",
4141
"sepal width (cm)",
4242
"petal length (cm)",
@@ -45,7 +45,7 @@ pub fn load_dataset() -> Dataset<f32, u32> {
4545
.iter()
4646
.map(|s| s.to_string())
4747
.collect(),
48-
target_names: vec!["setosa", "versicolor", "virginica"]
48+
target_names: ["setosa", "versicolor", "virginica"]
4949
.iter()
5050
.map(|s| s.to_string())
5151
.collect(),

src/decomposition/pca.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
//! &[4.9, 2.4, 3.3, 1.0],
3636
//! &[6.6, 2.9, 4.6, 1.3],
3737
//! &[5.2, 2.7, 3.9, 1.4],
38-
//! ]);
38+
//! ]).unwrap();
3939
//!
4040
//! let pca = PCA::fit(&iris, PCAParameters::default().with_n_components(2)).unwrap(); // Reduce number of features to 2
4141
//!
@@ -443,6 +443,7 @@ mod tests {
443443
&[2.6, 53.0, 66.0, 10.8],
444444
&[6.8, 161.0, 60.0, 15.6],
445445
])
446+
.unwrap()
446447
}
447448
#[cfg_attr(
448449
all(target_arch = "wasm32", not(target_os = "wasi")),
@@ -457,7 +458,8 @@ mod tests {
457458
&[0.9952, 0.0588],
458459
&[0.0463, 0.9769],
459460
&[0.0752, 0.2007],
460-
]);
461+
])
462+
.unwrap();
461463

462464
let pca = PCA::fit(&us_arrests, Default::default()).unwrap();
463465

@@ -500,7 +502,8 @@ mod tests {
500502
-0.974080592182491,
501503
0.0723250196376097,
502504
],
503-
]);
505+
])
506+
.unwrap();
504507

505508
let expected_projection = DenseMatrix::from_2d_array(&[
506509
&[-64.8022, -11.448, 2.4949, -2.4079],
@@ -553,7 +556,8 @@ mod tests {
553556
&[91.5446, -22.9529, 0.402, -0.7369],
554557
&[118.1763, 5.5076, 2.7113, -0.205],
555558
&[10.4345, -5.9245, 3.7944, 0.5179],
556-
]);
559+
])
560+
.unwrap();
557561

558562
let expected_eigenvalues: Vec<f64> = vec![
559563
343544.6277001563,
@@ -616,7 +620,8 @@ mod tests {
616620
-0.0881962972508558,
617621
-0.0096011588898465,
618622
],
619-
]);
623+
])
624+
.unwrap();
620625

621626
let expected_projection = DenseMatrix::from_2d_array(&[
622627
&[0.9856, -1.1334, 0.4443, -0.1563],
@@ -669,7 +674,8 @@ mod tests {
669674
&[-2.1086, -1.4248, -0.1048, -0.1319],
670675
&[-2.0797, 0.6113, 0.1389, -0.1841],
671676
&[-0.6294, -0.321, 0.2407, 0.1667],
672-
]);
677+
])
678+
.unwrap();
673679

674680
let expected_eigenvalues: Vec<f64> = vec![
675681
2.480241579149493,
@@ -732,7 +738,7 @@ mod tests {
732738
// &[4.9, 2.4, 3.3, 1.0],
733739
// &[6.6, 2.9, 4.6, 1.3],
734740
// &[5.2, 2.7, 3.9, 1.4],
735-
// ]);
741+
// ]).unwrap();
736742

737743
// let pca = PCA::fit(&iris, Default::default()).unwrap();
738744

src/decomposition/svd.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
//! &[4.9, 2.4, 3.3, 1.0],
3333
//! &[6.6, 2.9, 4.6, 1.3],
3434
//! &[5.2, 2.7, 3.9, 1.4],
35-
//! ]);
35+
//! ]).unwrap();
3636
//!
3737
//! let svd = SVD::fit(&iris, SVDParameters::default().
3838
//! with_n_components(2)).unwrap(); // Reduce number of features to 2
@@ -292,15 +292,17 @@ mod tests {
292292
&[5.7, 81.0, 39.0, 9.3],
293293
&[2.6, 53.0, 66.0, 10.8],
294294
&[6.8, 161.0, 60.0, 15.6],
295-
]);
295+
])
296+
.unwrap();
296297

297298
let expected = DenseMatrix::from_2d_array(&[
298299
&[243.54655757, -18.76673788],
299300
&[268.36802004, -33.79304302],
300301
&[305.93972467, -15.39087376],
301302
&[197.28420365, -11.66808306],
302303
&[293.43187394, 1.91163633],
303-
]);
304+
])
305+
.unwrap();
304306
let svd = SVD::fit(&x, Default::default()).unwrap();
305307

306308
let x_transformed = svd.transform(&x).unwrap();
@@ -341,7 +343,7 @@ mod tests {
341343
// &[4.9, 2.4, 3.3, 1.0],
342344
// &[6.6, 2.9, 4.6, 1.3],
343345
// &[5.2, 2.7, 3.9, 1.4],
344-
// ]);
346+
// ]).unwrap();
345347

346348
// let svd = SVD::fit(&iris, Default::default()).unwrap();
347349

0 commit comments

Comments
 (0)