sklears-ensemble 0.1.2

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
//! Meta-learning strategies and utilities for stacking ensembles
//!
//! This module provides implementations of various meta-learning algorithms
//! used to combine predictions from base estimators in stacking ensembles.

use super::config::MetaLearningStrategy;
use scirs2_core::ndarray::{s, Array1, Array2, ArrayView1};
use sklears_core::{
    error::{Result, SklearsError},
    types::Float,
};

/// Meta-learner implementation that combines base estimator predictions
#[derive(Debug, Clone)]
pub struct MetaLearner {
    /// The strategy used for meta-learning
    pub strategy: MetaLearningStrategy,
    /// Learned weights (if applicable)
    pub weights: Option<Array1<Float>>,
    /// Learned intercept (if applicable)
    pub intercept: Option<Float>,
    /// Number of features expected during prediction
    pub n_features: Option<usize>,
}

impl MetaLearner {
    /// Create a new meta-learner with the specified strategy
    pub fn new(strategy: MetaLearningStrategy) -> Self {
        Self {
            strategy,
            weights: None,
            intercept: None,
            n_features: None,
        }
    }

    /// Train the meta-learner on meta-features
    pub fn fit(&mut self, meta_features: &Array2<Float>, targets: &Array1<Float>) -> Result<()> {
        if meta_features.nrows() != targets.len() {
            return Err(SklearsError::ShapeMismatch {
                expected: format!("{} samples", meta_features.nrows()),
                actual: format!("{} samples", targets.len()),
            });
        }

        let n_features = meta_features.ncols();
        self.n_features = Some(n_features);

        match self.strategy {
            MetaLearningStrategy::LinearRegression => {
                let (weights, intercept) = self.fit_linear_regression(meta_features, targets)?;
                self.weights = Some(weights);
                self.intercept = Some(intercept);
            }
            MetaLearningStrategy::Ridge(alpha) => {
                let (weights, intercept) =
                    self.fit_ridge_regression(meta_features, targets, alpha)?;
                self.weights = Some(weights);
                self.intercept = Some(intercept);
            }
            MetaLearningStrategy::Lasso(alpha) => {
                let (weights, intercept) =
                    self.fit_lasso_regression(meta_features, targets, alpha)?;
                self.weights = Some(weights);
                self.intercept = Some(intercept);
            }
            MetaLearningStrategy::ElasticNet(alpha, l1_ratio) => {
                let (weights, intercept) =
                    self.fit_elastic_net(meta_features, targets, alpha, l1_ratio)?;
                self.weights = Some(weights);
                self.intercept = Some(intercept);
            }
            MetaLearningStrategy::LogisticRegression => {
                let (weights, intercept) = self.fit_logistic_regression(meta_features, targets)?;
                self.weights = Some(weights);
                self.intercept = Some(intercept);
            }
            MetaLearningStrategy::BayesianAveraging => {
                // Bayesian averaging uses uniform weights
                self.weights = Some(Array1::from_elem(n_features, 1.0 / n_features as Float));
                self.intercept = Some(0.0);
            }
            _ => {
                // For other strategies, use linear regression as fallback
                let (weights, intercept) = self.fit_linear_regression(meta_features, targets)?;
                self.weights = Some(weights);
                self.intercept = Some(intercept);
            }
        }

        Ok(())
    }

    /// Make predictions using the trained meta-learner
    pub fn predict(&self, meta_features: &Array2<Float>) -> Result<Array1<Float>> {
        let weights = self
            .weights
            .as_ref()
            .ok_or_else(|| SklearsError::NotFitted {
                operation: "predict".to_string(),
            })?;
        let intercept = self.intercept.ok_or_else(|| SklearsError::NotFitted {
            operation: "predict".to_string(),
        })?;

        if meta_features.ncols() != self.n_features.expect("operation should succeed") {
            return Err(SklearsError::FeatureMismatch {
                expected: self.n_features.expect("operation should succeed"),
                actual: meta_features.ncols(),
            });
        }

        let n_samples = meta_features.nrows();
        let mut predictions = Array1::zeros(n_samples);

        for i in 0..n_samples {
            let sample = meta_features.row(i);
            predictions[i] = sample.dot(weights) + intercept;
        }

        Ok(predictions)
    }

