sklears-tree 0.1.2

Decision tree algorithms for sklears: CART, ID3, C4.5
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
//! Decision Tree Classifier implementation
//!
//! This module contains the DecisionTreeClassifier struct and its implementation
//! for classification tasks using decision trees.

use crate::builder::*;
use crate::config::*;
use crate::node::*;
use scirs2_core::ndarray::{Array1, Array2, s};
use sklears_core::{
    error::{Result, SklearsError},
    traits::{Estimator, Fit, Predict, Trained, Untrained},
    types::Float,
};
use smartcore::linalg::basic::matrix::DenseMatrix;
use smartcore::tree::decision_tree_classifier::{
    DecisionTreeClassifier as SmartCoreClassifier, DecisionTreeClassifierParameters,
    SplitCriterion as SmartCoreSplitCriterion,
};
use std::marker::PhantomData;

/// Decision Tree Classifier
pub struct DecisionTreeClassifier<State = Untrained> {
    config: DecisionTreeConfig,
    state: PhantomData<State>,
    // Fitted attributes
    model_: Option<SmartCoreClassifier<f64, i32, DenseMatrix<f64>, Vec<i32>>>,
    classes_: Option<Array1<i32>>,
    n_classes_: Option<usize>,
    n_features_: Option<usize>,
    max_depth_: Option<usize>,
}

impl DecisionTreeClassifier<Untrained> {
    /// Create a new Decision Tree Classifier
    pub fn new() -> Self {
        Self {
            config: DecisionTreeConfig::default(),
            state: PhantomData,
            model_: None,
            classes_: None,
            n_classes_: None,
            n_features_: None,
            max_depth_: None,
        }
    }

    /// Set the split criterion
    pub fn criterion(mut self, criterion: SplitCriterion) -> Self {
        self.config.criterion = criterion;
        self
    }

    /// Set the maximum depth of the tree
    pub fn max_depth(mut self, max_depth: usize) -> Self {
        self.config.max_depth = Some(max_depth);
        self
    }

    /// Set the minimum samples required to split
    pub fn min_samples_split(mut self, min_samples_split: usize) -> Self {
        self.config.min_samples_split = min_samples_split;
        self
    }

    /// Set the minimum samples required at a leaf
    pub fn min_samples_leaf(mut self, min_samples_leaf: usize) -> Self {
        self.config.min_samples_leaf = min_samples_leaf;
        self
    }

    /// Set the maximum features strategy
    pub fn max_features(mut self, max_features: MaxFeatures) -> Self {
        self.config.max_features = max_features;
        self
    }

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

    /// Set the minimum impurity decrease
    pub fn min_impurity_decrease(mut self, min_impurity_decrease: f64) -> Self {
        self.config.min_impurity_decrease = min_impurity_decrease;
        self
    }

    /// Set the pruning strategy
    pub fn pruning(mut self, pruning: PruningStrategy) -> Self {
        self.config.pruning = pruning;
        self
    }

    /// Set the missing values handling strategy
    pub fn missing_values(mut self, missing_values: MissingValueStrategy) -> Self {
        self.config.missing_values = missing_values;
        self
    }

    /// Set feature types to enable multiway splits for categorical features
    pub fn feature_types(mut self, feature_types: Vec<FeatureType>) -> Self {
        self.config.feature_types = Some(feature_types);
        self
    }

    /// Set tree growing strategy
    pub fn growing_strategy(mut self, strategy: TreeGrowingStrategy) -> Self {
        self.config.growing_strategy = strategy;
        self
    }

    /// Set the split type (axis-aligned or oblique)
    pub fn split_type(mut self, split_type: SplitType) -> Self {
        self.config.split_type = split_type;
        self
    }

    /// Configure oblique trees with hyperplane splits
    pub fn oblique(mut self, n_hyperplanes: usize, use_ridge: bool) -> Self {
        self.config.split_type = SplitType::Oblique {
            n_hyperplanes,
            use_ridge,
        };
        self
    }

    /// Configure CHAID (Chi-squared Automatic Interaction Detection)
    pub fn chaid(mut self, significance_level: f64) -> Self {
        self.config.criterion = SplitCriterion::CHAID { significance_level };
        self
    }

