sklears-feature-selection 0.2.0

Feature selection algorithms for sklears - scikit-learn compatible ML in Rust
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
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
//! Relevance scoring methods for feature selection evaluation
//!
//! This module implements comprehensive relevance scoring methods to evaluate
//! how relevant selected features are to the target variable. All implementations
//! follow the SciRS2 policy using scirs2-core for numerical computations.

use scirs2_core::ndarray::{Array1, ArrayView1, ArrayView2};
use sklears_core::error::{Result as SklResult, SklearsError};
type Result<T> = SklResult<T>;

impl From<RelevanceError> for SklearsError {
    fn from(err: RelevanceError) -> Self {
        SklearsError::FitError(format!("Relevance analysis error: {}", err))
    }
}
use scirs2_core::random::thread_rng;
use std::collections::HashMap;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum RelevanceError {
    #[error("Feature matrix is empty")]
    EmptyFeatureMatrix,
    #[error("Target array is empty")]
    EmptyTarget,
    #[error("Feature and target lengths do not match")]
    LengthMismatch,
    #[error("Invalid feature indices")]
    InvalidFeatureIndices,
    #[error("Insufficient variance in data")]
    InsufficientVariance,
}

/// Information gain-based relevance scoring
#[derive(Debug, Clone)]
pub struct InformationGainScoring {
    n_bins: usize,
    use_equal_width: bool,
}

impl InformationGainScoring {
    /// Create a new information gain scorer
    pub fn new(n_bins: usize, use_equal_width: bool) -> Self {
        Self {
            n_bins,
            use_equal_width,
        }
    }

    /// Compute information gain for selected features
    pub fn compute(
        &self,
        X: ArrayView2<f64>,
        y: ArrayView1<f64>,
        feature_indices: &[usize],
    ) -> Result<Vec<f64>> {
        if X.nrows() != y.len() {
            return Err(RelevanceError::LengthMismatch.into());
        }

        if X.is_empty() || y.is_empty() {
            return Err(RelevanceError::EmptyFeatureMatrix.into());
        }

        let mut scores = Vec::with_capacity(feature_indices.len());

        // Discretize target if it's continuous
        let discretized_target = self.discretize_target(y)?;

        for &feature_idx in feature_indices {
            if feature_idx >= X.ncols() {
                return Err(RelevanceError::InvalidFeatureIndices.into());
            }

            let feature_column = X.column(feature_idx);
            let discretized_feature = self.discretize_feature(feature_column)?;

            let ig = self.compute_information_gain(&discretized_feature, &discretized_target)?;
            scores.push(ig);
        }

        Ok(scores)
    }

    /// Discretize target variable
    fn discretize_target(&self, target: ArrayView1<f64>) -> Result<Array1<i32>> {
        // Check if target is already discrete (all integers)
        let is_discrete = target.iter().all(|&x| x.fract() == 0.0);

        if is_discrete {
            // Convert to integers
            return Ok(target.mapv(|x| x as i32));
        }

        // Discretize continuous target
        self.discretize_continuous(target)
    }

    /// Discretize feature variable
    fn discretize_feature(&self, feature: ArrayView1<f64>) -> Result<Array1<i32>> {
        // Check if feature is already discrete (all integers)
        let is_discrete = feature.iter().all(|&x| x.fract() == 0.0);

        if is_discrete {
            // Convert to integers, ensuring non-negative
            let min_val = feature.iter().fold(f64::INFINITY, |acc, &x| acc.min(x)) as i32;
            return Ok(feature.mapv(|x| x as i32 - min_val));
        }

        // Discretize continuous feature
        self.discretize_continuous(feature)
    }

    /// Discretize continuous variable using binning
    fn discretize_continuous(&self, values: ArrayView1<f64>) -> Result<Array1<i32>> {
        let min_val = values.iter().fold(f64::INFINITY, |acc, &x| acc.min(x));
        let max_val = values.iter().fold(f64::NEG_INFINITY, |acc, &x| acc.max(x));

        if (max_val - min_val).abs() < 1e-10 {
            // Constant feature
            return Ok(Array1::zeros(values.len()));
        }

        if self.use_equal_width {
            self.equal_width_binning(values, min_val, max_val)
        } else {
            self.equal_frequency_binning(values)
        }
    }

