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
//! Auto-generated test module (consolidated from inline `#[cfg(test)] mod` blocks)

use scirs2_core::ndarray::{Array1, Array2};
use sklears_core::types::Float;
#[allow(unused_imports)]
use std::collections::HashSet;

use super::*;

#[allow(non_snake_case)]
#[cfg(test)]
mod tests_2 {
    use super::*;
    use proptest::prelude::*;
    use scirs2_core::ndarray::array;
    use sklears_core::traits::Predict;
    #[test]
    fn test_bagging_classifier_creation() {
        let classifier = BaggingClassifier::new()
            .n_estimators(20)
            .random_state(42)
            .oob_score(true);
        assert_eq!(classifier.config.n_estimators, 20);
        assert_eq!(classifier.config.random_state, Some(42));
        assert!(classifier.config.oob_score);
    }
    #[test]
    fn test_bagging_classifier_fit_predict() {
        let x = array![
            [1.0, 2.0],
            [2.0, 3.0],
            [3.0, 4.0],
            [4.0, 5.0],
            [5.0, 6.0],
            [6.0, 7.0],
            [7.0, 8.0],
            [8.0, 9.0],
        ];
        let y = array![0, 0, 1, 1, 2, 2, 0, 1];
        let classifier = BaggingClassifier::new().n_estimators(5).random_state(42);
        let fitted = classifier
            .fit(&x, &y)
            .expect("model fitting should succeed");
        let predictions = fitted.predict(&x).expect("prediction should succeed");
        assert_eq!(predictions.len(), 8);
        assert_eq!(fitted.n_classes(), 3);
        assert_eq!(fitted.classes().len(), 3);
        assert_eq!(fitted.n_features_in(), 2);
    }
    #[test]
    fn test_bagging_classifier_with_oob() {
        let x = array![
            [1.0, 2.0],
            [2.0, 3.0],
            [3.0, 4.0],
            [4.0, 5.0],
            [5.0, 6.0],
            [6.0, 7.0],
            [7.0, 8.0],
            [8.0, 9.0],
            [9.0, 10.0],
            [10.0, 11.0],
        ];
        let y = array![0, 0, 1, 1, 2, 2, 0, 1, 2, 0];
        let classifier = BaggingClassifier::new()
            .n_estimators(10)
            .random_state(42)
            .oob_score(true)
            .bootstrap(true);
        let fitted = classifier
            .fit(&x, &y)
            .expect("model fitting should succeed");
        assert!(fitted.oob_score().is_some());
        let oob_score = fitted.oob_score().expect("operation should succeed");
        assert!((0.0..=1.0).contains(&oob_score));
        let predictions = fitted.predict(&x).expect("prediction should succeed");
        assert_eq!(predictions.len(), 10);
    }
    #[test]
    fn test_bagging_classifier_feature_bagging() {
        let x = array![
            [1.0, 2.0, 3.0, 4.0],
            [2.0, 3.0, 4.0, 5.0],
            [3.0, 4.0, 5.0, 6.0],
            [4.0, 5.0, 6.0, 7.0],
            [5.0, 6.0, 7.0, 8.0],
            [6.0, 7.0, 8.0, 9.0],
        ];
        let y = array![0, 0, 1, 1, 2, 2];
        let classifier = BaggingClassifier::new()
            .n_estimators(5)
            .max_features(Some(2))
            .bootstrap_features(false)
            .random_state(42);
        let fitted = classifier
            .fit(&x, &y)
            .expect("model fitting should succeed");
        let predictions = fitted.predict(&x).expect("prediction should succeed");
        assert_eq!(predictions.len(), 6);
        assert_eq!(fitted.n_features_in(), 4);
        let importances = fitted.feature_importances();
        assert_eq!(importances.len(), 4);
        let sum: Float = importances.sum();
        assert!((sum - 1.0).abs() < 1e-10);
    }
    #[test]
    fn test_bagging_classifier_confidence_intervals() {
        let x = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0],];
        let y = array![0, 0, 1, 1];
        let classifier = BaggingClassifier::new()
            .n_estimators(10)
            .random_state(42)
            .confidence_level(0.95);
        let fitted = classifier
            .fit(&x, &y)
            .expect("model fitting should succeed");
        let (predictions, confidence_intervals) = fitted
            .predict_with_confidence(&x)
            .expect("operation should succeed");
        assert_eq!(predictions.len(), 4);
        assert_eq!(confidence_intervals.dim(), (4, 2));
        for i in 0..4 {
            assert!(confidence_intervals[[i, 0]] <= confidence_intervals[[i, 1]]);
        }
    }
    #[test]
    fn test_bagging_regressor_creation() {
        let regressor = BaggingRegressor::new().n_estimators(15).random_state(123);
        assert_eq!(regressor.config.n_estimators, 15);
        assert_eq!(regressor.config.random_state, Some(123));
    }
    fn regression_dataset() -> (Array2<Float>, Array1<Float>) {
        let x = array![
            [1.0, 1.0],
            [2.0, 1.0],
            [3.0, 2.0],
            [4.0, 2.0],
            [5.0, 3.0],
            [6.0, 3.0],
            [7.0, 4.0],
            [8.0, 4.0],
            [1.0, 5.0],
            [2.0, 6.0],
            [3.0, 7.0],
            [4.0, 8.0],
        ];
        let y = array![1.1, 2.9, 4.05, 5.95, 7.1, 8.9, 10.05, 11.95, -2.9, -2.1, -0.95, -0.05,];
        (x, y)
    }
    fn r2_score(y_true: &Array1<Float>, y_pred: &Array1<Float>) -> Float {
        let mean_y = y_true.iter().sum::<Float>() / y_true.len() as Float;
        let ss_tot: Float = y_true.iter().map(|&yi| (yi - mean_y).powi(2)).sum();
        let ss_res: Float = y_true
            .iter()
            .zip(y_pred.iter())
            .map(|(&yi, &pi)| (yi - pi).powi(2))
            .sum();
        1.0 - ss_res / ss_tot
    }
    #[test]
    fn test_bagging_regressor_fit_predict_accuracy() {
        let (x, y) = regression_dataset();
        let fitted = BaggingRegressor::new()
            .n_estimators(25)
            .max_depth(Some(5))
            .random_state(42)
            .fit(&x, &y)
            .expect("model fitting should succeed");
        let predictions = fitted.predict(&x).expect("prediction should succeed");
        assert_eq!(predictions.len(), x.nrows());
        let p_min = predictions
            .iter()
            .cloned()
            .fold(Float::INFINITY, Float::min);
        let p_max = predictions
            .iter()
            .cloned()
            .fold(Float::NEG_INFINITY, Float::max);
        assert!(
            p_max - p_min > 1.0,
            "predictions should vary, got range [{p_min}, {p_max}]"
        );
        let r2 = r2_score(&y, &predictions);
        assert!(r2 > 0.9, "training R^2 too low: {r2}");
    }
    #[test]
    fn test_bagging_regressor_oob_score() {
        let (x, y) = regression_dataset();
        let fitted = BaggingRegressor::new()
            .n_estimators(25)
            .max_depth(Some(5))
            .oob_score(true)
            .bootstrap(true)
            .random_state(42)
            .fit(&x, &y)
            .expect("model fitting should succeed");
        let oob = fitted.oob_score();
        assert!(oob.is_some(), "oob_score should be computed when requested");
        let oob = oob.expect("oob score should be present");
        assert!(oob <= 1.0, "R^2 OOB score must not exceed 1.0, got {oob}");
    }
    #[test]
    fn test_bagging_regressor_different_seeds_differ() {
        let (x, y) = regression_dataset();
        let fitted_a = BaggingRegressor::new()
            .n_estimators(10)
            .max_depth(Some(4))
            .bootstrap(true)
            .random_state(1)
            .fit(&x, &y)
            .expect("model fitting should succeed");
        let fitted_b = BaggingRegressor::new()
            .n_estimators(10)
            .max_depth(Some(4))
            .bootstrap(true)
            .random_state(999)
            .fit(&x, &y)
            .expect("model fitting should succeed");
        let pred_a = fitted_a.predict(&x).expect("prediction should succeed");
        let pred_b = fitted_b.predict(&x).expect("prediction should succeed");
        let any_diff = pred_a
            .iter()
            .zip(pred_b.iter())
            .any(|(&a, &b)| (a - b).abs() > 1e-9);
        assert!(any_diff, "different seeds produced identical predictions");
    }
    #[test]
    fn test_bagging_regressor_determinism_same_seed() {
        let (x, y) = regression_dataset();
        let build = || {
            BaggingRegressor::new()
                .n_estimators(10)
                .max_depth(Some(4))
                .bootstrap(true)
                .random_state(7)
                .fit(&x, &y)
                .expect("model fitting should succeed")
        };
        let pred_1 = build().predict(&x).expect("prediction should succeed");
        let pred_2 = build().predict(&x).expect("prediction should succeed");
        assert_eq!(pred_1.len(), pred_2.len());
        for (a, b) in pred_1.iter().zip(pred_2.iter()) {
            assert_eq!(
                a.to_bits(),
                b.to_bits(),
                "same seed must produce bit-identical predictions"
            );
        }
    }
    #[test]
    fn test_bagging_regressor_bootstrap_diversity() {
        let x = array![
            [1.0, 1.0],
            [2.0, 1.0],
            [3.0, 2.0],
            [4.0, 2.0],
            [5.0, 3.0],
            [6.0, 3.0],
            [7.0, 4.0],
            [8.0, 4.0],
            [9.0, 5.0],
            [10.0, 5.0],
        ];
        let y = array![1.0, 3.0, 4.0, 6.0, 7.0, 9.0, 10.0, 12.0, 13.0, 15.0];
        let fitted = BaggingRegressor::new()
            .n_estimators(8)
            .bootstrap(true)
            .random_state(42)
            .fit(&x, &y)
            .expect("model fitting should succeed");
        let estimators_samples = fitted.estimators_samples();
        let mut unique_sample_sets = HashSet::new();
        for samples in estimators_samples {
            let mut sorted_samples = samples.clone();
            sorted_samples.sort();
            unique_sample_sets.insert(sorted_samples);
        }
        assert!(
            unique_sample_sets.len() > 1,
            "bootstrap samples lack diversity: {} unique of {} estimators",
            unique_sample_sets.len(),
            estimators_samples.len()
        );
    }
    #[test]
    fn test_bagging_regressor_invalid_input() {
        let regressor = BaggingRegressor::new();
        let x = Array2::zeros((0, 2));
        let y = Array1::zeros(0);
        assert!(regressor.fit(&x, &y).is_err());
        let regressor = BaggingRegressor::new();
        let x = Array2::zeros((3, 2));
        let y = Array1::zeros(2);
        assert!(regressor.fit(&x, &y).is_err());
    }
    #[test]
    fn test_bagging_regressor_feature_mismatch() {
        let x_train = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]];
        let y_train = array![1.0, 2.0, 3.0];
        let x_test = array![[1.0, 2.0]];
        let fitted = BaggingRegressor::new()
            .fit(&x_train, &y_train)
            .expect("model fitting should succeed");
        assert!(fitted.predict(&x_test).is_err());
    }
    #[test]
    fn test_bagging_regressor_feature_importances_sum() {
        let x = array![
            [1.0, 2.0, 3.0, 4.0],
            [2.0, 3.0, 4.0, 5.0],
            [3.0, 4.0, 5.0, 6.0],
            [4.0, 5.0, 6.0, 7.0],
            [5.0, 6.0, 7.0, 8.0],
            [6.0, 7.0, 8.0, 9.0],
        ];
        let y = array![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
        let fitted = BaggingRegressor::new()
            .n_estimators(5)
            .max_features(Some(2))
            .bootstrap_features(false)
            .random_state(42)
            .fit(&x, &y)
            .expect("model fitting should succeed");
        let importances = fitted.feature_importances();
        assert_eq!(importances.len(), 4);
        let sum: Float = importances.sum();
        assert!((sum - 1.0).abs() < 1e-10, "feature importances sum = {sum}");
    }
    #[test]
    fn test_bagging_config_default() {
        let config = BaggingConfig::default();
        assert_eq!(config.n_estimators, 10);
        assert!(config.bootstrap);
        assert!(!config.bootstrap_features);
        assert!(!config.oob_score);
        assert_eq!(config.random_state, None);
        assert_eq!(config.min_samples_split, 2);
        assert_eq!(config.min_samples_leaf, 1);
        assert_eq!(config.confidence_level, 0.95);
    }
    #[test]
    fn test_bagging_classifier_invalid_input() {
        let classifier = BaggingClassifier::new();
        let x = Array2::zeros((0, 2));
        let y = Array1::zeros(0);
        assert!(classifier.fit(&x, &y).is_err());
        let classifier = BaggingClassifier::new();
        let x = Array2::zeros((3, 2));
        let y = Array1::zeros(2);
        assert!(classifier.fit(&x, &y).is_err());
        let classifier = BaggingClassifier::new();
        let x = array![[1.0, 2.0], [3.0, 4.0]];
        let y = array![0, 0];
        assert!(classifier.fit(&x, &y).is_err());
    }
    #[test]
    fn test_bagging_classifier_feature_mismatch() {
        let x_train = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
        let y_train = array![0, 1];
        let x_test = array![[1.0, 2.0]];
        let classifier = BaggingClassifier::new();
        let fitted = classifier
            .fit(&x_train, &y_train)
            .expect("model fitting should succeed");
        assert!(fitted.predict(&x_test).is_err());
    }
    proptest! {
        #[test] fn prop_bagging_deterministic_with_seed(n_estimators in 1usize..10,
        random_seed in 0u64..1000,) { let x = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0],
        [4.0, 5.0], [5.0, 6.0], [6.0, 7.0], [7.0, 8.0], [8.0, 9.0],]; let y = array![0,
        0, 1, 1, 2, 2, 0, 1]; let classifier1 = BaggingClassifier::new()
        .n_estimators(n_estimators).random_state(random_seed).fit(& x, & y)
        .expect("operation should succeed"); let classifier2 = BaggingClassifier::new()
        .n_estimators(n_estimators).random_state(random_seed).fit(& x, & y)
        .expect("operation should succeed"); let pred1 = classifier1.predict(& x)
        .expect("prediction should succeed"); let pred2 = classifier2.predict(& x)
        .expect("prediction should succeed"); prop_assert_eq!(pred1, pred2); } #[test] fn
        prop_bagging_feature_importance_normalization(n_estimators in 1usize..10,
        max_features in 1usize..4,) { let x = array![[1.0, 2.0, 3.0], [2.0, 3.0, 4.0],
        [3.0, 4.0, 5.0], [4.0, 5.0, 6.0], [5.0, 6.0, 7.0], [6.0, 7.0, 8.0],]; let y =
        array![0, 0, 1, 1, 2, 2]; let classifier = BaggingClassifier::new()
        .n_estimators(n_estimators).max_features(Some(max_features)).random_state(42)
        .fit(& x, & y).expect("operation should succeed"); let importances = classifier
        .feature_importances(); let sum : Float = importances.sum(); prop_assert!((sum -
        1.0).abs() < 1e-10); for & importance in importances.iter() {
        prop_assert!(importance >= 0.0); } } #[test] fn
        prop_bagging_bootstrap_diversity(n_estimators in 2usize..8,) { let x =
        array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0], [5.0, 6.0], [6.0, 7.0],
        [7.0, 8.0], [8.0, 9.0], [9.0, 10.0], [10.0, 11.0],]; let y = array![0, 0, 1, 1,
        2, 2, 0, 1, 2, 0]; let classifier = BaggingClassifier::new()
        .n_estimators(n_estimators).bootstrap(true).random_state(42).fit(& x, & y)
        .expect("operation should succeed"); let estimators_samples = classifier
        .estimators_samples(); let mut unique_sample_sets = HashSet::new(); for samples
        in estimators_samples { let mut sorted_samples = samples.clone(); sorted_samples
        .sort(); unique_sample_sets.insert(sorted_samples); } prop_assert!(!
        unique_sample_sets.is_empty()); } #[test] fn
        prop_bagging_prediction_stability(n_estimators in 3usize..10,) { let x =
        array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0], [5.0, 6.0], [6.0, 7.0],];
        let y = array![0, 0, 1, 1, 2, 2]; let classifier = BaggingClassifier::new()
        .n_estimators(n_estimators).random_state(42).fit(& x, & y)
        .expect("operation should succeed"); let predictions = classifier.predict(& x)
        .expect("prediction should succeed"); let classes = classifier.classes(); for &
        pred in predictions.iter() { prop_assert!(classes.iter().any(|& c | c == pred));
        } prop_assert_eq!(predictions.len(), x.nrows()); } #[test] fn
        prop_bagging_oob_score_bounds(n_estimators in 5usize..15,) { let x = array![[1.0,
        2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0], [5.0, 6.0], [6.0, 7.0], [7.0, 8.0],
        [8.0, 9.0], [9.0, 10.0], [10.0, 11.0], [11.0, 12.0], [12.0, 13.0],]; let y =
        array![0, 0, 1, 1, 2, 2, 0, 1, 2, 0, 1, 2]; let classifier =
        BaggingClassifier::new().n_estimators(n_estimators).oob_score(true)
        .bootstrap(true).random_state(42).fit(& x, & y)
        .expect("operation should succeed"); if let Some(oob_score) = classifier
        .oob_score() { prop_assert!((0.0..= 1.0).contains(& oob_score)); } } #[test] fn
        prop_bagging_confidence_intervals_bounds(n_estimators in 3usize..8,
        confidence_level in 0.7..0.99,) { let x = array![[1.0, 2.0], [2.0, 3.0], [3.0,
        4.0], [4.0, 5.0],]; let y = array![0, 0, 1, 1]; let classifier =
        BaggingClassifier::new().n_estimators(n_estimators)
        .confidence_level(confidence_level).random_state(42).fit(& x, & y)
        .expect("operation should succeed"); let (predictions, confidence_intervals) =
        classifier.predict_with_confidence(& x).expect("operation should succeed"); for i
        in 0..predictions.len() { let lower = confidence_intervals[[i, 0]]; let upper =
        confidence_intervals[[i, 1]]; prop_assert!(lower <= upper); prop_assert!(lower
        .is_finite() && upper.is_finite()); } }
    }
}