Skip to content

Commit 239c004

Browse files
lorenzo-moriondo-agreenaEdmund Cape
and
Edmund Cape
authored
Patch to version 0.4.0 (#257)
* uncomment test * Add random test for logistic regression * linting * Bump version * Add test for logistic regression * linting * initial commit * final * final-clean * Bump to 0.4.0 * Fix linter * cleanup * Update CHANDELOG with breaking changes * Update CHANDELOG date * Add functional methods to DenseMatrix implementation * linting * add type declaration in test * Fix Wasm tests failing * linting * fix tests * linting * Add type annotations on BBDTree constructor * fix clippy * fix clippy * fix tests * bump version * run fmt. fix changelog --------- Co-authored-by: Edmund Cape <[email protected]>
1 parent 80a93c1 commit 239c004

Some content is hidden

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

45 files changed

+763
-410
lines changed

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

+1-1
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.2"
5+
version = "0.4.0"
66
authors = ["smartcore Developers"]
77
edition = "2021"
88
license = "Apache-2.0"

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,8 @@ mod tests {
442442
&[2.2, 1.2],
443443
&[1.8, 0.8],
444444
&[3.0, 5.0],
445-
]);
445+
])
446+
.unwrap();
446447

447448
let expected_labels = vec![1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 0];
448449

@@ -487,7 +488,8 @@ mod tests {
487488
&[4.9, 2.4, 3.3, 1.0],
488489
&[6.6, 2.9, 4.6, 1.3],
489490
&[5.2, 2.7, 3.9, 1.4],
490-
]);
491+
])
492+
.unwrap();
491493

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

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/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

src/ensemble/random_forest_classifier.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
//! &[4.9, 2.4, 3.3, 1.0],
3434
//! &[6.6, 2.9, 4.6, 1.3],
3535
//! &[5.2, 2.7, 3.9, 1.4],
36-
//! ]);
36+
//! ]).unwrap();
3737
//! let y = vec![
3838
//! 0, 0, 0, 0, 0, 0, 0, 0,
3939
//! 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
@@ -660,7 +660,8 @@ mod tests {
660660
&[4.9, 2.4, 3.3, 1.0],
661661
&[6.6, 2.9, 4.6, 1.3],
662662
&[5.2, 2.7, 3.9, 1.4],
663-
]);
663+
])
664+
.unwrap();
664665
let y = vec![0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
665666

666667
let classifier = RandomForestClassifier::fit(
@@ -733,7 +734,8 @@ mod tests {
733734
&[4.9, 2.4, 3.3, 1.0],
734735
&[6.6, 2.9, 4.6, 1.3],
735736
&[5.2, 2.7, 3.9, 1.4],
736-
]);
737+
])
738+
.unwrap();
737739
let y = vec![0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
738740

739741
let classifier = RandomForestClassifier::fit(
@@ -786,7 +788,8 @@ mod tests {
786788
&[4.9, 2.4, 3.3, 1.0],
787789
&[6.6, 2.9, 4.6, 1.3],
788790
&[5.2, 2.7, 3.9, 1.4],
789-
]);
791+
])
792+
.unwrap();
790793
let y = vec![0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
791794

792795
let forest = RandomForestClassifier::fit(&x, &y, Default::default()).unwrap();

src/ensemble/random_forest_regressor.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
//! &[502.601, 393.1, 251.4, 125.368, 1960., 69.564],
3030
//! &[518.173, 480.6, 257.2, 127.852, 1961., 69.331],
3131
//! &[554.894, 400.7, 282.7, 130.081, 1962., 70.551],
32-
//! ]);
32+
//! ]).unwrap();
3333
//! let y = vec![
3434
//! 83.0, 88.5, 88.2, 89.5, 96.2, 98.1, 99.0, 100.0, 101.2,
3535
//! 104.6, 108.4, 110.8, 112.6, 114.2, 115.7, 116.9
@@ -574,7 +574,8 @@ mod tests {
574574
&[502.601, 393.1, 251.4, 125.368, 1960., 69.564],
575575
&[518.173, 480.6, 257.2, 127.852, 1961., 69.331],
576576
&[554.894, 400.7, 282.7, 130.081, 1962., 70.551],
577-
]);
577+
])
578+
.unwrap();
578579
let y = vec![
579580
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,
580581
114.2, 115.7, 116.9,
@@ -648,7 +649,8 @@ mod tests {
648649
&[502.601, 393.1, 251.4, 125.368, 1960., 69.564],
649650
&[518.173, 480.6, 257.2, 127.852, 1961., 69.331],
650651
&[554.894, 400.7, 282.7, 130.081, 1962., 70.551],
651-
]);
652+
])
653+
.unwrap();
652654
let y = vec![
653655
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,
654656
114.2, 115.7, 116.9,
@@ -702,7 +704,8 @@ mod tests {
702704
&[502.601, 393.1, 251.4, 125.368, 1960., 69.564],
703705
&[518.173, 480.6, 257.2, 127.852, 1961., 69.331],
704706
&[554.894, 400.7, 282.7, 130.081, 1962., 70.551],
705-
]);
707+
])
708+
.unwrap();
706709
let y = vec![
707710
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,
708711
114.2, 115.7, 116.9,

0 commit comments

Comments
 (0)