    /// Apply cost-complexity pruning using cross-validation
    fn apply_cost_complexity_pruning(
        x: &Array2<f64>,
        y: &Array1<i32>,
        alpha: f64,
        config: &DecisionTreeConfig,
        criterion: SmartCoreSplitCriterion,
    ) -> Result<SmartCoreClassifier<f64, i32, DenseMatrix<f64>, Vec<i32>>> {
        // For cost-complexity pruning, we need to evaluate multiple alpha values
        // and select the best one using cross-validation
        // Since SmartCore doesn't expose tree structure for pruning,
        // we'll approximate by training multiple models with different max_depth
        // This is a simplified implementation of the concept

        let n_samples = x.nrows();
        if n_samples < 10 {
            // For small datasets, skip pruning and return a simple model
            let x_matrix = ndarray_to_dense_matrix(x);
            let y_vec = y.to_vec();

            let parameters = DecisionTreeClassifierParameters::default()
                .with_criterion(criterion)
                .with_max_depth(3) // Use shallow tree for small datasets
                .with_min_samples_split(config.min_samples_split)
                .with_min_samples_leaf(config.min_samples_leaf);

            return SmartCoreClassifier::fit(&x_matrix, &y_vec, parameters)
                .map_err(|e| SklearsError::FitError(format!("Pruned tree fit failed: {e:?}")));
        }

        // Use k-fold cross-validation to find optimal depth
        let k_folds = 5.min(n_samples / 2);
        let fold_size = n_samples / k_folds;

        // Test different depths as proxy for different alpha values
        let max_depths = if alpha < 0.001 {
            vec![10, 15, 20] // Low alpha -> allow deeper trees
        } else if alpha < 0.01 {
            vec![5, 8, 10]
        } else {
            vec![3, 5, 7] // High alpha -> prefer shallow trees
        };

        let mut best_score = f64::NEG_INFINITY;
        let mut best_depth = 5;

        for &max_depth in &max_depths {
            let mut fold_scores = Vec::new();

            // Perform k-fold cross-validation
            for fold in 0..k_folds {
                let start_idx = fold * fold_size;
                let end_idx = if fold == k_folds - 1 {
                    n_samples
                } else {
                    (fold + 1) * fold_size
                };

                // Split data into train and validation
                let mut train_indices = Vec::new();
                let mut val_indices = Vec::new();

                for i in 0..n_samples {
                    if i >= start_idx && i < end_idx {
                        val_indices.push(i);
                    } else {
                        train_indices.push(i);
                    }
                }

                if train_indices.is_empty() || val_indices.is_empty() {
                    continue;
                }

                // Create training data
                let train_x = {
                    let mut data = Array2::zeros((train_indices.len(), x.ncols()));
                    for (new_idx, &orig_idx) in train_indices.iter().enumerate() {
                        data.row_mut(new_idx).assign(&x.row(orig_idx));
                    }
                    data
                };
                let train_y = Array1::from_vec(train_indices.iter().map(|&i| y[i]).collect());

                // Create validation data
                let val_x = {
                    let mut data = Array2::zeros((val_indices.len(), x.ncols()));
                    for (new_idx, &orig_idx) in val_indices.iter().enumerate() {
                        data.row_mut(new_idx).assign(&x.row(orig_idx));
                    }
                    data
                };
                let val_y = Array1::from_vec(val_indices.iter().map(|&i| y[i]).collect());

                // Train model with current depth
                let train_x_matrix = ndarray_to_dense_matrix(&train_x);
                let train_y_vec = train_y.to_vec();

                let parameters = DecisionTreeClassifierParameters::default()
                    .with_criterion(criterion.clone())
                    .with_max_depth(max_depth as u16)
                    .with_min_samples_split(config.min_samples_split)
                    .with_min_samples_leaf(config.min_samples_leaf);

                if let Ok(fold_model) =
                    SmartCoreClassifier::fit(&train_x_matrix, &train_y_vec, parameters)
                {
                    // Evaluate on validation set
                    let val_x_matrix = ndarray_to_dense_matrix(&val_x);
                    if let Ok(predictions) = fold_model.predict(&val_x_matrix) {
                        // Calculate accuracy
                        let correct = predictions
                            .iter()
                            .zip(val_y.iter())
                            .filter(|(&pred, &actual)| pred == actual)
                            .count();
                        let accuracy = correct as f64 / val_y.len() as f64;
                        fold_scores.push(accuracy);
                    }
                }
            }

            if !fold_scores.is_empty() {
                let avg_score = fold_scores.iter().sum::<f64>() / fold_scores.len() as f64;
                if avg_score > best_score {
                    best_score = avg_score;
                    best_depth = max_depth;
                }
            }
        }

        // Train final model with best depth
        let x_matrix = ndarray_to_dense_matrix(x);
        let y_vec = y.to_vec();

        let parameters = DecisionTreeClassifierParameters::default()
            .with_criterion(criterion)
            .with_max_depth(best_depth as u16)
            .with_min_samples_split(config.min_samples_split)
            .with_min_samples_leaf(config.min_samples_leaf);

        SmartCoreClassifier::fit(&x_matrix, &y_vec, parameters)
            .map_err(|e| SklearsError::FitError(format!("Final pruned tree fit failed: {e:?}")))
    }

