anofox-ml-ensemble 0.1.0

Random forest and gradient boosting ensemble methods for the anofox-ml library
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
//! Stacking classifier: two-level ensemble where base classifiers' predictions
//! are used as features for a meta-classifier.
//!
//! Mirrors `sklearn.ensemble.StackingClassifier` with `stack_method='predict'`:
//! base classifier *predictions* (not class probabilities) become inputs to the
//! meta-estimator. Out-of-fold predictions are generated via k-fold CV during
//! fitting to avoid leakage.

use anofox_ml_core::{Fit, Float, Predict, PredictProba, Result, RustMlError};
use ndarray::{Array1, Array2};

/// Choice of base-estimator output used as meta-features.
///
/// - `Predict`: hard class labels (sklearn `stack_method='predict'`).
/// - `PredictProba`: class probabilities (sklearn `stack_method='predict_proba'`).
///
/// sklearn's default is `'auto'`, which prefers `predict_proba` then
/// `decision_function` then `predict`. We require an explicit choice and
/// only support the two forms above.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum StackMethod {
    Predict,
    PredictProba,
}

trait FitPredBox<F: Float>: Send + Sync {
    fn fit_box(&self, x: &Array2<F>, y: &Array1<F>) -> Result<Box<dyn PredBox<F>>>;
}

trait FitProbaBox<F: Float>: Send + Sync {
    fn fit_proba_box(&self, x: &Array2<F>, y: &Array1<F>) -> Result<Box<dyn ProbaBox<F>>>;
}

trait PredBox<F: Float>: Send + Sync {
    fn predict_box(&self, x: &Array2<F>) -> Result<Array1<F>>;
}

trait ProbaBox<F: Float>: Send + Sync {
    fn predict_proba_box(&self, x: &Array2<F>) -> Result<Array2<F>>;
}

impl<F, T> FitPredBox<F> for T
where
    F: Float,
    T: Fit<F> + Send + Sync,
    T::Fitted: Predict<F> + Send + Sync + 'static,
{
    fn fit_box(&self, x: &Array2<F>, y: &Array1<F>) -> Result<Box<dyn PredBox<F>>> {
        let fitted = Fit::fit(self, x, y)?;
        Ok(Box::new(fitted))
    }
}

impl<F, T> PredBox<F> for T
where
    F: Float,
    T: Predict<F> + Send + Sync,
{
    fn predict_box(&self, x: &Array2<F>) -> Result<Array1<F>> {
        self.predict(x)
    }
}

/// Wrapper for estimators whose Fitted type implements both Predict and PredictProba.
struct ProbaWrap<T>(T);

impl<F, T> FitProbaBox<F> for ProbaWrap<T>
where
    F: Float,
    T: Fit<F> + Send + Sync,
    T::Fitted: Predict<F> + PredictProba<F> + Send + Sync + 'static,
{
    fn fit_proba_box(&self, x: &Array2<F>, y: &Array1<F>) -> Result<Box<dyn ProbaBox<F>>> {
        let fitted = Fit::fit(&self.0, x, y)?;
        Ok(Box::new(fitted))
    }
}

impl<F, T> ProbaBox<F> for T
where
    F: Float,
    T: PredictProba<F> + Send + Sync,
{
    fn predict_proba_box(&self, x: &Array2<F>) -> Result<Array2<F>> {
        self.predict_proba(x)
    }
}

/// Stacking classifier.
///
/// Base estimators contribute either hard predictions (one meta-feature each)
/// or class probabilities (n_classes - 1 meta-features each, dropping the last
/// column to avoid colinearity — sklearn's convention).
pub struct StackingClassifier<F: Float> {
    base_estimators: Vec<(String, BaseEstimator<F>)>,
    meta_estimator: Box<dyn FitPredBox<F>>,
    cv_folds: usize,
}

enum BaseEstimator<F: Float> {
    Predict(Box<dyn FitPredBox<F>>),
    PredictProba(Box<dyn FitProbaBox<F>>),
}

impl<F: Float> StackingClassifier<F> {
    pub fn new<M>(meta_estimator: M) -> Self
    where
        M: Fit<F> + Send + Sync + 'static,
        M::Fitted: Predict<F> + Send + Sync + 'static,
    {
        Self {
            base_estimators: Vec::new(),
            meta_estimator: Box::new(meta_estimator),
            cv_folds: 5,
        }
    }

    /// Add a base estimator using hard predictions (`stack_method='predict'`).
    pub fn push<T>(mut self, name: impl Into<String>, estimator: T) -> Self
    where
        T: Fit<F> + Send + Sync + 'static,
        T::Fitted: Predict<F> + Send + Sync + 'static,
    {
        self.base_estimators
            .push((name.into(), BaseEstimator::Predict(Box::new(estimator))));
        self
    }

