From d37230053eaa589209eb767bc03a9b3f89ca5ce1 Mon Sep 17 00:00:00 2001 From: Luis Moreno Date: Mon, 20 Nov 2023 21:45:40 -0400 Subject: [PATCH 1/2] chore: update clippy lints --- src/cluster/dbscan.rs | 3 +-- src/linalg/basic/arrays.rs | 6 ++---- src/linalg/basic/matrix.rs | 2 +- src/linalg/basic/vector.rs | 2 +- src/linear/bg_solver.rs | 2 +- src/linear/logistic_regression.rs | 6 +----- src/neighbors/knn_regressor.rs | 4 ++-- src/preprocessing/categorical.rs | 2 +- src/svm/mod.rs | 2 +- src/tree/decision_tree_regressor.rs | 4 ++-- 10 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/cluster/dbscan.rs b/src/cluster/dbscan.rs index 0d84a613..584cdc31 100644 --- a/src/cluster/dbscan.rs +++ b/src/cluster/dbscan.rs @@ -315,8 +315,7 @@ impl, Y: Array1, D: Distance>> } } - while !neighbors.is_empty() { - let neighbor = neighbors.pop().unwrap(); + while let Some(neighbor) = neighbors.pop() { let index = neighbor.0; if y[index] == outlier { diff --git a/src/linalg/basic/arrays.rs b/src/linalg/basic/arrays.rs index 7d0c77a6..0df1bf75 100644 --- a/src/linalg/basic/arrays.rs +++ b/src/linalg/basic/arrays.rs @@ -188,8 +188,7 @@ pub trait ArrayView1: Array { _ => max, } }; - self.iterator(0) - .fold(T::min_value(), |max, x| max_f(max, x)) + self.iterator(0).fold(T::min_value(), max_f) } /// return min value from the view fn min(&self) -> T @@ -202,8 +201,7 @@ pub trait ArrayView1: Array { _ => min, } }; - self.iterator(0) - .fold(T::max_value(), |max, x| min_f(max, x)) + self.iterator(0).fold(T::max_value(), min_f) } /// return the position of the max value of the view fn argmax(&self) -> usize diff --git a/src/linalg/basic/matrix.rs b/src/linalg/basic/matrix.rs index e108cea4..6b39b55c 100644 --- a/src/linalg/basic/matrix.rs +++ b/src/linalg/basic/matrix.rs @@ -650,7 +650,7 @@ mod tests { #[test] fn test_from_iterator() { - let data = vec![1, 2, 3, 4, 5, 6]; + let data = [1, 2, 3, 4, 5, 6]; let m = DenseMatrix::from_iterator(data.iter(), 2, 3, 0); diff --git a/src/linalg/basic/vector.rs b/src/linalg/basic/vector.rs index 5d79ab22..08ea620b 100644 --- a/src/linalg/basic/vector.rs +++ b/src/linalg/basic/vector.rs @@ -211,7 +211,7 @@ mod tests { #[test] fn test_len() { - let x = vec![1, 2, 3]; + let x = [1, 2, 3]; assert_eq!(3, x.len()); } diff --git a/src/linear/bg_solver.rs b/src/linear/bg_solver.rs index d1ad29f2..5665c763 100644 --- a/src/linear/bg_solver.rs +++ b/src/linear/bg_solver.rs @@ -160,7 +160,7 @@ mod tests { fn bg_solver() { let a = DenseMatrix::from_2d_array(&[&[25., 15., -5.], &[15., 18., 0.], &[-5., 0., 11.]]); let b = vec![40., 51., 28.]; - let expected = vec![1.0, 2.0, 3.0]; + let expected = [1.0, 2.0, 3.0]; let mut x = Vec::zeros(3); diff --git a/src/linear/logistic_regression.rs b/src/linear/logistic_regression.rs index 4a4041bc..0defd0fa 100644 --- a/src/linear/logistic_regression.rs +++ b/src/linear/logistic_regression.rs @@ -890,11 +890,7 @@ mod tests { let y_hat = lr.predict(&x).unwrap(); - let error: i32 = y - .into_iter() - .zip(y_hat.into_iter()) - .map(|(a, b)| (a - b).abs()) - .sum(); + let error: i32 = y.into_iter().zip(y_hat).map(|(a, b)| (a - b).abs()).sum(); assert!(error <= 1); diff --git a/src/neighbors/knn_regressor.rs b/src/neighbors/knn_regressor.rs index 914f810e..5798700d 100644 --- a/src/neighbors/knn_regressor.rs +++ b/src/neighbors/knn_regressor.rs @@ -297,7 +297,7 @@ mod tests { let x = DenseMatrix::from_2d_array(&[&[1., 2.], &[3., 4.], &[5., 6.], &[7., 8.], &[9., 10.]]); let y: Vec = vec![1., 2., 3., 4., 5.]; - let y_exp = vec![1., 2., 3., 4., 5.]; + let y_exp = [1., 2., 3., 4., 5.]; let knn = KNNRegressor::fit( &x, &y, @@ -324,7 +324,7 @@ mod tests { let x = DenseMatrix::from_2d_array(&[&[1., 2.], &[3., 4.], &[5., 6.], &[7., 8.], &[9., 10.]]); let y: Vec = vec![1., 2., 3., 4., 5.]; - let y_exp = vec![2., 2., 3., 4., 4.]; + let y_exp = [2., 2., 3., 4., 4.]; let knn = KNNRegressor::fit(&x, &y, Default::default()).unwrap(); let y_hat = knn.predict(&x).unwrap(); assert_eq!(5, Vec::len(&y_hat)); diff --git a/src/preprocessing/categorical.rs b/src/preprocessing/categorical.rs index 933d7c2b..dfa7d239 100644 --- a/src/preprocessing/categorical.rs +++ b/src/preprocessing/categorical.rs @@ -277,7 +277,7 @@ mod tests { )] #[test] fn hash_encode_f64_series() { - let series = vec![3.0, 1.0, 2.0, 1.0]; + let series = [3.0, 1.0, 2.0, 1.0]; let hashable_series: Vec = series.iter().map(|v| v.to_category()).collect(); let enc = CategoryMapper::from_positional_category_vec(hashable_series); diff --git a/src/svm/mod.rs b/src/svm/mod.rs index b2bd79cb..0792fdb8 100644 --- a/src/svm/mod.rs +++ b/src/svm/mod.rs @@ -56,7 +56,7 @@ pub struct Kernels; impl Kernels { /// Return a default linear pub fn linear() -> LinearKernel { - LinearKernel::default() + LinearKernel } /// Return a default RBF pub fn rbf() -> RBFKernel { diff --git a/src/tree/decision_tree_regressor.rs b/src/tree/decision_tree_regressor.rs index d21c7490..21832ba4 100644 --- a/src/tree/decision_tree_regressor.rs +++ b/src/tree/decision_tree_regressor.rs @@ -767,7 +767,7 @@ mod tests { assert!((y_hat[i] - y[i]).abs() < 0.1); } - let expected_y = vec![ + let expected_y = [ 87.3, 87.3, 87.3, 87.3, 98.9, 98.9, 98.9, 98.9, 98.9, 107.9, 107.9, 107.9, 114.85, 114.85, 114.85, 114.85, ]; @@ -788,7 +788,7 @@ mod tests { assert!((y_hat[i] - expected_y[i]).abs() < 0.1); } - let expected_y = vec![ + let expected_y = [ 83.0, 88.35, 88.35, 89.5, 97.15, 97.15, 99.5, 99.5, 101.2, 104.6, 109.6, 109.6, 113.4, 113.4, 116.30, 116.30, ]; From 901a7b3853068532afdec345f0001aa3d979b393 Mon Sep 17 00:00:00 2001 From: Luis Moreno Date: Mon, 20 Nov 2023 21:50:07 -0400 Subject: [PATCH 2/2] clippy: dataset --- src/dataset/diabetes.rs | 2 +- src/dataset/digits.rs | 8 +++----- src/dataset/iris.rs | 4 ++-- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/dataset/diabetes.rs b/src/dataset/diabetes.rs index faf169eb..a95b5116 100644 --- a/src/dataset/diabetes.rs +++ b/src/dataset/diabetes.rs @@ -40,7 +40,7 @@ pub fn load_dataset() -> Dataset { target: y, num_samples, num_features, - feature_names: vec![ + feature_names: [ "Age", "Sex", "BMI", "BP", "S1", "S2", "S3", "S4", "S5", "S6", ] .iter() diff --git a/src/dataset/digits.rs b/src/dataset/digits.rs index b3556e53..c32648cd 100644 --- a/src/dataset/digits.rs +++ b/src/dataset/digits.rs @@ -25,16 +25,14 @@ pub fn load_dataset() -> Dataset { target: y, num_samples, num_features, - feature_names: vec![ - "sepal length (cm)", + feature_names: ["sepal length (cm)", "sepal width (cm)", "petal length (cm)", - "petal width (cm)", - ] + "petal width (cm)"] .iter() .map(|s| s.to_string()) .collect(), - target_names: vec!["setosa", "versicolor", "virginica"] + target_names: ["setosa", "versicolor", "virginica"] .iter() .map(|s| s.to_string()) .collect(), diff --git a/src/dataset/iris.rs b/src/dataset/iris.rs index fe60241a..75c58acc 100644 --- a/src/dataset/iris.rs +++ b/src/dataset/iris.rs @@ -36,7 +36,7 @@ pub fn load_dataset() -> Dataset { target: y, num_samples, num_features, - feature_names: vec![ + feature_names: [ "sepal length (cm)", "sepal width (cm)", "petal length (cm)", @@ -45,7 +45,7 @@ pub fn load_dataset() -> Dataset { .iter() .map(|s| s.to_string()) .collect(), - target_names: vec!["setosa", "versicolor", "virginica"] + target_names: ["setosa", "versicolor", "virginica"] .iter() .map(|s| s.to_string()) .collect(),