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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
//! AdaBoost Classifier implementation

use super::helpers::*;
use super::types::*;
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::random::RngExt;
use sklears_core::{
    error::{Result, SklearsError},
    prelude::{Fit, Predict},
    traits::{Trained, Untrained},
    types::Float,
};
use std::marker::PhantomData;

use super::types::AdaBoostClassifier;
impl AdaBoostClassifier<Untrained> {
    /// Create a new AdaBoost classifier
    pub fn new() -> Self {
        Self {
            config: AdaBoostConfig::default(),
            state: PhantomData,
            estimators_: None,
            estimator_weights_: None,
            estimator_errors_: None,
            classes_: None,
            n_classes_: None,
            n_features_in_: None,
        }
    }

    /// Set the number of boosting iterations
    pub fn n_estimators(mut self, n_estimators: usize) -> Self {
        self.config.n_estimators = n_estimators;
        self
    }

    /// Set the learning rate
    pub fn learning_rate(mut self, learning_rate: Float) -> Self {
        self.config.learning_rate = learning_rate;
        self
    }

    /// Set the random state for reproducible results
    pub fn random_state(mut self, random_state: u64) -> Self {
        self.config.random_state = Some(random_state);
        self
    }

    /// Set the algorithm variant
    pub fn algorithm(mut self, algorithm: AdaBoostAlgorithm) -> Self {
        self.config.algorithm = algorithm;
        self
    }

    /// Use the SAMME.R algorithm variant
    pub fn with_samme_r(mut self) -> Self {
        self.config.algorithm = AdaBoostAlgorithm::SAMMER;
        self
    }

    /// Use the Gentle AdaBoost algorithm variant
    pub fn with_gentle(mut self) -> Self {
        self.config.algorithm = AdaBoostAlgorithm::Gentle;
        self
    }

    /// Find unique classes in the target array
    pub(crate) fn find_classes(y: &Array1<Float>) -> Array1<Float> {
        let mut classes: Vec<Float> = y.iter().cloned().collect();
        classes.sort_by(|a, b| a.partial_cmp(b).expect("operation should succeed"));
        classes.dedup();
        Array1::from_vec(classes)
    }

    /// Calculate class weights for current iteration
    fn calculate_sample_weights(
        &self,
        y: &Array1<Float>,
        y_pred: &Array1<Float>,
        sample_weight: &Array1<Float>,
        estimator_weight: Float,
    ) -> Array1<Float> {
        let n_samples = y.len();
        let mut new_weights = sample_weight.clone();

        for i in 0..n_samples {
            if y[i] != y_pred[i] {
                new_weights[i] *= (estimator_weight).exp();
            }
        }

        let weight_sum = new_weights.sum();
        if weight_sum > 0.0 {
            new_weights /= weight_sum;
        } else {
            new_weights.fill(1.0 / n_samples as Float);
        }

        new_weights
    }

    /// Calculate estimator weight based on error
    fn calculate_estimator_weight(&self, error: Float, n_classes: usize) -> Float {
        if error <= 0.0 {
            return 10.0;
        }

        if error >= 1.0 - 1.0 / n_classes as Float {
            return 0.0;
        }

        match self.config.algorithm {
            AdaBoostAlgorithm::SAMME => {
                let alpha = ((1.0 - error) / error).ln() + (n_classes as Float - 1.0).ln();
                alpha * self.config.learning_rate
            }
            AdaBoostAlgorithm::SAMMER => self.config.learning_rate,
            AdaBoostAlgorithm::RealAdaBoost => 0.5 * ((1.0 - error) / error).ln(),
            AdaBoostAlgorithm::M1 => {
                if error >= 0.5 {
                    return 0.0;
                }
                0.5 * ((1.0 - error) / error).ln()
            }
            AdaBoostAlgorithm::M2 => {
                let alpha = 0.5 * ((1.0 - error) / error).ln();
                alpha * self.config.learning_rate
            }
            AdaBoostAlgorithm::Gentle => {
                let alpha = 0.5 * ((1.0 - error) / error).ln();
                alpha * self.config.learning_rate * 0.5
            }
            AdaBoostAlgorithm::Discrete => ((1.0 - error) / error).ln() * self.config.learning_rate,
        }
    }