    /// Fit linear regression using normal equations
    fn fit_linear_regression(
        &self,
        x: &Array2<Float>,
        y: &Array1<Float>,
    ) -> Result<(Array1<Float>, Float)> {
        let (n_samples, n_features) = x.dim();

        // Create augmented feature matrix with intercept column
        let mut x_aug = Array2::ones((n_samples, n_features + 1));
        x_aug.slice_mut(s![.., ..n_features]).assign(x);

        // Solve normal equations: (X^T X)^(-1) X^T y
        let xtx = x_aug.t().dot(&x_aug);
        let xty = x_aug.t().dot(y);

        let params = self.solve_linear_system(&xtx, &xty)?;
        let intercept = params[n_features];
        let weights = params.slice(s![..n_features]).to_owned();

        Ok((weights, intercept))
    }

    /// Fit Ridge regression with L2 regularization
    fn fit_ridge_regression(
        &self,
        x: &Array2<Float>,
        y: &Array1<Float>,
        alpha: Float,
    ) -> Result<(Array1<Float>, Float)> {
        let (n_samples, n_features) = x.dim();

        // Create augmented feature matrix with intercept column
        let mut x_aug = Array2::ones((n_samples, n_features + 1));
        x_aug.slice_mut(s![.., ..n_features]).assign(x);

        // Solve regularized normal equations: (X^T X + αI)^(-1) X^T y
        let mut xtx = x_aug.t().dot(&x_aug);

        // Add regularization to diagonal (except intercept term)
        for i in 0..n_features {
            xtx[[i, i]] += alpha;
        }

        let xty = x_aug.t().dot(y);
        let params = self.solve_linear_system(&xtx, &xty)?;

        let intercept = params[n_features];
        let weights = params.slice(s![..n_features]).to_owned();

        Ok((weights, intercept))
    }

    /// Fit Lasso regression with L1 regularization (simplified implementation)
    fn fit_lasso_regression(
        &self,
        x: &Array2<Float>,
        y: &Array1<Float>,
        alpha: Float,
    ) -> Result<(Array1<Float>, Float)> {
        // For simplicity, use coordinate descent approximation with soft thresholding
        let (n_samples, n_features) = x.dim();
        let mut weights = Array1::zeros(n_features);
        let mut intercept = y.mean().unwrap_or(0.0);

        // Simple coordinate descent iterations
        for _iter in 0..100 {
            for j in 0..n_features {
                let mut residual = 0.0;
                for i in 0..n_samples {
                    let mut prediction = intercept;
                    for k in 0..n_features {
                        if k != j {
                            prediction += weights[k] * x[[i, k]];
                        }
                    }
                    residual += x[[i, j]] * (y[i] - prediction);
                }

                // Soft thresholding
                let threshold = alpha * n_samples as Float;
                if residual > threshold {
                    weights[j] = (residual - threshold) / n_samples as Float;
                } else if residual < -threshold {
                    weights[j] = (residual + threshold) / n_samples as Float;
                } else {
                    weights[j] = 0.0;
                }
            }

            // Update intercept
            let mut prediction_sum = 0.0;
            for i in 0..n_samples {
                prediction_sum += weights.dot(&x.row(i));
            }
            intercept = (y.sum() - prediction_sum) / n_samples as Float;
        }

        Ok((weights, intercept))
    }

    /// Fit Elastic Net regression with combined L1/L2 regularization
    fn fit_elastic_net(
        &self,
        x: &Array2<Float>,
        y: &Array1<Float>,
        alpha: Float,
        l1_ratio: Float,
    ) -> Result<(Array1<Float>, Float)> {
        let l1_alpha = alpha * l1_ratio;
        let l2_alpha = alpha * (1.0 - l1_ratio);

        // Simplified elastic net using coordinate descent
        let (n_samples, n_features) = x.dim();
        let mut weights = Array1::zeros(n_features);
        let mut intercept = y.mean().unwrap_or(0.0);

        for _iter in 0..100 {
            for j in 0..n_features {
                let mut residual = 0.0;
                let mut x_squared_sum = 0.0;

                for i in 0..n_samples {
                    let mut prediction = intercept;
                    for k in 0..n_features {
                        if k != j {
                            prediction += weights[k] * x[[i, k]];
                        }
                    }
                    residual += x[[i, j]] * (y[i] - prediction);
                    x_squared_sum += x[[i, j]] * x[[i, j]];
                }

                // Soft thresholding with L2 modification
                let threshold = l1_alpha * n_samples as Float;
                let denominator = x_squared_sum + l2_alpha * n_samples as Float;

                if residual > threshold {
                    weights[j] = (residual - threshold) / denominator;
                } else if residual < -threshold {
                    weights[j] = (residual + threshold) / denominator;
                } else {
                    weights[j] = 0.0;
                }
            }

            // Update intercept
            let mut prediction_sum = 0.0;
            for i in 0..n_samples {
                prediction_sum += weights.dot(&x.row(i));
            }
            intercept = (y.sum() - prediction_sum) / n_samples as Float;
        }

        Ok((weights, intercept))
    }

