sklears-ensemble 0.2.0

Ensemble methods for sklears: Random Forest, Gradient Boosting, AdaBoost
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//! Decision tree implementations for AdaBoost
//!
//! Provides weighted decision-stump classifiers and regressors used as base
//! learners inside AdaBoost.  For AdaBoost, the default depth is 1 (a single
//! decision stump), but arbitrary `max_depth` is also supported.

use super::types::*;
use scirs2_core::ndarray::{Array1, Array2};
use sklears_core::{
    error::Result,
    traits::{Fit, Trained, Untrained},
    types::{Float, Int},
};
use std::marker::PhantomData;

// ---------------------------------------------------------------------------
// Impurity helpers
// ---------------------------------------------------------------------------

/// Gini impurity for a slice of integer class labels.
fn gini_impurity(labels: &[Int]) -> Float {
    let n = labels.len();
    if n == 0 {
        return 0.0;
    }
    let mut counts: std::collections::HashMap<Int, usize> = std::collections::HashMap::new();
    for &l in labels {
        *counts.entry(l).or_insert(0) += 1;
    }
    let n_f = n as Float;
    1.0 - counts
        .values()
        .map(|&c| (c as Float / n_f).powi(2))
        .sum::<Float>()
}

/// Entropy impurity for a slice of integer class labels.
fn entropy_impurity(labels: &[Int]) -> Float {
    let n = labels.len();
    if n == 0 {
        return 0.0;
    }
    let mut counts: std::collections::HashMap<Int, usize> = std::collections::HashMap::new();
    for &l in labels {
        *counts.entry(l).or_insert(0) += 1;
    }
    let n_f = n as Float;
    -counts
        .values()
        .map(|&c| {
            let p = c as Float / n_f;
            if p > 0.0 {
                p * p.ln()
            } else {
                0.0
            }
        })
        .sum::<Float>()
}

/// Variance (MSE) for a slice of float targets — used in regression splits.
fn variance(targets: &[Float]) -> Float {
    let n = targets.len();
    if n == 0 {
        return 0.0;
    }
    let mean = targets.iter().sum::<Float>() / n as Float;
    targets.iter().map(|&v| (v - mean).powi(2)).sum::<Float>() / n as Float
}

/// Most frequent class label (ties broken by smallest label).
fn majority_class(labels: &[Int]) -> Int {
    let mut counts: std::collections::HashMap<Int, usize> = std::collections::HashMap::new();
    for &l in labels {
        *counts.entry(l).or_insert(0) += 1;
    }
    counts
        .into_iter()
        .max_by_key(|&(label, count)| (count, -(label as i64)))
        .map(|(label, _)| label)
        .unwrap_or(0)
}

/// Mean of a float slice.
fn mean_value(targets: &[Float]) -> Float {
    if targets.is_empty() {
        return 0.0;
    }
    targets.iter().sum::<Float>() / targets.len() as Float
}

// ---------------------------------------------------------------------------
// Classification tree builder
// ---------------------------------------------------------------------------

/// Bundled build parameters to avoid too-many-arguments clippy lint.
struct ClassifierBuildParams {
    criterion: SplitCriterion,
    max_depth: usize,
    min_samples_split: usize,
    min_samples_leaf: usize,
}

