Skip to content

Commit 470de49

Browse files
committed
clippy
1 parent f09d365 commit 470de49

2 files changed

Lines changed: 17 additions & 11 deletions

File tree

src/ensemble/random_forest_classifier.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -628,23 +628,31 @@ impl<TX: FloatNumber + PartialOrd, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY
628628
/// Each probability represents the average fraction of training samples of that class
629629
/// across all trees that reached the same leaf node for this input.
630630
fn predict_proba_for_row(&self, x: &X, row: usize) -> Vec<f64> {
631+
// improvement: unwrap делаем один раз
632+
let trees = self.trees.as_ref().unwrap();
631633

634+
// improvement: unwrap classes тоже один раз
632635
let k = self.classes.as_ref().unwrap().len();
633-
let mut probs = vec![0.0; k];
634636

635-
for tree in self.trees.as_ref().unwrap().iter() {
637+
let mut probs = vec![0.0; k];
636638

639+
for tree in trees {
637640
let tree_probs = tree.predict_proba_for_row_real(x, row);
638641

639-
for i in 0..k {
640-
probs[i] += tree_probs[i];
642+
// improvement: убран range loop
643+
// improvement: нет индексирования
644+
// improvement: zip гарантирует покомпонентное сложение
645+
for (p, tp) in probs.iter_mut().zip(tree_probs.iter()) {
646+
*p += *tp; // важно разыменование
641647
}
642648
}
643649

644-
let n_trees = self.trees.as_ref().unwrap().len();
650+
// improvement: unwrap уже не нужен
651+
let n_trees = trees.len() as f64;
645652

646-
for i in 0..k {
647-
probs[i] /= n_trees as f64;
653+
// improvement: убран needless_range_loop
654+
for p in &mut probs {
655+
*p /= n_trees;
648656
}
649657

650658
probs
@@ -663,7 +671,7 @@ impl<TX: FloatNumber + PartialOrd, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY
663671
/// # Arguments
664672
///
665673
/// * `x` - The input samples as a matrix where each row is a sample and each column
666-
/// is a feature.
674+
/// is a feature.
667675
///
668676
/// # Returns
669677
///

src/tree/decision_tree_classifier.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -963,9 +963,7 @@ impl<TX: Number + PartialOrd, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>>
963963
let total: usize = current.class_distribution.iter().sum();
964964
let mut probs = vec![0.0; self.num_classes];
965965
if total > 0 {
966-
for i in 0..self.num_classes {
967-
probs[i] = current.class_distribution[i] as f64 / total as f64;
968-
}
966+
for (p, count) in probs.iter_mut().zip(&current.class_distribution) { *p = *count as f64 / total as f64; }
969967
}
970968
return probs;
971969
}

0 commit comments

Comments
 (0)