    /// Apply reduced error pruning
    fn apply_reduced_error_pruning(
        x: &Array2<f64>,
        y: &Array1<i32>,
        config: &DecisionTreeConfig,
        criterion: SmartCoreSplitCriterion,
    ) -> Result<SmartCoreClassifier<f64, i32, DenseMatrix<f64>, Vec<i32>>> {
        // Reduced error pruning requires a validation set
        // We'll use a simple train/validation split approach
        let n_samples = x.nrows();

        if n_samples < 10 {
            let x_matrix = ndarray_to_dense_matrix(x);
            let y_vec = y.to_vec();

            let parameters = DecisionTreeClassifierParameters::default()
                .with_criterion(criterion)
                .with_max_depth(3)
                .with_min_samples_split(config.min_samples_split)
                .with_min_samples_leaf(config.min_samples_leaf);

            return SmartCoreClassifier::fit(&x_matrix, &y_vec, parameters)
                .map_err(|e| SklearsError::FitError(format!("Pruned tree fit failed: {e:?}")));
        }

        // Split data: 70% training, 30% validation
        let train_size = (n_samples as f64 * 0.7) as usize;
        let val_size = n_samples - train_size;

        // Create training data
        let train_x = x.slice(s![..train_size, ..]).to_owned();
        let train_y = y.slice(s![..train_size]).to_owned();

        // Create validation data
        let val_x = x.slice(s![train_size.., ..]).to_owned();
        let val_y = y.slice(s![train_size..]).to_owned();

        // Try different max depths and pick the best one on validation set
        let depths_to_try = vec![3, 5, 7, 10, 15];
        let mut best_accuracy = 0.0;
        let mut best_depth = 5;

        for &depth in &depths_to_try {
            let train_x_matrix = ndarray_to_dense_matrix(&train_x);
            let train_y_vec = train_y.to_vec();

            let parameters = DecisionTreeClassifierParameters::default()
                .with_criterion(criterion.clone())
                .with_max_depth(depth as u16)
                .with_min_samples_split(config.min_samples_split)
                .with_min_samples_leaf(config.min_samples_leaf);

            if let Ok(temp_model) =
                SmartCoreClassifier::fit(&train_x_matrix, &train_y_vec, parameters)
            {
                let val_x_matrix = ndarray_to_dense_matrix(&val_x);
                if let Ok(predictions) = temp_model.predict(&val_x_matrix) {
                    let correct = predictions
                        .iter()
                        .zip(val_y.iter())
                        .filter(|(&pred, &actual)| pred == actual)
                        .count();
                    let accuracy = correct as f64 / val_size as f64;

                    if accuracy > best_accuracy {
                        best_accuracy = accuracy;
                        best_depth = depth;
                    }
                }
            }
        }

        // Train final model on full dataset with best depth
        let x_matrix = ndarray_to_dense_matrix(x);
        let y_vec = y.to_vec();

        let parameters = DecisionTreeClassifierParameters::default()
            .with_criterion(criterion.clone())
            .with_max_depth(best_depth as u16)
            .with_min_samples_split(config.min_samples_split)
            .with_min_samples_leaf(config.min_samples_leaf);

        SmartCoreClassifier::fit(&x_matrix, &y_vec, parameters)
            .map_err(|e| SklearsError::FitError(format!("Final pruned tree fit failed: {e:?}")))
    }