    /// Equal width binning
    fn equal_width_binning(
        &self,
        values: ArrayView1<f64>,
        min_val: f64,
        max_val: f64,
    ) -> Result<Array1<i32>> {
        let bin_width = (max_val - min_val) / self.n_bins as f64;
        let mut discretized = Array1::zeros(values.len());

        for (i, &value) in values.iter().enumerate() {
            let bin = ((value - min_val) / bin_width).floor() as i32;
            discretized[i] = bin.min((self.n_bins - 1) as i32).max(0);
        }

        Ok(discretized)
    }

    /// Equal frequency binning (quantile-based)
    fn equal_frequency_binning(&self, values: ArrayView1<f64>) -> Result<Array1<i32>> {
        let mut sorted_values: Vec<f64> = values.to_vec();
        sorted_values.sort_by(|a, b| a.partial_cmp(b).expect("operation should succeed"));

        let n = values.len();
        let bin_size = n / self.n_bins;
        let mut discretized = Array1::zeros(n);

        for (i, &value) in values.iter().enumerate() {
            // Find which quantile this value belongs to
            let rank = sorted_values.partition_point(|&x| x < value);
            let bin = (rank / bin_size.max(1)).min(self.n_bins - 1);
            discretized[i] = bin as i32;
        }

        Ok(discretized)
    }

    /// Compute information gain between feature and target
    fn compute_information_gain(&self, feature: &Array1<i32>, target: &Array1<i32>) -> Result<f64> {
        let target_entropy = self.compute_entropy(target)?;
        let conditional_entropy = self.compute_conditional_entropy(feature, target)?;

        Ok(target_entropy - conditional_entropy)
    }

    /// Compute entropy of a discrete variable
    fn compute_entropy(&self, values: &Array1<i32>) -> Result<f64> {
        let mut counts = HashMap::new();
        let total = values.len() as f64;

        for &value in values.iter() {
            *counts.entry(value).or_insert(0) += 1;
        }

        let mut entropy = 0.0;
        for count in counts.values() {
            if *count > 0 {
                let probability = *count as f64 / total;
                entropy -= probability * probability.ln();
            }
        }

        Ok(entropy)
    }

    /// Compute conditional entropy H(Y|X)
    fn compute_conditional_entropy(
        &self,
        feature: &Array1<i32>,
        target: &Array1<i32>,
    ) -> Result<f64> {
        let mut joint_counts = HashMap::new();
        let mut feature_counts = HashMap::new();
        let total = feature.len() as f64;

        // Count joint occurrences and marginal counts
        for i in 0..feature.len() {
            let x_val = feature[i];
            let y_val = target[i];

            *joint_counts.entry((x_val, y_val)).or_insert(0) += 1;
            *feature_counts.entry(x_val).or_insert(0) += 1;
        }

        let mut conditional_entropy = 0.0;

        // For each value of X
        for (&x_val, &x_count) in feature_counts.iter() {
            if x_count == 0 {
                continue;
            }

            let p_x = x_count as f64 / total;

            // Compute H(Y | X = x_val)
            let mut entropy_y_given_x = 0.0;
            for (&(joint_x, _joint_y), &joint_count) in joint_counts.iter() {
                if joint_x == x_val && joint_count > 0 {
                    let p_y_given_x = joint_count as f64 / x_count as f64;
                    entropy_y_given_x -= p_y_given_x * p_y_given_x.ln();
                }
            }

            conditional_entropy += p_x * entropy_y_given_x;
        }

        Ok(conditional_entropy)
    }