    /// Fit logistic regression (simplified implementation)
    fn fit_logistic_regression(
        &self,
        x: &Array2<Float>,
        y: &Array1<Float>,
    ) -> Result<(Array1<Float>, Float)> {
        let (n_samples, n_features) = x.dim();
        let mut weights = Array1::zeros(n_features);
        let mut intercept = 0.0;
        let learning_rate = 0.01;

        // Simple gradient descent
        for _iter in 0..1000 {
            let mut weight_gradients = Array1::<Float>::zeros(n_features);
            let mut intercept_gradient = 0.0;

            for i in 0..n_samples {
                let z = weights.dot(&x.row(i)) + intercept;
                let prediction = self.sigmoid(z);
                let error = y[i] - prediction;

                for j in 0..n_features {
                    weight_gradients[j] += error * x[[i, j]];
                }
                intercept_gradient += error;
            }

            // Update parameters
            for j in 0..n_features {
                weights[j] += learning_rate * weight_gradients[j] / n_samples as Float;
            }
            intercept += learning_rate * intercept_gradient / n_samples as Float;
        }

        Ok((weights, intercept))
    }

    /// Sigmoid activation function
    fn sigmoid(&self, z: Float) -> Float {
        1.0 / (1.0 + (-z).exp())
    }

    /// Solve linear system using Gaussian elimination
    fn solve_linear_system(&self, a: &Array2<Float>, b: &Array1<Float>) -> Result<Array1<Float>> {
        let n = a.nrows();
        if n != a.ncols() || n != b.len() {
            return Err(SklearsError::InvalidInput(
                "Matrix dimensions don't match".to_string(),
            ));
        }

        // Create augmented matrix
        let mut aug = Array2::zeros((n, n + 1));
        for i in 0..n {
            for j in 0..n {
                aug[[i, j]] = a[[i, j]];
            }
            aug[[i, n]] = b[i];
        }

        // Forward elimination with partial pivoting
        for i in 0..n {
            // Find pivot
            let mut max_row = i;
            for k in (i + 1)..n {
                if aug[[k, i]].abs() > aug[[max_row, i]].abs() {
                    max_row = k;
                }
            }

            // Swap rows
            if max_row != i {
                for j in 0..(n + 1) {
                    let temp = aug[[i, j]];
                    aug[[i, j]] = aug[[max_row, j]];
                    aug[[max_row, j]] = temp;
                }
            }

            // Check for singular matrix
            if aug[[i, i]].abs() < 1e-12 {
                return Err(SklearsError::NumericalError(
                    "Singular matrix in linear system".to_string(),
                ));
            }

            // Eliminate column
            for k in (i + 1)..n {
                let factor = aug[[k, i]] / aug[[i, i]];
                for j in i..(n + 1) {
                    aug[[k, j]] -= factor * aug[[i, j]];
                }
            }
        }

        // Back substitution
        let mut x = Array1::zeros(n);
        for i in (0..n).rev() {
            x[i] = aug[[i, n]];
            for j in (i + 1)..n {
                x[i] -= aug[[i, j]] * x[j];
            }
            x[i] /= aug[[i, i]];
        }

        Ok(x)
    }
}

/// Calculate ensemble diversity using pairwise correlation
pub fn calculate_diversity(predictions: &Array2<Float>) -> Result<Float> {
    let (_n_samples, n_estimators) = predictions.dim();

    if n_estimators < 2 {
        return Ok(0.0);
    }

    let mut total_correlation = 0.0;
    let mut count = 0;

    for i in 0..n_estimators {
        for j in (i + 1)..n_estimators {
            let pred_i = predictions.column(i);
            let pred_j = predictions.column(j);

            let correlation = calculate_correlation(&pred_i, &pred_j)?;
            total_correlation += correlation.abs();
            count += 1;
        }
    }

    if count == 0 {
        Ok(0.0)
    } else {
        // Diversity is 1 - average absolute correlation
        Ok(1.0 - total_correlation / count as Float)
    }
}