    /// Resample data according to sample weights
    fn resample_data(
        &self,
        x: &Array2<Float>,
        y: &Array1<i32>,
        sample_weight: &Array1<Float>,
        rng: &mut impl RngExt,
    ) -> Result<(Array2<Float>, Array1<i32>)> {
        let n_samples = x.nrows();

        let weight_sum = sample_weight.sum();
        let normalized_weights = if weight_sum > 0.0 {
            sample_weight / weight_sum
        } else {
            Array1::<Float>::from_elem(n_samples, 1.0 / n_samples as Float)
        };

        let mut cumulative = Array1::<Float>::zeros(n_samples);
        cumulative[0] = normalized_weights[0];
        for i in 1..n_samples {
            cumulative[i] = cumulative[i - 1] + normalized_weights[i];
        }

        let mut selected_indices = Vec::new();
        let unique_classes: std::collections::HashSet<i32> = y.iter().cloned().collect();

        for _i in 0..n_samples {
            let rand_val = rng.random::<Float>() * cumulative[n_samples - 1];
            let idx = cumulative
                .iter()
                .position(|&cum| cum >= rand_val)
                .unwrap_or(n_samples - 1);
            selected_indices.push(idx);
        }

        let resampled_classes: std::collections::HashSet<i32> =
            selected_indices.iter().map(|&idx| y[idx]).collect();

        if resampled_classes.len() < unique_classes.len() {
            let mut replacement_count = 0;
            for &missing_class in unique_classes.difference(&resampled_classes) {
                if let Some(original_idx) = y.iter().position(|&class| class == missing_class) {
                    if replacement_count < selected_indices.len() {
                        selected_indices[replacement_count] = original_idx;
                        replacement_count += 1;
                    }
                }
            }
        }

        let mut x_resampled = Array2::<Float>::zeros(x.dim());
        let mut y_resampled = Array1::<i32>::zeros(y.len());

        for (i, &idx) in selected_indices.iter().enumerate() {
            x_resampled.row_mut(i).assign(&x.row(idx));
            y_resampled[i] = y[idx];
        }

        Ok((x_resampled, y_resampled))
    }

    /// Calculate sample weights for SAMME.R algorithm
    fn calculate_sample_weights_sammer(
        &self,
        y: &Array1<Float>,
        prob_estimates: &Array2<Float>,
        sample_weight: &Array1<Float>,
        classes: &Array1<Float>,
        estimator_weight: Float,
    ) -> Array1<Float> {
        let n_samples = y.len();
        let n_classes = classes.len();
        let mut new_weights = sample_weight.clone();

        let factor = ((n_classes - 1) as Float / n_classes as Float) * estimator_weight;

        for i in 0..n_samples {
            let true_class = y[i];
            let true_class_idx = classes.iter().position(|&c| c == true_class);

            if let Some(class_idx) = true_class_idx {
                let probs = prob_estimates.row(i);

                let mut h_xi = 0.0;
                for k in 0..n_classes {
                    let p_k = probs[k].clamp(1e-7, 1.0 - 1e-7);

                    if k == class_idx {
                        h_xi += (n_classes as Float - 1.0) * p_k.ln();
                    } else {
                        h_xi -= p_k.ln();
                    }
                }

                let weight_multiplier = (-factor * h_xi / n_classes as Float).exp();
                new_weights[i] *= weight_multiplier;
                new_weights[i] = new_weights[i].clamp(1e-10, 1e3);
            }
        }

        let weight_sum = new_weights.sum();
        if weight_sum > 0.0 {
            new_weights /= weight_sum;
        } else {
            new_weights.fill(1.0 / n_samples as Float);
        }

        new_weights
    }

    /// Calculate sample weights for Real AdaBoost
    fn calculate_sample_weights_real_adaboost(
        &self,
        y: &Array1<Float>,
        prob_estimates: &Array2<Float>,
        sample_weight: &Array1<Float>,
        classes: &Array1<Float>,
    ) -> Array1<Float> {
        let n_samples = y.len();
        let mut new_weights = sample_weight.clone();

        for i in 0..n_samples {
            let true_class = y[i];
            let y_i = if true_class == classes[0] { -1.0 } else { 1.0 };

            let p_0 = prob_estimates[[i, 0]].clamp(1e-7, 1.0 - 1e-7);
            let p_1 = prob_estimates[[i, 1]].clamp(1e-7, 1.0 - 1e-7);

            let h_xi = 0.5 * (p_1 / p_0).ln();

            let weight_multiplier = (-y_i * h_xi).exp();
            new_weights[i] *= weight_multiplier;
            new_weights[i] = new_weights[i].clamp(1e-10, 1e3);
        }

        let weight_sum = new_weights.sum();
        if weight_sum > 0.0 {
            new_weights /= weight_sum;
        } else {
            new_weights.fill(1.0 / n_samples as Float);
        }

        new_weights
    }
}

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