    /// Compute average information gain for selected features
    pub fn average_score(
        &self,
        X: ArrayView2<f64>,
        y: ArrayView1<f64>,
        feature_indices: &[usize],
    ) -> Result<f64> {
        let scores = self.compute(X, y, feature_indices)?;
        Ok(scores.iter().sum::<f64>() / scores.len() as f64)
    }
}

/// Chi-square-based relevance scoring
#[derive(Debug, Clone)]
pub struct ChiSquareScoring {
    n_bins: usize,
}

impl ChiSquareScoring {
    /// Create a new chi-square scorer
    pub fn new(n_bins: usize) -> Self {
        Self { n_bins }
    }

    /// Compute chi-square statistics for selected features
    pub fn compute(
        &self,
        X: ArrayView2<f64>,
        y: ArrayView1<f64>,
        feature_indices: &[usize],
    ) -> Result<Vec<f64>> {
        if X.nrows() != y.len() {
            return Err(RelevanceError::LengthMismatch.into());
        }

        let mut scores = Vec::with_capacity(feature_indices.len());

        // Discretize target
        let discretized_target = self.discretize_variable(y)?;

        for &feature_idx in feature_indices {
            if feature_idx >= X.ncols() {
                return Err(RelevanceError::InvalidFeatureIndices.into());
            }

            let feature_column = X.column(feature_idx);
            let discretized_feature = self.discretize_variable(feature_column)?;

            let chi2 = self.compute_chi_square(&discretized_feature, &discretized_target)?;
            scores.push(chi2);
        }

        Ok(scores)
    }

    /// Discretize variable for chi-square test
    fn discretize_variable(&self, values: ArrayView1<f64>) -> Result<Array1<i32>> {
        let min_val = values.iter().fold(f64::INFINITY, |acc, &x| acc.min(x));
        let max_val = values.iter().fold(f64::NEG_INFINITY, |acc, &x| acc.max(x));

        if (max_val - min_val).abs() < 1e-10 {
            return Ok(Array1::zeros(values.len()));
        }

        let bin_width = (max_val - min_val) / self.n_bins as f64;
        let mut discretized = Array1::zeros(values.len());

        for (i, &value) in values.iter().enumerate() {
            let bin = ((value - min_val) / bin_width).floor() as i32;
            discretized[i] = bin.min((self.n_bins - 1) as i32).max(0);
        }

        Ok(discretized)
    }

    /// Compute chi-square statistic
    fn compute_chi_square(&self, feature: &Array1<i32>, target: &Array1<i32>) -> Result<f64> {
        // Create contingency table
        let mut joint_counts = HashMap::new();
        let mut feature_counts = HashMap::new();
        let mut target_counts = HashMap::new();
        let total = feature.len() as f64;

        for i in 0..feature.len() {
            let x = feature[i];
            let y = target[i];

            *joint_counts.entry((x, y)).or_insert(0) += 1;
            *feature_counts.entry(x).or_insert(0) += 1;
            *target_counts.entry(y).or_insert(0) += 1;
        }

        let mut chi_square = 0.0;

        for (&(x, y), &observed) in joint_counts.iter() {
            let x_count = *feature_counts.get(&x).unwrap_or(&0) as f64;
            let y_count = *target_counts.get(&y).unwrap_or(&0) as f64;

            let expected = (x_count * y_count) / total;

            if expected > 1e-10 {
                let diff = observed as f64 - expected;
                chi_square += (diff * diff) / expected;
            }
        }

        Ok(chi_square)
    }

    /// Compute average chi-square score
    pub fn average_score(
        &self,
        X: ArrayView2<f64>,
        y: ArrayView1<f64>,
        feature_indices: &[usize],
    ) -> Result<f64> {
        let scores = self.compute(X, y, feature_indices)?;
        Ok(scores.iter().sum::<f64>() / scores.len() as f64)
    }
}

/// F-statistic-based relevance scoring
#[derive(Debug, Clone)]
pub struct FStatisticScoring {
    classification: bool,
}