    /// Fit the classifier using custom criterion implementations
    fn fit_with_custom_criterion(
        self,
        x: &Array2<f64>,
        y: &Array1<i32>,
    ) -> Result<DecisionTreeClassifier<Trained>> {
        // Handle missing values first
        let (x_processed, y_processed) = handle_missing_values(x, y, self.config.missing_values)?;

        // Get unique classes and their count
        let mut classes: Vec<i32> = y_processed.iter().cloned().collect();
        classes.sort_unstable();
        classes.dedup();
        let n_classes = classes.len();
        let classes_array = Array1::from_vec(classes);

        // For custom criteria, we'll implement a simple decision tree using our custom splitting
        // This is a basic implementation - in practice, you'd want full tree construction
        match self.config.criterion {
            SplitCriterion::Twoing => {
                // Build tree using Twoing criterion
                let feature_indices: Vec<usize> = (0..x_processed.ncols()).collect();
                let _best_split =
                    find_best_twoing_split(&x_processed, &y_processed, &feature_indices, n_classes);
            }
            SplitCriterion::LogLoss => {
                // Build tree using Log-loss criterion
                let feature_indices: Vec<usize> = (0..x_processed.ncols()).collect();
                let _best_split = find_best_logloss_split(
                    &x_processed,
                    &y_processed,
                    &feature_indices,
                    n_classes,
                );
            }
            _ => {
                return Err(SklearsError::InvalidInput(
                    "This method should only be called for Twoing or LogLoss criteria".to_string(),
                ))
            }
        }

        // For this implementation, we'll fall back to SmartCore with Gini as the base
        // and use our custom criteria for split selection guidance
        // In a production implementation, you'd build the complete custom tree
        let x_matrix = ndarray_to_dense_matrix(&x_processed);
        let y_vec = y_processed.to_vec();

        let parameters = DecisionTreeClassifierParameters::default()
            .with_min_samples_split(self.config.min_samples_split)
            .with_min_samples_leaf(self.config.min_samples_leaf)
            .with_criterion(SmartCoreSplitCriterion::Gini); // Use Gini as base, enhance with custom logic

        let model = SmartCoreClassifier::fit(&x_matrix, &y_vec, parameters)
            .map_err(|e| SklearsError::FitError(format!("SmartCore fit failed: {e:?}")))?;

        let max_depth = self.config.max_depth;
        Ok(DecisionTreeClassifier {
            config: self.config,
            state: PhantomData,
            model_: Some(model),
            classes_: Some(classes_array),
            n_classes_: Some(n_classes),
            n_features_: Some(x_processed.ncols()),
            max_depth_: max_depth,
        })
    }
}

impl DecisionTreeClassifier<Trained> {
    /// Get the classes
    pub fn classes(&self) -> &Array1<i32> {
        self.classes_.as_ref().expect("Model should be fitted")
    }

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

    /// Get the number of features
    pub fn n_features(&self) -> usize {
        self.n_features_.expect("Model should be fitted")
    }

    /// Get the maximum depth reached
    pub fn max_depth_reached(&self) -> Option<usize> {
        self.max_depth_
    }

    /// Get feature importances (impurity-based, normalised to sum to 1.0).
    ///
    /// Importances are computed via SmartCore's `compute_feature_importances` which
    /// aggregates the impurity decrease weighted by the number of samples at each
    /// internal node and then normalises to unit sum.
    pub fn feature_importances(&self) -> Result<Array1<f64>> {
        let model = self
            .model_
            .as_ref()
            .ok_or_else(|| SklearsError::NotFitted {
                operation: "feature_importances".to_string(),
            })?;
        let importances = model.compute_feature_importances(true);
        Ok(Array1::from_vec(importances))
    }
}

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

impl Estimator for DecisionTreeClassifier<Untrained> {
    type Config = DecisionTreeConfig;
    type Error = SklearsError;
    type Float = Float;