impl<State> std::fmt::Debug for AdaBoostClassifier<State> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AdaBoostClassifier")
            .field("config", &self.config)
            .field(
                "n_estimators_fitted",
                &self.estimators_.as_ref().map(|e| e.len()),
            )
            .field("n_classes", &self.n_classes_)
            .field("n_features_in", &self.n_features_in_)
            .finish()
    }
}

impl Fit<Array2<Float>, Array1<Float>> for AdaBoostClassifier<Untrained> {
    type Fitted = AdaBoostClassifier<Trained>;
    fn fit(self, x: &Array2<Float>, y: &Array1<Float>) -> Result<Self::Fitted> {
        let (n_samples, n_features) = x.dim();
        if n_samples != y.len() {
            return Err(SklearsError::InvalidInput(
                "Number of samples in X and y must match".to_string(),
            ));
        }
        if n_samples == 0 {
            return Err(SklearsError::InvalidInput(
                "Cannot fit AdaBoost on empty dataset".to_string(),
            ));
        }
        if self.config.n_estimators == 0 {
            return Err(SklearsError::InvalidParameter {
                name: "n_estimators".to_string(),
                reason: "Number of estimators must be positive".to_string(),
            });
        }
        let classes = Self::find_classes(y);
        let n_classes = classes.len();
        if n_classes < 2 {
            return Err(SklearsError::InvalidInput(
                "AdaBoost requires at least 2 classes".to_string(),
            ));
        }
        let mut sample_weight = Array1::<Float>::from_elem(n_samples, 1.0 / n_samples as Float);
        let mut estimators = Vec::new();
        let mut estimator_weights = Vec::new();
        let mut estimator_errors = Vec::new();
        let mut rng = match self.config.random_state {
            Some(seed) => scirs2_core::random::seeded_rng(seed),
            None => scirs2_core::random::seeded_rng(42),
        };
        let y_i32 = convert_labels_to_i32(y);
        for _iteration in 0..self.config.n_estimators {
            let base_estimator = DecisionTreeClassifier::new()
                .criterion(SplitCriterion::Gini)
                .max_depth(1)
                .min_samples_split(2)
                .min_samples_leaf(1);
            match self.config.algorithm {
                AdaBoostAlgorithm::SAMME => {
                    let (x_resampled, y_resampled) =
                        self.resample_data(x, &y_i32, &sample_weight, &mut rng)?;
                    let fitted_estimator = base_estimator.fit(&x_resampled, &y_resampled)?;
                    let y_pred_i32 = fitted_estimator.predict(x)?;
                    let y_pred = convert_predictions_to_float(&y_pred_i32);
                    if y_pred.len() != n_samples {
                        return Err(SklearsError::ShapeMismatch {
                            expected: format!("{} predictions", n_samples),
                            actual: format!("{} predictions", y_pred.len()),
                        });
                    }
                    let mut weighted_error = 0.0;
                    for i in 0..n_samples {
                        if y[i] != y_pred[i] {
                            weighted_error += sample_weight[i];
                        }
                    }
                    if weighted_error >= 0.5 {
                        if estimators.is_empty() {
                            estimators.push(fitted_estimator);
                            estimator_weights.push(0.0);
                            estimator_errors.push(weighted_error);
                        }
                        break;
                    }
                    let estimator_weight =
                        self.calculate_estimator_weight(weighted_error, n_classes);
                    estimators.push(fitted_estimator);
                    estimator_weights.push(estimator_weight);
                    estimator_errors.push(weighted_error);
                    sample_weight =
                        self.calculate_sample_weights(y, &y_pred, &sample_weight, estimator_weight);
                    if weighted_error < 1e-10 {
                        break;
                    }
                }
                AdaBoostAlgorithm::SAMMER => {
                    let (x_resampled, y_resampled) =
                        self.resample_data(x, &y_i32, &sample_weight, &mut rng)?;
                    let fitted_estimator = base_estimator.fit(&x_resampled, &y_resampled)?;
                    let y_pred_i32 = fitted_estimator.predict(x)?;
                    let y_pred = convert_predictions_to_float(&y_pred_i32);
                    if y_pred.len() != n_samples {
                        return Err(SklearsError::ShapeMismatch {
                            expected: format!("{} predictions", n_samples),
                            actual: format!("{} predictions", y_pred.len()),
                        });
                    }
                    let prob_estimates =
                        estimate_probabilities(&y_pred, &classes, n_samples, n_classes);
                    let mut weighted_error = 0.0;
                    for i in 0..n_samples {
                        if y[i] != y_pred[i] {
                            weighted_error += sample_weight[i];
                        }
                    }
                    let estimator_weight = self.config.learning_rate;
                    estimators.push(fitted_estimator);
                    estimator_weights.push(estimator_weight);
                    estimator_errors.push(weighted_error);
                    sample_weight = self.calculate_sample_weights_sammer(
                        y,
                        &prob_estimates,
                        &sample_weight,
                        &classes,
                        estimator_weight,
                    );
                    if !(1e-10..0.5).contains(&weighted_error) {
                        break;
                    }
                }
                AdaBoostAlgorithm::RealAdaBoost => {
                    let (x_resampled, y_resampled) =
                        self.resample_data(x, &y_i32, &sample_weight, &mut rng)?;
                    let fitted_estimator = base_estimator.fit(&x_resampled, &y_resampled)?;
                    if n_classes != 2 {
                        return Err(SklearsError::InvalidInput(
                            "Real AdaBoost currently supports only binary classification"
                                .to_string(),
                        ));
                    }
                    let y_pred_i32 = fitted_estimator.predict(x)?;
                    let y_pred = convert_predictions_to_float(&y_pred_i32);
                    if y_pred.len() != n_samples {
                        return Err(SklearsError::ShapeMismatch {
                            expected: format!("{} predictions", n_samples),
                            actual: format!("{} predictions", y_pred.len()),
                        });
                    }
                    let prob_estimates = estimate_binary_probabilities(&y_pred, &classes);
                    let mut weighted_error = 0.0;
                    for i in 0..n_samples {
                        let correct_class_idx = if y[i] == classes[0] { 0 } else { 1 };
                        let prob_correct = prob_estimates[[i, correct_class_idx]];
                        if prob_correct < 0.5 {
                            weighted_error += sample_weight[i];
                        }
                    }
                    let estimator_weight = if weighted_error > 0.0 && weighted_error < 0.5 {
                        0.5 * ((1.0 - weighted_error) / weighted_error).ln()
                    } else if weighted_error == 0.0 {
                        10.0
                    } else {
                        0.0
                    };
                    estimators.push(fitted_estimator);
                    estimator_weights.push(estimator_weight);
                    estimator_errors.push(weighted_error);
                    sample_weight = self.calculate_sample_weights_real_adaboost(
                        y,
                        &prob_estimates,
                        &sample_weight,
                        &classes,
                    );
                    if !(1e-10..0.5).contains(&weighted_error) {
                        break;
                    }
                }
                AdaBoostAlgorithm::M1 => {
                    let (x_resampled, y_resampled) =
                        self.resample_data(x, &y_i32, &sample_weight, &mut rng)?;
                    let fitted_estimator = base_estimator.fit(&x_resampled, &y_resampled)?;
                    let y_pred_i32 = fitted_estimator.predict(x)?;
                    let y_pred = convert_predictions_to_float(&y_pred_i32);
                    if y_pred.len() != n_samples {
                        return Err(SklearsError::ShapeMismatch {
                            expected: format!("{} predictions", n_samples),
                            actual: format!("{} predictions", y_pred.len()),
                        });
                    }
                    let mut weighted_error = 0.0;
                    for i in 0..n_samples {
                        if y[i] != y_pred[i] {
                            weighted_error += sample_weight[i];
                        }
                    }
                    if weighted_error >= 0.5 {
                        if estimators.is_empty() {
                            return Err(SklearsError::InvalidInput(
                                "AdaBoost.M1 requires strong learners (error < 0.5)".to_string(),
                            ));
                        }
                        break;
                    }
                    let estimator_weight =
                        self.calculate_estimator_weight(weighted_error, n_classes);
                    estimators.push(fitted_estimator);
                    estimator_weights.push(estimator_weight);
                    estimator_errors.push(weighted_error);
                    sample_weight =
                        self.calculate_sample_weights(y, &y_pred, &sample_weight, estimator_weight);
                    if weighted_error < 1e-10 {
                        break;
                    }
                }
                AdaBoostAlgorithm::M2 => {
                    let (x_resampled, y_resampled) =
                        self.resample_data(x, &y_i32, &sample_weight, &mut rng)?;
                    let fitted_estimator = base_estimator.fit(&x_resampled, &y_resampled)?;
                    let y_pred_i32 = fitted_estimator.predict(x)?;
                    let y_pred = convert_predictions_to_float(&y_pred_i32);
                    if y_pred.len() != n_samples {
                        return Err(SklearsError::ShapeMismatch {
                            expected: format!("{} predictions", n_samples),
                            actual: format!("{} predictions", y_pred.len()),
                        });
                    }
                    let prob_estimates =
                        estimate_probabilities(&y_pred, &classes, n_samples, n_classes);
                    let mut pseudo_loss = 0.0;
                    for i in 0..n_samples {
                        let true_class_idx = classes.iter().position(|&c| c == y[i]).unwrap_or(0);
                        let mut margin = prob_estimates[[i, true_class_idx]];
                        for j in 0..n_classes {
                            if j != true_class_idx {
                                margin -= prob_estimates[[i, j]] / (n_classes - 1) as Float;
                            }
                        }
                        if margin <= 0.0 {
                            pseudo_loss += sample_weight[i] * (1.0 - margin);
                        }
                    }
                    let total_weight: Float = sample_weight.sum();
                    if total_weight > 0.0 {
                        pseudo_loss /= total_weight;
                    }
                    if pseudo_loss >= 0.5 {
                        if estimators.is_empty() {
                            estimators.push(fitted_estimator);
                            estimator_weights.push(0.0);
                            estimator_errors.push(pseudo_loss);
                        }
                        break;
                    }
                    let estimator_weight = self.calculate_estimator_weight(pseudo_loss, n_classes);
                    estimators.push(fitted_estimator);
                    estimator_weights.push(estimator_weight);
                    estimator_errors.push(pseudo_loss);
                    let mut new_weights = sample_weight.clone();
                    for i in 0..n_samples {
                        let true_class_idx = classes.iter().position(|&c| c == y[i]).unwrap_or(0);
                        let confidence = prob_estimates[[i, true_class_idx]];
                        let weight_multiplier = if confidence < 0.5 {
                            (estimator_weight * (1.0 - confidence)).exp()
                        } else {
                            (estimator_weight * confidence).exp().recip()
                        };
                        new_weights[i] *= weight_multiplier;
                    }
                    let weight_sum = new_weights.sum();
                    if weight_sum > 0.0 {
                        new_weights /= weight_sum;
                    } else {
                        new_weights.fill(1.0 / n_samples as Float);
                    }
                    sample_weight = new_weights;
                    if pseudo_loss < 1e-10 {
                        break;
                    }
                }
                AdaBoostAlgorithm::Gentle | AdaBoostAlgorithm::Discrete => {
                    let (x_resampled, y_resampled) =
                        self.resample_data(x, &y_i32, &sample_weight, &mut rng)?;
                    let fitted_estimator = base_estimator.fit(&x_resampled, &y_resampled)?;
                    let y_pred_i32 = fitted_estimator.predict(x)?;
                    let y_pred = convert_predictions_to_float(&y_pred_i32);
                    if y_pred.len() != n_samples {
                        return Err(SklearsError::ShapeMismatch {
                            expected: format!("{} predictions", n_samples),
                            actual: format!("{} predictions", y_pred.len()),
                        });
                    }
                    let mut weighted_error = 0.0;
                    for i in 0..n_samples {
                        if y[i] != y_pred[i] {
                            weighted_error += sample_weight[i];
                        }
                    }
                    if weighted_error >= 0.6 {
                        if estimators.is_empty() {
                            estimators.push(fitted_estimator);
                            estimator_weights.push(0.1);
                            estimator_errors.push(weighted_error);
                        }
                        break;
                    }
                    let estimator_weight =
                        self.calculate_estimator_weight(weighted_error, n_classes);
                    estimators.push(fitted_estimator);
                    estimator_weights.push(estimator_weight);
                    estimator_errors.push(weighted_error);
                    let mut new_weights = sample_weight.clone();
                    let gentle_factor = 0.5;
                    for i in 0..n_samples {
                        let multiplier = if y[i] != y_pred[i] {
                            (gentle_factor * estimator_weight).exp()
                        } else {
                            (-gentle_factor * estimator_weight).exp()
                        };
                        new_weights[i] *= multiplier;
                    }
                    let weight_sum = new_weights.sum();
                    if weight_sum > 0.0 {
                        new_weights /= weight_sum;
                        let smoothing = 0.01;
                        let uniform_weight = 1.0 / n_samples as Float;
                        for i in 0..n_samples {
                            new_weights[i] =
                                (1.0 - smoothing) * new_weights[i] + smoothing * uniform_weight;
                        }
                        let smoothed_sum = new_weights.sum();
                        if smoothed_sum > 0.0 {
                            new_weights /= smoothed_sum;
                        }
                    } else {
                        new_weights.fill(1.0 / n_samples as Float);
                    }
                    sample_weight = new_weights;
                    if weighted_error < 1e-10 {
                        break;
                    }
                }
            }
        }
        if estimators.is_empty() {
            return Err(SklearsError::InvalidInput(
                "AdaBoost failed to fit any estimators".to_string(),
            ));
        }
        Ok(AdaBoostClassifier {
            config: self.config,
            state: PhantomData,
            estimators_: Some(estimators),
            estimator_weights_: Some(Array1::from_vec(estimator_weights)),
            estimator_errors_: Some(Array1::from_vec(estimator_errors)),
            classes_: Some(classes),
            n_classes_: Some(n_classes),
            n_features_in_: Some(n_features),
        })
    }
}