impl FStatisticScoring {
    /// Create a new F-statistic scorer
    pub fn new(classification: bool) -> Self {
        Self { classification }
    }

    /// Compute F-statistics for selected features
    pub fn compute(
        &self,
        X: ArrayView2<f64>,
        y: ArrayView1<f64>,
        feature_indices: &[usize],
    ) -> Result<Vec<f64>> {
        if X.nrows() != y.len() {
            return Err(RelevanceError::LengthMismatch.into());
        }

        let mut scores = Vec::with_capacity(feature_indices.len());

        for &feature_idx in feature_indices {
            if feature_idx >= X.ncols() {
                return Err(RelevanceError::InvalidFeatureIndices.into());
            }

            let feature_column = X.column(feature_idx);

            let f_stat = if self.classification {
                self.compute_f_classif(feature_column, y)?
            } else {
                self.compute_f_regression(feature_column, y)?
            };

            scores.push(f_stat);
        }

        Ok(scores)
    }

    /// Compute F-statistic for classification
    fn compute_f_classif(&self, feature: ArrayView1<f64>, target: ArrayView1<f64>) -> Result<f64> {
        // Group feature values by class
        let mut class_groups: HashMap<i32, Vec<f64>> = HashMap::new();

        for i in 0..feature.len() {
            let class = target[i] as i32;
            class_groups.entry(class).or_default().push(feature[i]);
        }

        if class_groups.len() < 2 {
            return Ok(0.0); // No variation between classes
        }

        // Compute overall mean
        let overall_mean = feature.mean().unwrap_or(0.0);
        let total_n = feature.len() as f64;

        // Compute between-group sum of squares
        let mut ss_between = 0.0;
        for group in class_groups.values() {
            let group_mean = group.iter().sum::<f64>() / group.len() as f64;
            let n_group = group.len() as f64;
            ss_between += n_group * (group_mean - overall_mean).powi(2);
        }

        // Compute within-group sum of squares
        let mut ss_within = 0.0;
        for group in class_groups.values() {
            let group_mean = group.iter().sum::<f64>() / group.len() as f64;
            for &value in group {
                ss_within += (value - group_mean).powi(2);
            }
        }

        let df_between = (class_groups.len() - 1) as f64;
        let df_within = (total_n - class_groups.len() as f64).max(1.0);

        if ss_within < 1e-10 {
            return Ok(f64::INFINITY);
        }

        let ms_between = ss_between / df_between;
        let ms_within = ss_within / df_within;

        Ok(ms_between / ms_within)
    }

    /// Compute F-statistic for regression
    fn compute_f_regression(
        &self,
        feature: ArrayView1<f64>,
        target: ArrayView1<f64>,
    ) -> Result<f64> {
        let n = feature.len() as f64;
        if n < 3.0 {
            return Ok(0.0);
        }

        // Compute correlation coefficient
        let mean_x = feature.mean().unwrap_or(0.0);
        let mean_y = target.mean().unwrap_or(0.0);

        let mut sum_xy = 0.0;
        let mut sum_xx = 0.0;
        let mut sum_yy = 0.0;

        for i in 0..feature.len() {
            let dx = feature[i] - mean_x;
            let dy = target[i] - mean_y;
            sum_xy += dx * dy;
            sum_xx += dx * dx;
            sum_yy += dy * dy;
        }

        if sum_xx < 1e-10 || sum_yy < 1e-10 {
            return Ok(0.0);
        }

        let r = sum_xy / (sum_xx * sum_yy).sqrt();
        let r_squared = r * r;

        // F-statistic for regression: F = (r²/(1-r²)) * (n-2)
        if (1.0 - r_squared).abs() < 1e-10 {
            return Ok(f64::INFINITY);
        }

        Ok((r_squared / (1.0 - r_squared)) * (n - 2.0))
    }

    /// Compute average F-statistic score
    pub fn average_score(
        &self,
        X: ArrayView2<f64>,
        y: ArrayView1<f64>,
        feature_indices: &[usize],
    ) -> Result<f64> {
        let scores = self.compute(X, y, feature_indices)?;
        Ok(scores.iter().sum::<f64>() / scores.len() as f64)
    }
}