/// Calculate Pearson correlation coefficient between two vectors
pub fn calculate_correlation(x: &ArrayView1<Float>, y: &ArrayView1<Float>) -> Result<Float> {
    if x.len() != y.len() {
        return Err(SklearsError::InvalidInput(
            "Vectors must have the same length".to_string(),
        ));
    }

    let n = x.len() as Float;
    if n < 2.0 {
        return Ok(0.0);
    }

    let mean_x = x.sum() / n;
    let mean_y = y.sum() / n;

    let mut numerator = 0.0;
    let mut sum_sq_x = 0.0;
    let mut sum_sq_y = 0.0;

    for i in 0..x.len() {
        let dx = x[i] - mean_x;
        let dy = y[i] - mean_y;

        numerator += dx * dy;
        sum_sq_x += dx * dx;
        sum_sq_y += dy * dy;
    }

    let denominator = (sum_sq_x * sum_sq_y).sqrt();

    if denominator < 1e-12 {
        Ok(0.0)
    } else {
        Ok(numerator / denominator)
    }
}

#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::array;

    #[test]
    fn test_meta_learner_creation() {
        let meta_learner = MetaLearner::new(MetaLearningStrategy::LinearRegression);
        assert!(matches!(
            meta_learner.strategy,
            MetaLearningStrategy::LinearRegression
        ));
        assert!(meta_learner.weights.is_none());
        assert!(meta_learner.intercept.is_none());
    }

    #[test]
    fn test_linear_regression_fit_predict() {
        let mut meta_learner = MetaLearner::new(MetaLearningStrategy::LinearRegression);

        // Create non-linearly dependent data to avoid singular matrix
        let meta_features = array![
            [1.0, 0.5],
            [2.0, 1.0],
            [0.5, 2.0],
            [1.5, 0.8],
            [0.3, 1.2],
            [2.1, 0.4]
        ];
        let targets = array![1.2, 2.1, 1.8, 1.6, 1.1, 1.9];

        meta_learner
            .fit(&meta_features, &targets)
            .expect("model fitting should succeed");

        assert!(meta_learner.weights.is_some());
        assert!(meta_learner.intercept.is_some());

        let predictions = meta_learner
            .predict(&meta_features)
            .expect("prediction should succeed");
        assert_eq!(predictions.len(), 6);
    }

    #[test]
    fn test_ridge_regression() {
        let mut meta_learner = MetaLearner::new(MetaLearningStrategy::Ridge(0.1));

        let meta_features = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
        let targets = array![3.0, 5.0, 7.0, 9.0];

        meta_learner
            .fit(&meta_features, &targets)
            .expect("model fitting should succeed");
        let predictions = meta_learner
            .predict(&meta_features)
            .expect("prediction should succeed");
        assert_eq!(predictions.len(), 4);
    }

    #[test]
    fn test_bayesian_averaging() {
        let mut meta_learner = MetaLearner::new(MetaLearningStrategy::BayesianAveraging);

        let meta_features = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
        let targets = array![6.0, 15.0];

        meta_learner
            .fit(&meta_features, &targets)
            .expect("model fitting should succeed");

        let weights = meta_learner
            .weights
            .as_ref()
            .expect("operation should succeed");
        assert_eq!(weights.len(), 3);
        assert!((weights.sum() - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_diversity_calculation() {
        let predictions = array![
            [1.0, 2.0, 1.5],
            [2.0, 3.0, 2.2],
            [3.0, 4.0, 3.8],
            [4.0, 5.0, 4.1]
        ];

        let diversity = calculate_diversity(&predictions).expect("operation should succeed");
        assert!((0.0..=1.0).contains(&diversity));
    }

    #[test]
    fn test_correlation_calculation() {
        let x = array![1.0, 2.0, 3.0, 4.0];
        let y = array![2.0, 4.0, 6.0, 8.0]; // Perfect positive correlation

        let correlation =
            calculate_correlation(&x.view(), &y.view()).expect("operation should succeed");
        assert!((correlation - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_shape_mismatch_error() {
        let mut meta_learner = MetaLearner::new(MetaLearningStrategy::LinearRegression);

        let meta_features = array![[1.0, 2.0], [3.0, 4.0]];
        let targets = array![3.0]; // Wrong length

        let result = meta_learner.fit(&meta_features, &targets);
        assert!(result.is_err());
    }

    #[test]
    fn test_not_fitted_error() {
        let meta_learner = MetaLearner::new(MetaLearningStrategy::LinearRegression);
        let meta_features = array![[1.0, 2.0]];

        let result = meta_learner.predict(&meta_features);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not fitted"));
    }
}