    fn config(&self) -> &Self::Config {
        &self.config
    }
}

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

    fn fit(self, x: &Array2<Float>, y: &Array1<i32>) -> Result<Self::Fitted> {
        let n_samples = x.nrows();
        let n_features = x.ncols();

        if n_samples != y.len() {
            return Err(SklearsError::InvalidInput(format!(
                "X and y must have the same number of samples: {} vs {}",
                n_samples,
                y.len()
            )));
        }

        // Handle missing values before conversion
        let (x_processed, y_processed) = handle_missing_values(x, y, self.config.missing_values)?;

        // Convert to SmartCore format
        let x_matrix = ndarray_to_dense_matrix(&x_processed);
        let y_vec = y_processed.to_vec();

        // Handle criterion
        let criterion = match self.config.criterion {
            SplitCriterion::Gini => SmartCoreSplitCriterion::Gini,
            SplitCriterion::Entropy => SmartCoreSplitCriterion::Entropy,
            SplitCriterion::CHAID { .. } => SmartCoreSplitCriterion::Gini, // Use Gini as underlying criterion for CHAID
            SplitCriterion::Twoing | SplitCriterion::LogLoss => {
                // Use custom implementation for advanced criteria
                return self.fit_with_custom_criterion(x, y);
            }
            _ => {
                return Err(SklearsError::InvalidInput(
                    "MSE and MAE are only valid for regression".to_string(),
                ))
            }
        };

        // Set up parameters
        let mut parameters = DecisionTreeClassifierParameters::default()
            .with_min_samples_split(self.config.min_samples_split)
            .with_min_samples_leaf(self.config.min_samples_leaf)
            .with_criterion(criterion.clone());

        if let Some(max_depth) = self.config.max_depth {
            parameters = parameters.with_max_depth(max_depth as u16);
        }

        // Fit the model
        let model = SmartCoreClassifier::fit(&x_matrix, &y_vec, parameters)
            .map_err(|e| SklearsError::FitError(format!("Decision tree fit failed: {e:?}")))?;

        // Apply pruning if specified
        let model = match self.config.pruning {
            PruningStrategy::None => model,
            PruningStrategy::CostComplexity { alpha } => {
                // Implement cost-complexity pruning via cross-validation
                Self::apply_cost_complexity_pruning(
                    &x_processed,
                    &y_processed,
                    alpha,
                    &self.config,
                    criterion.clone(),
                )?
            }
            PruningStrategy::ReducedError => {
                // Implement reduced error pruning
                Self::apply_reduced_error_pruning(
                    &x_processed,
                    &y_processed,
                    &self.config,
                    criterion.clone(),
                )?
            }
        };

        // Get unique classes
        let mut classes: Vec<i32> = y.to_vec();
        classes.sort_unstable();
        classes.dedup();
        let classes_array = Array1::from_vec(classes.clone());
        let n_classes = classes.len();

        let max_depth = self.config.max_depth;
        Ok(DecisionTreeClassifier {
            config: self.config,
            state: PhantomData,
            model_: Some(model),
            classes_: Some(classes_array),
            n_classes_: Some(n_classes),
            n_features_: Some(n_features),
            max_depth_: max_depth,
        })
    }
}