/// Correlation-based relevance scoring
#[derive(Debug, Clone)]
pub struct CorrelationScoring {
    use_absolute: bool,
}

impl CorrelationScoring {
    /// Create a new correlation scorer
    pub fn new(use_absolute: bool) -> Self {
        Self { use_absolute }
    }

    /// Compute correlations for selected features
    pub fn compute(
        &self,
        X: ArrayView2<f64>,
        y: ArrayView1<f64>,
        feature_indices: &[usize],
    ) -> Result<Vec<f64>> {
        if X.nrows() != y.len() {
            return Err(RelevanceError::LengthMismatch.into());
        }

        let mut scores = Vec::with_capacity(feature_indices.len());

        for &feature_idx in feature_indices {
            if feature_idx >= X.ncols() {
                return Err(RelevanceError::InvalidFeatureIndices.into());
            }

            let feature_column = X.column(feature_idx);
            let correlation = self.compute_correlation(feature_column, y)?;

            scores.push(if self.use_absolute {
                correlation.abs()
            } else {
                correlation
            });
        }

        Ok(scores)
    }

    /// Compute Pearson correlation coefficient
    fn compute_correlation(&self, x: ArrayView1<f64>, y: ArrayView1<f64>) -> Result<f64> {
        let n = x.len() as f64;
        if n < 2.0 {
            return Ok(0.0);
        }

        let mean_x = x.mean().unwrap_or(0.0);
        let mean_y = y.mean().unwrap_or(0.0);

        let mut sum_xy = 0.0;
        let mut sum_x2 = 0.0;
        let mut sum_y2 = 0.0;

        for i in 0..x.len() {
            let dx = x[i] - mean_x;
            let dy = y[i] - mean_y;
            sum_xy += dx * dy;
            sum_x2 += dx * dx;
            sum_y2 += dy * dy;
        }

        let denom = (sum_x2 * sum_y2).sqrt();
        if denom < 1e-10 {
            return Ok(0.0);
        }

        Ok(sum_xy / denom)
    }

    /// Compute average correlation score
    pub fn average_score(
        &self,
        X: ArrayView2<f64>,
        y: ArrayView1<f64>,
        feature_indices: &[usize],
    ) -> Result<f64> {
        let scores = self.compute(X, y, feature_indices)?;
        Ok(scores.iter().sum::<f64>() / scores.len() as f64)
    }
}

/// Relief algorithm-based relevance scoring
#[derive(Debug, Clone)]
pub struct ReliefScoring {
    n_iterations: usize,
    _k_neighbors: usize,
}

impl ReliefScoring {
    /// Create a new Relief scorer
    pub fn new(n_iterations: usize, k_neighbors: usize) -> Self {
        Self {
            n_iterations,
            _k_neighbors: k_neighbors,
        }
    }

    /// Compute Relief scores for selected features
    pub fn compute(
        &self,
        X: ArrayView2<f64>,
        y: ArrayView1<f64>,
        feature_indices: &[usize],
    ) -> Result<Vec<f64>> {
        if X.nrows() != y.len() {
            return Err(RelevanceError::LengthMismatch.into());
        }

        if X.is_empty() || y.is_empty() {
            return Err(RelevanceError::EmptyFeatureMatrix.into());
        }

        let mut feature_weights = vec![0.0; feature_indices.len()];

        // Relief algorithm iterations
        for _ in 0..self.n_iterations {
            // Randomly select an instance
            let random_idx = (thread_rng().random::<f64>() * X.nrows() as f64) as usize % X.nrows();

            let random_instance = X.row(random_idx);
            let random_class = y[random_idx];

            // Find nearest hit and miss
            let (nearest_hit_idx, nearest_miss_idx) =
                self.find_nearest_hit_miss(X, y, random_idx, random_class)?;

            if let Some(hit_idx) = nearest_hit_idx {
                let hit_instance = X.row(hit_idx);
                for (i, &feature_idx) in feature_indices.iter().enumerate() {
                    if feature_idx < X.ncols() {
                        let diff = (random_instance[feature_idx] - hit_instance[feature_idx]).abs();
                        feature_weights[i] -= diff;
                    }
                }
            }

            if let Some(miss_idx) = nearest_miss_idx {
                let miss_instance = X.row(miss_idx);
                for (i, &feature_idx) in feature_indices.iter().enumerate() {
                    if feature_idx < X.ncols() {
                        let diff =
                            (random_instance[feature_idx] - miss_instance[feature_idx]).abs();
                        feature_weights[i] += diff;
                    }
                }
            }
        }

        // Normalize weights
        let n_iterations = self.n_iterations as f64;
        for weight in &mut feature_weights {
            *weight /= n_iterations;
        }

        Ok(feature_weights)
    }