impl AdaBoostClassifier<Trained> {
    /// Get the fitted base estimators
    pub fn estimators(&self) -> &[DecisionTreeClassifier<Trained>] {
        self.estimators_
            .as_ref()
            .expect("AdaBoost should be fitted")
    }

    /// Get the weights for each estimator
    pub fn estimator_weights(&self) -> &Array1<Float> {
        self.estimator_weights_
            .as_ref()
            .expect("AdaBoost should be fitted")
    }

    /// Get the errors for each estimator
    pub fn estimator_errors(&self) -> &Array1<Float> {
        self.estimator_errors_
            .as_ref()
            .expect("AdaBoost should be fitted")
    }

    /// Get the classes
    pub fn classes(&self) -> &Array1<Float> {
        self.classes_.as_ref().expect("AdaBoost should be fitted")
    }

    /// Get the number of classes
    pub fn n_classes(&self) -> usize {
        self.n_classes_.expect("AdaBoost should be fitted")
    }

    /// Get the number of input features
    pub fn n_features_in(&self) -> usize {
        self.n_features_in_.expect("AdaBoost should be fitted")
    }

    /// Get feature importances (averaged from all estimators)
    pub fn feature_importances(&self) -> Result<Array1<Float>> {
        let estimators = self.estimators();
        let weights = self.estimator_weights();
        let n_features = self.n_features_in();

        if estimators.is_empty() {
            return Ok(Array1::<Float>::zeros(n_features));
        }

        let mut importances = Array1::<Float>::zeros(n_features);
        let mut total_weight = 0.0;

        for (_estimator, &weight) in estimators.iter().zip(weights.iter()) {
            // For decision stumps, we simulate feature importance
            // In practice, this would use the actual feature importance from the tree
            let tree_importances = Array1::<Float>::ones(n_features) / n_features as Float;
            importances += &(tree_importances * weight.abs());
            total_weight += weight.abs();
        }

        if total_weight > 0.0 {
            importances /= total_weight;
        } else {
            // If no valid weights, return uniform importance
            importances.fill(1.0 / n_features as Float);
        }

        Ok(importances)
    }