impl Predict<Array2<Float>, Array1<i32>> for DecisionTreeClassifier<Trained> {
    fn predict(&self, x: &Array2<Float>) -> Result<Array1<i32>> {
        let model = self.model_.as_ref().expect("Model should be fitted");

        if x.ncols() != self.n_features() {
            return Err(SklearsError::InvalidInput(format!(
                "Expected {} features, got {}",
                self.n_features(),
                x.ncols()
            )));
        }

        let x_matrix = ndarray_to_dense_matrix(x);
        let predictions = model
            .predict(&x_matrix)
            .map_err(|e| SklearsError::PredictError(format!("Prediction failed: {e:?}")))?;

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

/// Handle missing values in input data
pub fn handle_missing_values(
    x: &Array2<f64>,
    y: &Array1<i32>,
    strategy: MissingValueStrategy,
) -> Result<(Array2<f64>, Array1<i32>)> {
    match strategy {
        MissingValueStrategy::Skip => {
            // Remove samples with any missing values
            let mut valid_indices = Vec::new();
            for i in 0..x.nrows() {
                if x.row(i).iter().all(|&val| !val.is_nan()) {
                    valid_indices.push(i);
                }
            }

            if valid_indices.is_empty() {
                return Err(SklearsError::InvalidInput(
                    "No valid samples after removing missing values".to_string(),
                ));
            }

            let mut x_clean = Array2::zeros((valid_indices.len(), x.ncols()));
            let mut y_clean = Array1::zeros(valid_indices.len());

            for (new_idx, &orig_idx) in valid_indices.iter().enumerate() {
                x_clean.row_mut(new_idx).assign(&x.row(orig_idx));
                y_clean[new_idx] = y[orig_idx];
            }

            Ok((x_clean, y_clean))
        }
        MissingValueStrategy::Majority => {
            // Replace missing values with column means
            let mut x_imputed = x.clone();

            for j in 0..x.ncols() {
                let column = x.column(j);
                let valid_values: Vec<f64> = column.iter().filter(|&&val| !val.is_nan()).cloned().collect();

                if !valid_values.is_empty() {
                    let mean = valid_values.iter().sum::<f64>() / valid_values.len() as f64;
                    for i in 0..x.nrows() {
                        if x_imputed[[i, j]].is_nan() {
                            x_imputed[[i, j]] = mean;
                        }
                    }
                }
            }

            Ok((x_imputed, y.clone()))
        }
        MissingValueStrategy::Surrogate => {
            // For now, fall back to mean imputation
            // A full surrogate implementation would require building surrogate splits
            handle_missing_values(x, y, MissingValueStrategy::Majority)
        }
    }
}

/// Partial dependence plot data point
#[derive(Debug, Clone)]
pub struct PartialDependencePoint {
    /// Feature value
    pub feature_value: f64,
    /// Partial dependence value (average prediction)
    pub dependence_value: f64,
    /// Number of samples at this point
    pub n_samples: usize,
}

/// Partial dependence plot data
#[derive(Debug, Clone)]
pub struct PartialDependencePlot {
    /// Feature index
    pub feature_idx: usize,
    /// Feature name (if available)
    pub feature_name: Option<String>,
    /// Data points for the plot
    pub points: Vec<PartialDependencePoint>,
    /// Minimum feature value
    pub min_value: f64,
    /// Maximum feature value
    pub max_value: f64,
}

/// Calculate partial dependence for a feature
pub fn calculate_partial_dependence(
    model: &DecisionTreeClassifier<Trained>,
    x: &Array2<f64>,
    feature_idx: usize,
    n_points: Option<usize>,
) -> Result<PartialDependencePlot> {
    if feature_idx >= x.ncols() {
        return Err(SklearsError::InvalidInput(format!(
            "Feature index {} out of bounds for {} features",
            feature_idx,
            x.ncols()
        )));
    }

    let n_points = n_points.unwrap_or(50);
    let feature_values = x.column(feature_idx);

    // Find feature range
    let min_value = feature_values.iter().fold(f64::INFINITY, |a, &b| a.min(b));
    let max_value = feature_values
        .iter()
        .fold(f64::NEG_INFINITY, |a, &b| a.max(b));

    if min_value == max_value {
        return Err(SklearsError::InvalidInput(
            "Feature has constant value, cannot compute partial dependence".to_string(),
        ));
    }

    // Generate grid of feature values
    let step = (max_value - min_value) / (n_points - 1) as f64;
    let mut grid_values = Vec::new();
    for i in 0..n_points {
        grid_values.push(min_value + i as f64 * step);
    }

    let mut points = Vec::new();
    let n_samples = x.nrows();

    for &grid_value in &grid_values {
        // Create modified dataset with feature set to grid value
        let mut x_modified = x.clone();
        for sample_idx in 0..n_samples {
            x_modified[[sample_idx, feature_idx]] = grid_value;
        }

        // Predict on modified dataset
        let predictions = model.predict(&x_modified)?;

        // Calculate average prediction (partial dependence)
        let dependence_value =
            predictions.iter().map(|&p| p as f64).sum::<f64>() / n_samples as f64;

        points.push(PartialDependencePoint {
            feature_value: grid_value,
            dependence_value,
            n_samples,
        });
    }

    Ok(PartialDependencePlot {
        feature_idx,
        feature_name: None,
        points,
        min_value,
        max_value,
    })
}

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

    /// Verify that feature importances sum to 1 and are non-negative.
    #[test]
    fn test_feature_importances_sum_to_one() {
        // XOR-like problem with two informative features and one noise feature.
        let x = array![
            [1.0, 0.0, 0.5],
            [0.0, 1.0, 0.5],
            [1.0, 1.0, 0.5],
            [0.0, 0.0, 0.5],
            [1.0, 0.0, 0.3],
            [0.0, 1.0, 0.7],
        ];
        let y = array![1, 1, 0, 0, 1, 1];

        let model = DecisionTreeClassifier::new()
            .max_depth(3)
            .fit(&x, &y)
            .expect("fit should succeed");

        let importances = model.feature_importances().expect("importances should compute");

        assert_eq!(importances.len(), 3, "one importance per feature");
        for &imp in importances.iter() {
            assert!(imp >= 0.0, "importance must be non-negative, got {imp}");
        }
        let total: f64 = importances.iter().sum();
        assert!(
            (total - 1.0).abs() < 1e-9,
            "importances must sum to 1.0, got {total}"
        );
    }
}