    /// Find nearest hit (same class) and miss (different class)
    fn find_nearest_hit_miss(
        &self,
        X: ArrayView2<f64>,
        y: ArrayView1<f64>,
        instance_idx: usize,
        instance_class: f64,
    ) -> Result<(Option<usize>, Option<usize>)> {
        let mut nearest_hit_idx = None;
        let mut nearest_miss_idx = None;
        let mut min_hit_distance = f64::INFINITY;
        let mut min_miss_distance = f64::INFINITY;

        let instance = X.row(instance_idx);

        for i in 0..X.nrows() {
            if i == instance_idx {
                continue;
            }

            let other_instance = X.row(i);
            let other_class = y[i];

            // Compute Euclidean distance
            let mut distance = 0.0;
            for j in 0..X.ncols() {
                let diff = instance[j] - other_instance[j];
                distance += diff * diff;
            }
            distance = distance.sqrt();

            // Check if it's a hit or miss
            if (other_class - instance_class).abs() < 1e-10 {
                // Same class (hit)
                if distance < min_hit_distance {
                    min_hit_distance = distance;
                    nearest_hit_idx = Some(i);
                }
            } else {
                // Different class (miss)
                if distance < min_miss_distance {
                    min_miss_distance = distance;
                    nearest_miss_idx = Some(i);
                }
            }
        }

        Ok((nearest_hit_idx, nearest_miss_idx))
    }

    /// Compute average Relief score
    pub fn average_score(
        &self,
        X: ArrayView2<f64>,
        y: ArrayView1<f64>,
        feature_indices: &[usize],
    ) -> Result<f64> {
        let scores = self.compute(X, y, feature_indices)?;
        Ok(scores.iter().sum::<f64>() / scores.len() as f64)
    }
}

/// Comprehensive relevance scoring aggregator
#[derive(Debug, Clone)]
pub struct RelevanceScoring {
    classification: bool,
}

impl RelevanceScoring {
    /// Create a new relevance scorer
    pub fn new(classification: bool) -> Self {
        Self { classification }
    }

    /// Compute comprehensive relevance assessment
    pub fn compute(
        &self,
        X: ArrayView2<f64>,
        y: ArrayView1<f64>,
        feature_indices: &[usize],
    ) -> Result<RelevanceAssessment> {
        let information_gain = InformationGainScoring::new(10, true);
        let chi_square = ChiSquareScoring::new(10);
        let f_statistic = FStatisticScoring::new(self.classification);
        let correlation = CorrelationScoring::new(true);
        let relief = ReliefScoring::new(100, 5);

        let ig_scores = information_gain.compute(X, y, feature_indices)?;
        let chi2_scores = chi_square.compute(X, y, feature_indices)?;
        let f_scores = f_statistic.compute(X, y, feature_indices)?;
        let corr_scores = correlation.compute(X, y, feature_indices)?;
        let relief_scores = relief.compute(X, y, feature_indices)?;

        Ok(RelevanceAssessment {
            information_gain_scores: ig_scores,
            chi_square_scores: chi2_scores,
            f_statistic_scores: f_scores,
            correlation_scores: corr_scores,
            relief_scores,
            feature_indices: feature_indices.to_vec(),
            average_information_gain: information_gain.average_score(X, y, feature_indices)?,
            average_chi_square: chi_square.average_score(X, y, feature_indices)?,
            average_f_statistic: f_statistic.average_score(X, y, feature_indices)?,
            average_correlation: correlation.average_score(X, y, feature_indices)?,
            average_relief: relief.average_score(X, y, feature_indices)?,
        })
    }
}