    /// Predict class probabilities using weighted voting
    pub fn predict_proba(&self, x: &Array2<Float>) -> Result<Array2<Float>> {
        let (n_samples, n_features) = x.dim();

        if n_features != self.n_features_in() {
            return Err(SklearsError::FeatureMismatch {
                expected: self.n_features_in(),
                actual: n_features,
            });
        }

        let estimators = self.estimators();
        let weights = self.estimator_weights();
        let classes = self.classes();
        let n_classes = self.n_classes();

        match self.config.algorithm {
            AdaBoostAlgorithm::SAMME => {
                // Original SAMME: use weighted voting
                let mut class_votes = Array2::<Float>::zeros((n_samples, n_classes));

                // Aggregate predictions from all estimators
                for (estimator, &weight) in estimators.iter().zip(weights.iter()) {
                    let predictions_i32 = estimator.predict(x)?;
                    let predictions = convert_predictions_to_float(&predictions_i32);

                    for (i, &pred) in predictions.iter().enumerate() {
                        if let Some(class_idx) = classes.iter().position(|&c| c == pred) {
                            class_votes[[i, class_idx]] += weight;
                        }
                    }
                }

                // Convert votes to probabilities
                let mut probabilities = Array2::<Float>::zeros((n_samples, n_classes));
                for i in 0..n_samples {
                    let vote_sum = class_votes.row(i).sum();
                    if vote_sum > 0.0 {
                        for j in 0..n_classes {
                            probabilities[[i, j]] = class_votes[[i, j]] / vote_sum;
                        }
                    } else {
                        // Uniform distribution if no votes
                        probabilities.row_mut(i).fill(1.0 / n_classes as Float);
                    }
                }

                Ok(probabilities)
            }
            AdaBoostAlgorithm::SAMMER => {
                // SAMME.R: use probability aggregation
                let mut prob_sum = Array2::<Float>::zeros((n_samples, n_classes));

                // Initialize with uniform probabilities
                prob_sum.fill(1.0 / n_classes as Float);

                // Aggregate probability estimates from all estimators
                for (estimator, &weight) in estimators.iter().zip(weights.iter()) {
                    let predictions_i32 = estimator.predict(x)?;
                    let predictions = convert_predictions_to_float(&predictions_i32);

                    // Estimate probabilities for this estimator
                    let prob_estimates =
                        estimate_probabilities(&predictions, classes, n_samples, n_classes);

                    // SAMME.R aggregation: geometric mean of probabilities
                    for i in 0..n_samples {
                        for j in 0..n_classes {
                            // Use log-space for numerical stability
                            prob_sum[[i, j]] += weight * prob_estimates[[i, j]].ln();
                        }
                    }
                }

                // Convert back from log-space and normalize
                let mut probabilities = Array2::<Float>::zeros((n_samples, n_classes));
                for i in 0..n_samples {
                    // Find max for numerical stability
                    let max_log = prob_sum
                        .row(i)
                        .iter()
                        .cloned()
                        .fold(f64::NEG_INFINITY, f64::max);

                    // Compute exp and normalize
                    let mut sum = 0.0;
                    for j in 0..n_classes {
                        probabilities[[i, j]] = (prob_sum[[i, j]] - max_log).exp();
                        sum += probabilities[[i, j]];
                    }

                    // Normalize
                    for j in 0..n_classes {
                        probabilities[[i, j]] /= sum;
                    }
                }

                Ok(probabilities)
            }
            AdaBoostAlgorithm::RealAdaBoost => {
                // Real AdaBoost probability aggregation for binary classification
                if n_classes != 2 {
                    return Err(SklearsError::InvalidInput(
                        "Real AdaBoost predict_proba only supports binary classification"
                            .to_string(),
                    ));
                }

                let mut decision_scores = Array1::<Float>::zeros(n_samples);

                // Aggregate decision scores from all estimators
                for (estimator, &weight) in estimators.iter().zip(weights.iter()) {
                    let predictions_i32 = estimator.predict(x)?;
                    let predictions = convert_predictions_to_float(&predictions_i32);

                    // Get probability estimates for Real AdaBoost
                    let prob_estimates = estimate_binary_probabilities(&predictions, classes);

                    for i in 0..n_samples {
                        let p_0 = prob_estimates[[i, 0]].clamp(1e-7, 1.0 - 1e-7);
                        let p_1 = prob_estimates[[i, 1]].clamp(1e-7, 1.0 - 1e-7);

                        // Real AdaBoost decision function: h_t(x) = 0.5 * ln(p_1/p_0)
                        let h_t = 0.5 * (p_1 / p_0).ln();
                        decision_scores[i] += weight * h_t;
                    }
                }

                // Convert decision scores to probabilities using sigmoid
                let mut probabilities = Array2::<Float>::zeros((n_samples, 2));
                for i in 0..n_samples {
                    let sigmoid = 1.0 / (1.0 + (-decision_scores[i]).exp());
                    probabilities[[i, 1]] = sigmoid;
                    probabilities[[i, 0]] = 1.0 - sigmoid;
                }

                Ok(probabilities)
            }
            AdaBoostAlgorithm::M1 => {
                // AdaBoost.M1: similar to SAMME but with strong learner assumption
                let mut class_votes = Array2::<Float>::zeros((n_samples, n_classes));

                // Aggregate predictions from all estimators
                for (estimator, &weight) in estimators.iter().zip(weights.iter()) {
                    let predictions_i32 = estimator.predict(x)?;
                    let predictions = convert_predictions_to_float(&predictions_i32);

                    for (i, &pred) in predictions.iter().enumerate() {
                        if let Some(class_idx) = classes.iter().position(|&c| c == pred) {
                            class_votes[[i, class_idx]] += weight;
                        }
                    }
                }

                // Convert votes to probabilities (same as SAMME)
                let mut probabilities = Array2::<Float>::zeros((n_samples, n_classes));
                for i in 0..n_samples {
                    let vote_sum = class_votes.row(i).sum();
                    if vote_sum > 0.0 {
                        for j in 0..n_classes {
                            probabilities[[i, j]] = class_votes[[i, j]] / vote_sum;
                        }
                    } else {
                        // Uniform distribution if no votes
                        probabilities.row_mut(i).fill(1.0 / n_classes as Float);
                    }
                }

                Ok(probabilities)
            }
            AdaBoostAlgorithm::M2 => {
                // AdaBoost.M2: confidence-rated predictions with probability weighting
                let mut confidence_scores = Array2::<Float>::zeros((n_samples, n_classes));

                // Aggregate confidence-rated predictions from all estimators
                for (estimator, &weight) in estimators.iter().zip(weights.iter()) {
                    let predictions_i32 = estimator.predict(x)?;
                    let predictions = convert_predictions_to_float(&predictions_i32);

                    // Estimate confidences for M2 (using pseudo-probabilities)
                    let prob_estimates =
                        estimate_probabilities(&predictions, classes, n_samples, n_classes);

                    for i in 0..n_samples {
                        for j in 0..n_classes {
                            // M2 confidence: higher confidence for more certain predictions
                            let confidence = prob_estimates[[i, j]];
                            confidence_scores[[i, j]] += weight * confidence;
                        }
                    }
                }

                // Convert confidence scores to probabilities
                let mut probabilities = Array2::<Float>::zeros((n_samples, n_classes));
                for i in 0..n_samples {
                    let score_sum = confidence_scores.row(i).sum();
                    if score_sum > 0.0 {
                        for j in 0..n_classes {
                            probabilities[[i, j]] = confidence_scores[[i, j]] / score_sum;
                        }
                    } else {
                        // Uniform distribution if no scores
                        probabilities.row_mut(i).fill(1.0 / n_classes as Float);
                    }
                }

                Ok(probabilities)
            }
            AdaBoostAlgorithm::Gentle | AdaBoostAlgorithm::Discrete => {
                // Gentle AdaBoost: similar to SAMME but with dampened weights
                let mut class_votes = Array2::<Float>::zeros((n_samples, n_classes));

                // Aggregate predictions from all estimators with gentle weighting
                for (estimator, &weight) in estimators.iter().zip(weights.iter()) {
                    let predictions_i32 = estimator.predict(x)?;
                    let predictions = convert_predictions_to_float(&predictions_i32);

                    for (i, &pred) in predictions.iter().enumerate() {
                        if let Some(class_idx) = classes.iter().position(|&c| c == pred) {
                            class_votes[[i, class_idx]] += weight;
                        }
                    }
                }

                // Convert votes to probabilities with gentle smoothing
                let mut probabilities = Array2::<Float>::zeros((n_samples, n_classes));
                for i in 0..n_samples {
                    let vote_sum = class_votes.row(i).sum();
                    if vote_sum > 0.0 {
                        for j in 0..n_classes {
                            probabilities[[i, j]] = class_votes[[i, j]] / vote_sum;
                        }

                        // Apply gentle smoothing to avoid overconfident predictions
                        let alpha = 0.1; // Smoothing parameter
                        let uniform_prob = 1.0 / n_classes as Float;
                        for j in 0..n_classes {
                            probabilities[[i, j]] =
                                (1.0 - alpha) * probabilities[[i, j]] + alpha * uniform_prob;
                        }
                    } else {
                        // Uniform distribution if no votes
                        probabilities.row_mut(i).fill(1.0 / n_classes as Float);
                    }
                }

                Ok(probabilities)
            }
        }
    }