fn build_classifier(
    x: &Array2<Float>,
    y: &[Int],
    indices: &[usize],
    params: &ClassifierBuildParams,
    depth: usize,
) -> ClassifierNode {
    let labels: Vec<Int> = indices.iter().map(|&i| y[i]).collect();
    let n = indices.len();

    // Base cases
    let all_same = labels.windows(2).all(|w| w[0] == w[1]);
    if n < params.min_samples_split || depth >= params.max_depth || all_same {
        return ClassifierNode::Leaf(majority_class(&labels));
    }

    let impurity_fn: fn(&[Int]) -> Float = match params.criterion {
        SplitCriterion::Gini => gini_impurity,
        SplitCriterion::Entropy => entropy_impurity,
    };
    let parent_impurity = impurity_fn(&labels);

    let n_features = x.ncols();
    let mut best_gain = Float::NEG_INFINITY;
    let mut best_feature = 0;
    let mut best_threshold = 0.0_f64;

    for feat in 0..n_features {
        let mut vals: Vec<(Float, Int)> = indices.iter().map(|&i| (x[[i, feat]], y[i])).collect();
        vals.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));

        for k in 0..vals.len().saturating_sub(1) {
            if (vals[k].0 - vals[k + 1].0).abs() < Float::EPSILON {
                continue;
            }
            let threshold = (vals[k].0 + vals[k + 1].0) / 2.0;

            let left_labels: Vec<Int> = vals[..=k].iter().map(|v| v.1).collect();
            let right_labels: Vec<Int> = vals[k + 1..].iter().map(|v| v.1).collect();

            if left_labels.len() < params.min_samples_leaf
                || right_labels.len() < params.min_samples_leaf
            {
                continue;
            }

            let n_l = left_labels.len() as Float;
            let n_r = right_labels.len() as Float;
            let n_f = n as Float;
            let gain = parent_impurity
                - (n_l / n_f) * impurity_fn(&left_labels)
                - (n_r / n_f) * impurity_fn(&right_labels);

            if gain > best_gain {
                best_gain = gain;
                best_feature = feat;
                best_threshold = threshold;
            }
        }
    }

    if best_gain <= 0.0 {
        return ClassifierNode::Leaf(majority_class(&labels));
    }

    let (left_idx, right_idx): (Vec<usize>, Vec<usize>) = indices
        .iter()
        .partition(|&&i| x[[i, best_feature]] <= best_threshold);

    let left = build_classifier(x, y, &left_idx, params, depth + 1);
    let right = build_classifier(x, y, &right_idx, params, depth + 1);

    ClassifierNode::Split {
        feature_index: best_feature,
        threshold: best_threshold,
        left: Box::new(left),
        right: Box::new(right),
    }
}