/// Comprehensive relevance assessment results
#[derive(Debug, Clone)]
pub struct RelevanceAssessment {
    /// information_gain_scores
    pub information_gain_scores: Vec<f64>,
    /// chi_square_scores
    pub chi_square_scores: Vec<f64>,
    /// f_statistic_scores
    pub f_statistic_scores: Vec<f64>,
    /// correlation_scores
    pub correlation_scores: Vec<f64>,
    /// relief_scores
    pub relief_scores: Vec<f64>,
    /// feature_indices
    pub feature_indices: Vec<usize>,
    /// average_information_gain
    pub average_information_gain: f64,
    /// average_chi_square
    pub average_chi_square: f64,
    /// average_f_statistic
    pub average_f_statistic: f64,
    /// average_correlation
    pub average_correlation: f64,
    /// average_relief
    pub average_relief: f64,
}

impl RelevanceAssessment {
    /// Generate comprehensive relevance report
    pub fn report(&self) -> String {
        let mut report = String::new();

        report.push_str("=== Feature Relevance Assessment ===\n\n");

        report.push_str(&format!(
            "Number of features analyzed: {}\n\n",
            self.feature_indices.len()
        ));

        report.push_str(&format!(
            "Average Information Gain: {:.4}\n",
            self.average_information_gain
        ));
        report.push_str(&format!(
            "Average Chi-Square: {:.4}\n",
            self.average_chi_square
        ));
        report.push_str(&format!(
            "Average F-Statistic: {:.4}\n",
            self.average_f_statistic
        ));
        report.push_str(&format!(
            "Average Correlation: {:.4}\n",
            self.average_correlation
        ));
        report.push_str(&format!(
            "Average Relief Score: {:.4}\n\n",
            self.average_relief
        ));

        report.push_str("Per-Feature Relevance Scores:\n");
        report.push_str("Feature | InfoGain | Chi2     | F-Stat   | Corr     | Relief\n");
        report.push_str("--------|----------|----------|----------|----------|----------\n");

        for i in 0..self.feature_indices.len() {
            report.push_str(&format!(
                "{:7} | {:8.4} | {:8.4} | {:8.4} | {:8.4} | {:8.4}\n",
                self.feature_indices[i],
                self.information_gain_scores[i],
                self.chi_square_scores[i],
                self.f_statistic_scores[i],
                self.correlation_scores[i],
                self.relief_scores[i]
            ));
        }

        report.push_str(&format!(
            "\nOverall Relevance Assessment: {}\n",
            self.overall_assessment()
        ));

        report
    }

    fn overall_assessment(&self) -> &'static str {
        let scores = [
            self.average_information_gain,
            self.average_chi_square / 10.0,  // Normalize chi2
            self.average_f_statistic / 10.0, // Normalize F-stat
            self.average_correlation,
            self.average_relief,
        ];

        let average = scores.iter().sum::<f64>() / scores.len() as f64;

