diff --git a/src/composing/platt_scaling.rs b/src/composing/platt_scaling.rs index beca88513..63c413bb5 100644 --- a/src/composing/platt_scaling.rs +++ b/src/composing/platt_scaling.rs @@ -449,7 +449,7 @@ mod tests { y.len(), "The number of data points must match the number of output targets." ); - *y = self.reg_vals.clone(); + y.clone_from(&self.reg_vals); } fn default_target(&self, x: &Array2) -> Array1 { diff --git a/src/dataset/impl_dataset.rs b/src/dataset/impl_dataset.rs index dc225d29a..b8148793f 100644 --- a/src/dataset/impl_dataset.rs +++ b/src/dataset/impl_dataset.rs @@ -603,13 +603,13 @@ where )> { let targets = self.as_targets(); let fold_size = targets.len() / k; - let mut res = Vec::new(); // Generates all k folds of records and targets let mut records_chunks: Vec<_> = self.records.axis_chunks_iter(Axis(0), fold_size).collect(); let mut targets_chunks: Vec<_> = targets.axis_chunks_iter(Axis(0), fold_size).collect(); + let mut res = Vec::with_capacity(k); // For each iteration, take the first chunk for both records and targets as the validation set and // concatenate all the other chunks to create the training set. In the end swap the first chunk with the // one in the next index so that it is ready for the next iteration @@ -742,7 +742,7 @@ where let targets = self.ntargets(); let tshape = self.targets.raw_dim(); - let mut objs: Vec = Vec::new(); + let mut objs: Vec = Vec::with_capacity(k); { let records_sl = self.records.as_slice_mut().unwrap(); diff --git a/src/metrics_regression.rs b/src/metrics_regression.rs index b380c93f1..eed90900d 100644 --- a/src/metrics_regression.rs +++ b/src/metrics_regression.rs @@ -32,7 +32,7 @@ pub trait SingleTargetRegression>: fn mean_absolute_error(&self, compare_to: &T) -> Result { self.as_single_targets() .sub(&compare_to.as_single_targets()) - .mapv(|x| x.abs()) + .mapv_into(|x| x.abs()) .mean() .ok_or(Error::NotEnoughSamples) } @@ -41,7 +41,7 @@ pub trait SingleTargetRegression>: fn mean_squared_error(&self, compare_to: &T) -> Result { self.as_single_targets() .sub(&compare_to.as_single_targets()) - .mapv(|x| x * x) + .mapv_into(|x| x * x) .mean() .ok_or(Error::NotEnoughSamples) } @@ -58,7 +58,7 @@ pub trait SingleTargetRegression>: let mut abs_error = self .as_single_targets() .sub(&compare_to.as_single_targets()) - .mapv(|x| x.abs()) + .mapv_into(|x| x.abs()) .to_vec(); abs_error.sort_by(|a, b| a.partial_cmp(b).unwrap()); let mid = abs_error.len() / 2; @@ -74,8 +74,8 @@ pub trait SingleTargetRegression>: fn mean_absolute_percentage_error(&self, compare_to: &T) -> Result { self.as_single_targets() .sub(&compare_to.as_single_targets()) - .div(&self.as_single_targets()) - .mapv(|x| x.abs()) + .div(self.as_single_targets()) + .mapv_into(|x| x.abs()) .mean() .ok_or(Error::NotEnoughSamples) } @@ -95,7 +95,7 @@ pub trait SingleTargetRegression>: - self .as_single_targets() .sub(&single_target_compare_to) - .mapv(|x| x * x) + .mapv_into(|x| x * x) .sum() / (single_target_compare_to .mapv(|x| (x - mean) * (x - mean)) @@ -114,7 +114,7 @@ pub trait SingleTargetRegression>: let mean_error = diff.mean().ok_or(Error::NotEnoughSamples)?; Ok(F::one() - - (diff.mapv(|x| x * x).sum() - mean_error) + - (diff.mapv_into(|x| x * x).sum() - mean_error) / (single_target_compare_to .mapv(|x| (x - mean) * (x - mean)) .sum()