    /// Add a base estimator using `predict_proba` outputs (sklearn's
    /// `stack_method='predict_proba'`).
    pub fn push_proba<T>(mut self, name: impl Into<String>, estimator: T) -> Self
    where
        T: Fit<F> + Send + Sync + 'static,
        T::Fitted: Predict<F> + PredictProba<F> + Send + Sync + 'static,
    {
        self.base_estimators.push((
            name.into(),
            BaseEstimator::PredictProba(Box::new(ProbaWrap(estimator))),
        ));
        self
    }

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

pub struct FittedStackingClassifier<F: Float> {
    fitted_base: Vec<(String, FittedBase<F>)>,
    fitted_meta: Box<dyn PredBox<F>>,
    n_features: usize,
}

enum FittedBase<F: Float> {
    Predict(Box<dyn PredBox<F>>),
    PredictProba(Box<dyn ProbaBox<F>>),
}

impl<F: Float> FittedStackingClassifier<F> {
    pub fn estimator_names(&self) -> Vec<&str> {
        self.fitted_base.iter().map(|(n, _)| n.as_str()).collect()
    }
}

impl<F: Float + 'static> Fit<F> for StackingClassifier<F> {
    type Fitted = FittedStackingClassifier<F>;

    fn fit(&self, x: &Array2<F>, y: &Array1<F>) -> Result<Self::Fitted> {
        if self.base_estimators.is_empty() {
            return Err(RustMlError::InvalidParameter(
                "StackingClassifier needs at least one base estimator".into(),
            ));
        }
        if x.nrows() != y.len() {
            return Err(RustMlError::ShapeMismatch(format!(
                "X has {} rows but y has {} elements",
                x.nrows(),
                y.len()
            )));
        }
        let n = x.nrows();
        if n < 2 {
            return Err(RustMlError::EmptyInput("need at least 2 samples".into()));
        }

        let k = self.cv_folds.min(n);
        let folds = simple_k_fold(n, k);

        // Two passes through base estimators. Pass 1: out-of-fold predictions
        // build the meta-feature matrix. We need to know each base's output
        // width — for Predict it's 1, for PredictProba it's n_classes. We
        // discover n_classes from the first proba estimator's prediction;
        // otherwise default to 1.

        // Generate meta-features per estimator first, accumulate into a Vec<Vec<f64>>
        // (one column per meta-feature, length n).
        let mut meta_cols: Vec<Array1<F>> = Vec::new();
        for (_name, est) in self.base_estimators.iter() {
            match est {
                BaseEstimator::Predict(b) => {
                    let mut col = Array1::<F>::zeros(n);
                    for (train_idx, test_idx) in &folds {
                        let x_train = select_rows(x, train_idx);
                        let y_train = select_elements(y, train_idx);
                        let x_test = select_rows(x, test_idx);
                        let fitted = b.fit_box(&x_train, &y_train)?;
                        let preds = fitted.predict_box(&x_test)?;
                        for (li, &gi) in test_idx.iter().enumerate() {
                            col[gi] = preds[li];
                        }
                    }
                    meta_cols.push(col);
                }
                BaseEstimator::PredictProba(b) => {
                    // Need to know n_classes; defer column creation until we
                    // see the first proba output.
                    let mut buf: Option<Array2<F>> = None;
                    for (train_idx, test_idx) in &folds {
                        let x_train = select_rows(x, train_idx);
                        let y_train = select_elements(y, train_idx);
                        let x_test = select_rows(x, test_idx);
                        let fitted = b.fit_proba_box(&x_train, &y_train)?;
                        let probs = fitted.predict_proba_box(&x_test)?;
                        let nc = probs.ncols();
                        let bufm = buf.get_or_insert_with(|| Array2::<F>::zeros((n, nc)));
                        for (li, &gi) in test_idx.iter().enumerate() {
                            for c in 0..nc {
                                bufm[[gi, c]] = probs[[li, c]];
                            }
                        }
                    }
                    if let Some(bufm) = buf {
                        for c in 0..bufm.ncols() {
                            meta_cols.push(bufm.column(c).to_owned());
                        }
                    }
                }
            }
        }

        let n_meta = meta_cols.len();
        let mut meta_features = Array2::<F>::zeros((n, n_meta));
        for (c, col) in meta_cols.iter().enumerate() {
            for i in 0..n {
                meta_features[[i, c]] = col[i];
            }
        }

        let fitted_meta = self.meta_estimator.fit_box(&meta_features, y)?;

        let mut fitted_base = Vec::with_capacity(self.base_estimators.len());
        for (name, est) in &self.base_estimators {
            let f = match est {
                BaseEstimator::Predict(b) => FittedBase::Predict(b.fit_box(x, y)?),
                BaseEstimator::PredictProba(b) => FittedBase::PredictProba(b.fit_proba_box(x, y)?),
            };
            fitted_base.push((name.clone(), f));
        }

        Ok(FittedStackingClassifier {
            fitted_base,
            fitted_meta,
            n_features: x.ncols(),
        })
    }
}