        match average {
            x if x >= 0.8 => "EXCELLENT: Features show very high relevance to target",
            x if x >= 0.6 => "GOOD: Features show good relevance to target",
            x if x >= 0.4 => "MODERATE: Features show moderate relevance to target",
            x if x >= 0.2 => "POOR: Features show low relevance to target",
            _ => {
                "CRITICAL: Features show very low relevance to target - consider different features"
            }
        }
    }

    /// Get top N features by relevance score (using average of normalized scores)
    pub fn get_top_features(&self, n: usize) -> Vec<(usize, f64)> {
        let mut feature_scores: Vec<(usize, f64)> = Vec::new();

        for i in 0..self.feature_indices.len() {
            let normalized_scores = [
                self.information_gain_scores[i],
                self.chi_square_scores[i] / 10.0,
                self.f_statistic_scores[i] / 10.0,
                self.correlation_scores[i],
                self.relief_scores[i],
            ];

            let average_score =
                normalized_scores.iter().sum::<f64>() / normalized_scores.len() as f64;
            feature_scores.push((self.feature_indices[i], average_score));
        }

        // Sort by score (descending)
        feature_scores.sort_by(|a, b| b.1.partial_cmp(&a.1).expect("operation should succeed"));

        feature_scores.into_iter().take(n).collect()
    }
}

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

    #[test]
    #[allow(non_snake_case)]
    fn test_information_gain_scoring() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0],];
        let y = array![0.0, 0.0, 1.0, 1.0];

        let ig_scorer = InformationGainScoring::new(3, true);
        let scores = ig_scorer
            .compute(X.view(), y.view(), &[0, 1])
            .expect("operation should succeed");

        assert_eq!(scores.len(), 2);
        for score in &scores {
            assert!(score >= &0.0);
        }
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_chi_square_scoring() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0],];
        let y = array![0.0, 0.0, 1.0, 1.0];

        let chi2_scorer = ChiSquareScoring::new(3);
        let scores = chi2_scorer
            .compute(X.view(), y.view(), &[0, 1])
            .expect("operation should succeed");

        assert_eq!(scores.len(), 2);
        for score in &scores {
            assert!(score >= &0.0);
        }
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_f_statistic_scoring() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0],];
        let y = array![0.0, 0.0, 1.0, 1.0];

        let f_scorer = FStatisticScoring::new(true);
        let scores = f_scorer
            .compute(X.view(), y.view(), &[0, 1])
            .expect("operation should succeed");

        assert_eq!(scores.len(), 2);
        for score in &scores {
            assert!(score >= &0.0);
        }
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_correlation_scoring() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0],];
        let y = array![1.0, 2.0, 3.0, 4.0];

        let corr_scorer = CorrelationScoring::new(true);
        let scores = corr_scorer
            .compute(X.view(), y.view(), &[0, 1])
            .expect("operation should succeed");

        assert_eq!(scores.len(), 2);
        for score in &scores {
            assert!((0.0..=1.0).contains(score));
        }
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_relief_scoring() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 1.0],];
        let y = array![0.0, 0.0, 1.0, 1.0];

        let relief_scorer = ReliefScoring::new(10, 1);
        let scores = relief_scorer
            .compute(X.view(), y.view(), &[0, 1])
            .expect("operation should succeed");

        assert_eq!(scores.len(), 2);
        // Relief scores can be negative
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_relevance_assessment() {
        let X = array![
            [1.0, 2.0, 10.0],
            [2.0, 3.0, 20.0],
            [3.0, 4.0, 30.0],
            [4.0, 5.0, 40.0],
            [5.0, 6.0, 50.0],
        ];
        let y = array![0.0, 0.0, 1.0, 1.0, 1.0];

        let relevance_scorer = RelevanceScoring::new(true);
        let assessment = relevance_scorer
            .compute(X.view(), y.view(), &[0, 1, 2])
            .expect("operation should succeed");

        assert_eq!(assessment.information_gain_scores.len(), 3);
        assert_eq!(assessment.chi_square_scores.len(), 3);
        assert_eq!(assessment.f_statistic_scores.len(), 3);
        assert_eq!(assessment.correlation_scores.len(), 3);
        assert_eq!(assessment.relief_scores.len(), 3);

        let report = assessment.report();
        assert!(report.contains("Relevance Assessment"));

        let top_features = assessment.get_top_features(2);
        assert_eq!(top_features.len(), 2);
    }
}