fn predict_node(node: &ClassifierNode, x_row: &scirs2_core::ndarray::ArrayView1<Float>) -> Int {
    match node {
        ClassifierNode::Leaf(label) => *label,
        ClassifierNode::Split {
            feature_index,
            threshold,
            left,
            right,
        } => {
            if x_row[*feature_index] <= *threshold {
                predict_node(left, x_row)
            } else {
                predict_node(right, x_row)
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Regression tree builder
// ---------------------------------------------------------------------------

/// Bundled build parameters for regression trees.
struct RegressorBuildParams {
    max_depth: usize,
    min_samples_split: usize,
    min_samples_leaf: usize,
}

fn build_regressor(
    x: &Array2<Float>,
    y: &[Float],
    indices: &[usize],
    params: &RegressorBuildParams,
    depth: usize,
) -> RegressorNode {
    let targets: Vec<Float> = indices.iter().map(|&i| y[i]).collect();
    let n = indices.len();

    if n < params.min_samples_split || depth >= params.max_depth {
        return RegressorNode::Leaf(mean_value(&targets));
    }

    let parent_var = variance(&targets);
    let n_features = x.ncols();
    let mut best_gain = Float::NEG_INFINITY;
    let mut best_feature = 0;
    let mut best_threshold = 0.0_f64;

    for feat in 0..n_features {
        let mut vals: Vec<(Float, Float)> = indices.iter().map(|&i| (x[[i, feat]], y[i])).collect();
        vals.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));

        for k in 0..vals.len().saturating_sub(1) {
            if (vals[k].0 - vals[k + 1].0).abs() < Float::EPSILON {
                continue;
            }
            let threshold = (vals[k].0 + vals[k + 1].0) / 2.0;

            let left_targets: Vec<Float> = vals[..=k].iter().map(|v| v.1).collect();
            let right_targets: Vec<Float> = vals[k + 1..].iter().map(|v| v.1).collect();

            if left_targets.len() < params.min_samples_leaf
                || right_targets.len() < params.min_samples_leaf
            {
                continue;
            }

            let n_l = left_targets.len() as Float;
            let n_r = right_targets.len() as Float;
            let n_f = n as Float;
            let gain = parent_var
                - (n_l / n_f) * variance(&left_targets)
                - (n_r / n_f) * variance(&right_targets);

            if gain > best_gain {
                best_gain = gain;
                best_feature = feat;
                best_threshold = threshold;
            }
        }
    }

    if best_gain <= 0.0 {
        return RegressorNode::Leaf(mean_value(&targets));
    }

    let (left_idx, right_idx): (Vec<usize>, Vec<usize>) = indices
        .iter()
        .partition(|&&i| x[[i, best_feature]] <= best_threshold);

    let left = build_regressor(x, y, &left_idx, params, depth + 1);
    let right = build_regressor(x, y, &right_idx, params, depth + 1);

    RegressorNode::Split {
        feature_index: best_feature,
        threshold: best_threshold,
        left: Box::new(left),
        right: Box::new(right),
    }
}

fn predict_reg_node(
    node: &RegressorNode,
    x_row: &scirs2_core::ndarray::ArrayView1<Float>,
) -> Float {
    match node {
        RegressorNode::Leaf(value) => *value,
        RegressorNode::Split {
            feature_index,
            threshold,
            left,
            right,
        } => {
            if x_row[*feature_index] <= *threshold {
                predict_reg_node(left, x_row)
            } else {
                predict_reg_node(right, x_row)
            }
        }
    }
}

// ---------------------------------------------------------------------------
// DecisionTreeClassifier impl
// ---------------------------------------------------------------------------

impl Default for DecisionTreeClassifier<Untrained> {
    fn default() -> Self {
        Self::new()
    }
}

impl DecisionTreeClassifier<Untrained> {
    pub fn new() -> Self {
        Self {
            criterion: SplitCriterion::Gini,
            max_depth: Some(1),
            min_samples_split: 2,
            min_samples_leaf: 1,
            random_state: None,
            state: PhantomData,
            tree_: None,
        }
    }

    pub fn criterion(mut self, criterion: SplitCriterion) -> Self {
        self.criterion = criterion;
        self
    }

    pub fn max_depth(mut self, max_depth: usize) -> Self {
        self.max_depth = Some(max_depth);
        self
    }

    pub fn min_samples_split(mut self, min_samples_split: usize) -> Self {
        self.min_samples_split = min_samples_split;
        self
    }

    pub fn min_samples_leaf(mut self, min_samples_leaf: usize) -> Self {
        self.min_samples_leaf = min_samples_leaf;
        self
    }

    pub fn random_state(mut self, random_state: Option<u64>) -> Self {
        self.random_state = random_state;
        self
    }
}

impl Fit<Array2<Float>, Array1<Int>> for DecisionTreeClassifier<Untrained> {
    type Fitted = DecisionTreeClassifier<Trained>;

    fn fit(self, x: &Array2<Float>, y: &Array1<Int>) -> Result<Self::Fitted> {
        let n_samples = x.nrows();
        let n_features = x.ncols();
        let indices: Vec<usize> = (0..n_samples).collect();
        let y_slice: Vec<Int> = y.iter().cloned().collect();

        let params = ClassifierBuildParams {
            criterion: self.criterion,
            max_depth: self.max_depth.unwrap_or(usize::MAX),
            min_samples_split: self.min_samples_split,
            min_samples_leaf: self.min_samples_leaf,
        };
        let root = build_classifier(x, &y_slice, &indices, &params, 0);

        Ok(DecisionTreeClassifier {
            criterion: self.criterion,
            max_depth: self.max_depth,
            min_samples_split: self.min_samples_split,
            min_samples_leaf: self.min_samples_leaf,
            random_state: self.random_state,
            state: PhantomData::<Trained>,
            tree_: Some(DecisionTreeClassifierState { root, n_features }),
        })
    }
}

impl DecisionTreeClassifier<Trained> {
    pub fn predict(&self, x: &Array2<Float>) -> Result<Array1<Int>> {
        let tree = self
            .tree_
            .as_ref()
            .expect("tree_ must be set in trained state");

        let predictions: Vec<Int> = x
            .rows()
            .into_iter()
            .map(|row| predict_node(&tree.root, &row))
            .collect();

        Ok(Array1::from_vec(predictions))
    }
}

// ---------------------------------------------------------------------------
// DecisionTreeRegressor impl
// ---------------------------------------------------------------------------

impl Default for DecisionTreeRegressor<Untrained> {
    fn default() -> Self {
        Self::new()
    }
}

impl DecisionTreeRegressor<Untrained> {
    pub fn new() -> Self {
        Self {
            criterion: SplitCriterion::Gini,
            max_depth: Some(1),
            min_samples_split: 2,
            min_samples_leaf: 1,
            random_state: None,
            state: PhantomData,
            tree_: None,
        }
    }

    pub fn criterion(mut self, criterion: SplitCriterion) -> Self {
        self.criterion = criterion;
        self
    }

    pub fn max_depth(mut self, max_depth: usize) -> Self {
        self.max_depth = Some(max_depth);
        self
    }

    pub fn min_samples_split(mut self, min_samples_split: usize) -> Self {
        self.min_samples_split = min_samples_split;
        self
    }

    pub fn min_samples_leaf(mut self, min_samples_leaf: usize) -> Self {
        self.min_samples_leaf = min_samples_leaf;
        self
    }

    pub fn random_state(mut self, random_state: Option<u64>) -> Self {
        self.random_state = random_state;
        self
    }
}

impl Fit<Array2<Float>, Array1<Float>> for DecisionTreeRegressor<Untrained> {
    type Fitted = DecisionTreeRegressor<Trained>;

    fn fit(self, x: &Array2<Float>, y: &Array1<Float>) -> Result<Self::Fitted> {
        let n_samples = x.nrows();
        let n_features = x.ncols();
        let indices: Vec<usize> = (0..n_samples).collect();
        let y_slice: Vec<Float> = y.iter().cloned().collect();

        let params = RegressorBuildParams {
            max_depth: self.max_depth.unwrap_or(usize::MAX),
            min_samples_split: self.min_samples_split,
            min_samples_leaf: self.min_samples_leaf,
        };
        let root = build_regressor(x, &y_slice, &indices, &params, 0);

        Ok(DecisionTreeRegressor {
            criterion: self.criterion,
            max_depth: self.max_depth,
            min_samples_split: self.min_samples_split,
            min_samples_leaf: self.min_samples_leaf,
            random_state: self.random_state,
            state: PhantomData::<Trained>,
            tree_: Some(DecisionTreeRegressorState { root, n_features }),
        })
    }
}

impl DecisionTreeRegressor<Trained> {
    pub fn predict(&self, x: &Array2<Float>) -> Result<Array1<Float>> {
        let tree = self
            .tree_
            .as_ref()
            .expect("tree_ must be set in trained state");

        let predictions: Vec<Float> = x
            .rows()
            .into_iter()
            .map(|row| predict_reg_node(&tree.root, &row))
            .collect();

        Ok(Array1::from_vec(predictions))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::{array, Array1, Array2};
    use sklears_core::traits::Fit;

    // -------------------------------------------------------------------
    // Classifier tests
    // -------------------------------------------------------------------

    #[test]
    fn test_stump_perfect_separation() {
        // Feature 0 perfectly separates the classes at threshold 1.5.
        let x: Array2<Float> = array![[1.0, 0.0], [1.0, 1.0], [2.0, 0.0], [2.0, 1.0]];
        let y: Array1<Int> = array![0, 0, 1, 1];
        let tree = DecisionTreeClassifier::new()
            .max_depth(1)
            .fit(&x, &y)
            .expect("fit should succeed");
        let preds = tree.predict(&x).expect("predict should succeed");
        for (i, &p) in preds.iter().enumerate() {
            assert_eq!(
                p, y[i],
                "mismatch at sample {}: predicted {}, expected {}",
                i, p, y[i]
            );
        }
    }

    #[test]
    fn test_stump_output_len() {
        let x: Array2<Float> = array![[1.0], [2.0], [3.0], [4.0]];
        let y: Array1<Int> = array![0, 0, 1, 1];
        let tree = DecisionTreeClassifier::new()
            .fit(&x, &y)
            .expect("fit should succeed");
        let preds = tree.predict(&x).expect("predict should succeed");
        assert_eq!(preds.len(), 4);
    }

    #[test]
    fn test_stump_predictions_are_valid_labels() {
        let x: Array2<Float> = array![[1.0], [2.0], [3.0], [4.0]];
        let y: Array1<Int> = array![0, 0, 1, 1];
        let tree = DecisionTreeClassifier::new()
            .fit(&x, &y)
            .expect("fit should succeed");
        let preds = tree.predict(&x).expect("predict should succeed");
        for &p in preds.iter() {
            assert!(p == 0 || p == 1, "unexpected label {}", p);
        }
    }

    #[test]
    fn test_deeper_tree_classification() {
        let x: Array2<Float> = array![
            [1.0, 1.0],
            [1.0, 2.0],
            [2.0, 1.0],
            [2.0, 2.0],
            [3.0, 1.0],
            [3.0, 2.0],
        ];
        let y: Array1<Int> = array![0, 0, 1, 1, 2, 2];
        let tree = DecisionTreeClassifier::new()
            .max_depth(3)
            .fit(&x, &y)
            .expect("fit should succeed");
        let preds = tree.predict(&x).expect("predict should succeed");
        assert_eq!(preds.len(), 6);
        for &p in preds.iter() {
            assert!(p == 0 || p == 1 || p == 2, "unexpected label {}", p);
        }
    }

    // -------------------------------------------------------------------
    // Regressor tests
    // -------------------------------------------------------------------

    #[test]
    fn test_stump_regressor_output_shape() {
        let x: Array2<Float> = array![[1.0], [2.0], [3.0], [4.0]];
        let y: Array1<Float> = array![1.0, 2.0, 3.0, 4.0];
        let tree = DecisionTreeRegressor::new()
            .max_depth(1)
            .fit(&x, &y)
            .expect("fit should succeed");
        let preds = tree.predict(&x).expect("predict should succeed");
        assert_eq!(preds.len(), 4);
        for &p in preds.iter() {
            assert!(p.is_finite());
        }
    }

    #[test]
    fn test_stump_regressor_constant_output() {
        // All targets the same → leaf predicts constant
        let x: Array2<Float> = array![[1.0], [2.0], [3.0]];
        let y: Array1<Float> = array![5.0, 5.0, 5.0];
        let tree = DecisionTreeRegressor::new()
            .max_depth(1)
            .fit(&x, &y)
            .expect("fit should succeed");
        let preds = tree.predict(&x).expect("predict should succeed");
        for &p in preds.iter() {
            assert!((p - 5.0).abs() < 1e-10, "expected 5.0, got {}", p);
        }
    }

    #[test]
    fn test_deeper_tree_regression() {
        let x: Array2<Float> = array![[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]];
        let y: Array1<Float> = array![2.0, 4.0, 6.0, 8.0, 10.0, 12.0];
        let tree = DecisionTreeRegressor::new()
            .max_depth(4)
            .fit(&x, &y)
            .expect("fit should succeed");
        let preds = tree.predict(&x).expect("predict should succeed");
        assert_eq!(preds.len(), 6);
        let mse: Float = preds
            .iter()
            .zip(y.iter())
            .map(|(&p, &t)| (p - t).powi(2))
            .sum::<Float>()
            / 6.0;
        assert!(mse < 4.0, "MSE {} too high", mse);
    }
}