impl<F: Float> Predict<F> for FittedStackingClassifier<F> {
    fn predict(&self, x: &Array2<F>) -> Result<Array1<F>> {
        if x.ncols() != self.n_features {
            return Err(RustMlError::ShapeMismatch(format!(
                "expected {} features, got {}",
                self.n_features,
                x.ncols()
            )));
        }

        let n = x.nrows();
        let mut meta_cols: Vec<Array1<F>> = Vec::new();
        for (_name, m) in &self.fitted_base {
            match m {
                FittedBase::Predict(p) => {
                    meta_cols.push(p.predict_box(x)?);
                }
                FittedBase::PredictProba(p) => {
                    let probs = p.predict_proba_box(x)?;
                    for c in 0..probs.ncols() {
                        meta_cols.push(probs.column(c).to_owned());
                    }
                }
            }
        }
        let mut meta_features = Array2::<F>::zeros((n, meta_cols.len()));
        for (c, col) in meta_cols.iter().enumerate() {
            for i in 0..n {
                meta_features[[i, c]] = col[i];
            }
        }
        self.fitted_meta.predict_box(&meta_features)
    }
}

fn simple_k_fold(n: usize, k: usize) -> Vec<(Vec<usize>, Vec<usize>)> {
    let fold_size = n / k;
    let remainder = n % k;
    let mut folds = Vec::with_capacity(k);
    let mut start = 0;
    for f in 0..k {
        let end = start + fold_size + if f < remainder { 1 } else { 0 };
        let test: Vec<usize> = (start..end).collect();
        let train: Vec<usize> = (0..start).chain(end..n).collect();
        folds.push((train, test));
        start = end;
    }
    folds
}

fn select_rows<F: Float>(x: &Array2<F>, indices: &[usize]) -> Array2<F> {
    let ncols = x.ncols();
    let mut data = Vec::with_capacity(indices.len() * ncols);
    for &i in indices {
        for j in 0..ncols {
            data.push(x[[i, j]]);
        }
    }
    Array2::from_shape_vec((indices.len(), ncols), data).unwrap()
}

fn select_elements<F: Float>(y: &Array1<F>, indices: &[usize]) -> Array1<F> {
    Array1::from_vec(indices.iter().map(|&i| y[i]).collect())
}

#[cfg(test)]
mod tests {
    use super::*;
    use anofox_ml_trees::DecisionTreeClassifier;
    use ndarray::array;

    #[test]
    fn test_stacking_classifier_basic() {
        // Two well-separated clusters, interleaved so simple k-fold sees both
        // classes in each fold.
        let x = array![
            [0.0, 0.0],
            [5.0, 5.0],
            [0.1, 0.1],
            [5.1, 5.0],
            [0.2, -0.1],
            [4.9, 5.1],
            [-0.1, 0.2],
            [5.2, 4.8],
        ];
        let y = array![0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0];

        let sc = StackingClassifier::new(DecisionTreeClassifier::default())
            .push(
                "t1",
                DecisionTreeClassifier {
                    max_depth: Some(2),
                    ..Default::default()
                },
            )
            .push(
                "t2",
                DecisionTreeClassifier {
                    max_depth: Some(3),
                    ..Default::default()
                },
            )
            .with_cv_folds(2);

        let fitted: FittedStackingClassifier<f64> = sc.fit(&x, &y).unwrap();
        let preds = fitted.predict(&x).unwrap();
        for (p, t) in preds.iter().zip(y.iter()) {
            assert_eq!(*p, *t, "p={p}, t={t}");
        }
    }

    #[test]
    fn test_stacking_classifier_proba_path() {
        // Stack two DT classifiers via predict_proba into a DT meta.
        let x = array![
            [0.0, 0.0],
            [5.0, 5.0],
            [0.1, 0.1],
            [5.1, 5.0],
            [0.2, -0.1],
            [4.9, 5.1],
            [-0.1, 0.2],
            [5.2, 4.8],
        ];
        let y = array![0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0];

        let sc = StackingClassifier::new(DecisionTreeClassifier::default())
            .push_proba(
                "t1",
                DecisionTreeClassifier {
                    max_depth: Some(2),
                    ..Default::default()
                },
            )
            .push_proba(
                "t2",
                DecisionTreeClassifier {
                    max_depth: Some(3),
                    ..Default::default()
                },
            )
            .with_cv_folds(2);

        let fitted: FittedStackingClassifier<f64> = sc.fit(&x, &y).unwrap();
        let preds = fitted.predict(&x).unwrap();
        for (p, t) in preds.iter().zip(y.iter()) {
            assert_eq!(*p, *t, "p={p}, t={t}");
        }
    }

    #[test]
    fn test_stacking_classifier_empty_base_error() {
        let x = array![[1.0], [2.0]];
        let y = array![0.0, 1.0];

        let sc = StackingClassifier::<f64>::new(DecisionTreeClassifier::default());
        assert!(sc.fit(&x, &y).is_err());
    }
}