    /// Get decision function values
    pub fn decision_function(&self, x: &Array2<Float>) -> Result<Array2<Float>> {
        let probas = self.predict_proba(x)?;

        // For binary classification, return log-odds
        if self.n_classes() == 2 {
            let mut decision = Array2::<Float>::zeros((probas.nrows(), 1));
            for i in 0..probas.nrows() {
                let p1 = probas[[i, 1]].max(1e-15); // Avoid log(0)
                let p0 = probas[[i, 0]].max(1e-15);
                decision[[i, 0]] = (p1 / p0).ln();
            }
            Ok(decision)
        } else {
            // For multi-class, return log probabilities
            Ok(probas.mapv(|p| p.max(1e-15).ln()))
        }
    }
}

impl Predict<Array2<Float>, Array1<Float>> for AdaBoostClassifier<Trained> {
    fn predict(&self, x: &Array2<Float>) -> Result<Array1<Float>> {
        let probas = self.predict_proba(x)?;
        let classes = self.classes();
        let mut predictions = Array1::<Float>::zeros(probas.nrows());
        for (i, row) in probas.rows().into_iter().enumerate() {
            let max_idx = row
                .iter()
                .enumerate()
                .max_by(|(_, a), (_, b)| a.partial_cmp(b).expect("operation should succeed"))
                .map(|(idx, _)| idx)
                .unwrap_or(0);
            predictions[i] = classes[max_idx];
        }
        Ok(predictions)
    }
}