scirs2-transform 0.4.1

Data transformation module for SciRS2 (scirs2-transform)
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
//! Data normalization and standardization utilities
//!
//! This module provides functions for normalizing and standardizing data,
//! which is often a preprocessing step for machine learning algorithms.

use scirs2_core::ndarray::{Array1, Array2, ArrayBase, Axis, Data, Ix1, Ix2};
use scirs2_core::numeric::{Float, NumCast};

use crate::error::{Result, TransformError};

/// Small value to use for comparison with zero and numerical stability
pub const EPSILON: f64 = 1e-10;

/// Method of normalization to apply
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum NormalizationMethod {
    /// Min-max normalization (scales values to [0, 1] range)
    MinMax,
    /// Min-max normalization to custom range
    MinMaxCustom(f64, f64),
    /// Z-score standardization (zero mean, unit variance)
    ZScore,
    /// Max absolute scaling (scales by maximum absolute value)
    MaxAbs,
    /// L1 normalization (divide by sum of absolute values)
    L1,
    /// L2 normalization (divide by Euclidean norm)
    L2,
    /// Robust scaling using median and IQR (robust to outliers)
    Robust,
}

/// Normalizes a 2D array along a specified axis
///
/// # Arguments
/// * `array` - The input 2D array to normalize
/// * `method` - The normalization method to apply
/// * `axis` - The axis along which to normalize (0 for columns, 1 for rows)
///
/// # Returns
/// * `Result<Array2<f64>>` - The normalized array
///
/// # Examples
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_transform::normalize::{normalize_array, NormalizationMethod};
///
/// let data = array![[1.0, 2.0, 3.0],
///                   [4.0, 5.0, 6.0],
///                   [7.0, 8.0, 9.0]];
///                   
/// // Normalize columns (axis 0) using min-max normalization
/// let normalized = normalize_array(&data, NormalizationMethod::MinMax, 0).expect("Operation failed");
/// ```
#[allow(dead_code)]
pub fn normalize_array<S>(
    array: &ArrayBase<S, Ix2>,
    method: NormalizationMethod,
    axis: usize,
) -> Result<Array2<f64>>
where
    S: Data,
    S::Elem: Float + NumCast,
{
    let array_f64 = array.mapv(|x| NumCast::from(x).unwrap_or(0.0));

    if !array_f64.is_standard_layout() {
        return Err(TransformError::InvalidInput(
            "Input array must be in standard memory layout".to_string(),
        ));
    }

    if array_f64.ndim() != 2 {
        return Err(TransformError::InvalidInput(
            "Only 2D arrays are supported".to_string(),
        ));
    }

    if axis >= array_f64.ndim() {
        return Err(TransformError::InvalidInput(format!(
            "Invalid axis {} for array with {} dimensions",
            axis,
            array_f64.ndim()
        )));
    }

    let shape = array_f64.shape();
    let mut normalized = Array2::zeros((shape[0], shape[1]));

    match method {
        NormalizationMethod::MinMax => {
            let min = array_f64.map_axis(Axis(axis), |view| {
                view.fold(f64::INFINITY, |acc, &x| acc.min(x))
            });

            let max = array_f64.map_axis(Axis(axis), |view| {
                view.fold(f64::NEG_INFINITY, |acc, &x| acc.max(x))
            });

            let range = &max - &min;

            for i in 0..shape[0] {
                for j in 0..shape[1] {
                    let value = array_f64[[i, j]];
                    let idx = if axis == 0 { j } else { i };

                    if range[idx].abs() > EPSILON {
                        normalized[[i, j]] = (value - min[idx]) / range[idx];
                    } else {
                        normalized[[i, j]] = 0.5; // Default for constant features
                    }
                }
            }
        }
        NormalizationMethod::MinMaxCustom(new_min, new_max) => {
            let min = array_f64.map_axis(Axis(axis), |view| {
                view.fold(f64::INFINITY, |acc, &x| acc.min(x))
            });

            let max = array_f64.map_axis(Axis(axis), |view| {
                view.fold(f64::NEG_INFINITY, |acc, &x| acc.max(x))
            });

            let range = &max - &min;
            let new_range = new_max - new_min;

            for i in 0..shape[0] {
                for j in 0..shape[1] {
                    let value = array_f64[[i, j]];
                    let idx = if axis == 0 { j } else { i };

                    if range[idx].abs() > EPSILON {
                        normalized[[i, j]] = (value - min[idx]) / range[idx] * new_range + new_min;
                    } else {
                        normalized[[i, j]] = (new_min + new_max) / 2.0; // Default for constant features
                    }
                }
            }
        }
        NormalizationMethod::ZScore => {
            let mean = array_f64.map_axis(Axis(axis), |view| {
                view.iter().sum::<f64>() / view.len() as f64
            });

            let std_dev = array_f64.map_axis(Axis(axis), |view| {
                let m = view.iter().sum::<f64>() / view.len() as f64;
                let variance =
                    view.iter().map(|&x| (x - m).powi(2)).sum::<f64>() / view.len() as f64;
                variance.sqrt()
            });

            for i in 0..shape[0] {
                for j in 0..shape[1] {
                    let value = array_f64[[i, j]];
                    let idx = if axis == 0 { j } else { i };

                    if std_dev[idx] > EPSILON {
                        normalized[[i, j]] = (value - mean[idx]) / std_dev[idx];
                    } else {
                        normalized[[i, j]] = 0.0; // Default for constant features
                    }
                }
            }
        }
        NormalizationMethod::MaxAbs => {
            let max_abs = array_f64.map_axis(Axis(axis), |view| {
                view.fold(0.0, |acc, &x| acc.max(x.abs()))
            });

            for i in 0..shape[0] {
                for j in 0..shape[1] {
                    let value = array_f64[[i, j]];
                    let idx = if axis == 0 { j } else { i };

                    if max_abs[idx] > EPSILON {
                        normalized[[i, j]] = value / max_abs[idx];
                    } else {
                        normalized[[i, j]] = 0.0; // Default for constant features
                    }
                }
            }
        }
        NormalizationMethod::L1 => {
            let l1_norm =
                array_f64.map_axis(Axis(axis), |view| view.fold(0.0, |acc, &x| acc + x.abs()));

            for i in 0..shape[0] {
                for j in 0..shape[1] {
                    let value = array_f64[[i, j]];
                    let idx = if axis == 0 { j } else { i };

                    if l1_norm[idx] > EPSILON {
                        normalized[[i, j]] = value / l1_norm[idx];
                    } else {
                        normalized[[i, j]] = 0.0; // Default for constant features
                    }
                }
            }
        }
        NormalizationMethod::L2 => {
            let l2_norm = array_f64.map_axis(Axis(axis), |view| {
                let sum_squares = view.iter().fold(0.0, |acc, &x| acc + x * x);
                sum_squares.sqrt()
            });

            for i in 0..shape[0] {
                for j in 0..shape[1] {
                    let value = array_f64[[i, j]];
                    let idx = if axis == 0 { j } else { i };

                    if l2_norm[idx] > EPSILON {
                        normalized[[i, j]] = value / l2_norm[idx];
                    } else {
                        normalized[[i, j]] = 0.0; // Default for constant features
                    }
                }
            }
        }
        NormalizationMethod::Robust => {
            let median = array_f64.map_axis(Axis(axis), |view| {
                let mut data = view.to_vec();
                data.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
                let n = data.len();
                if n % 2 == 0 {
                    (data[n / 2 - 1] + data[n / 2]) / 2.0
                } else {
                    data[n / 2]
                }
            });

            let iqr = array_f64.map_axis(Axis(axis), |view| {
                let mut data = view.to_vec();
                data.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
                let n = data.len();

                // Calculate Q1 (25th percentile)
                let q1_pos = 0.25 * (n - 1) as f64;
                let q1_idx_low = q1_pos.floor() as usize;
                let q1_idx_high = q1_pos.ceil() as usize;
                let q1 = if q1_idx_low == q1_idx_high {
                    data[q1_idx_low]
                } else {
                    let weight = q1_pos - q1_idx_low as f64;
                    data[q1_idx_low] * (1.0 - weight) + data[q1_idx_high] * weight
                };

                // Calculate Q3 (75th percentile)
                let q3_pos = 0.75 * (n - 1) as f64;
                let q3_idx_low = q3_pos.floor() as usize;
                let q3_idx_high = q3_pos.ceil() as usize;
                let q3 = if q3_idx_low == q3_idx_high {
                    data[q3_idx_low]
                } else {
                    let weight = q3_pos - q3_idx_low as f64;
                    data[q3_idx_low] * (1.0 - weight) + data[q3_idx_high] * weight
                };

                q3 - q1
            });

            for i in 0..shape[0] {
                for j in 0..shape[1] {
                    let value = array_f64[[i, j]];
                    let idx = if axis == 0 { j } else { i };

                    if iqr[idx] > EPSILON {
                        normalized[[i, j]] = (value - median[idx]) / iqr[idx];
                    } else {
                        normalized[[i, j]] = 0.0; // Default for constant features
                    }
                }
            }
        }
    }

    Ok(normalized)
}

/// Normalizes a 1D array
///
/// # Arguments
/// * `array` - The input 1D array to normalize
/// * `method` - The normalization method to apply
///
/// # Returns
/// * `Result<Array1<f64>>` - The normalized array
///
/// # Examples
/// ```
/// use scirs2_core::ndarray::array;
/// use scirs2_transform::normalize::{normalize_vector, NormalizationMethod};
///
/// let data = array![1.0, 2.0, 3.0, 4.0, 5.0];
///                   
/// // Normalize vector using min-max normalization
/// let normalized = normalize_vector(&data, NormalizationMethod::MinMax).expect("Operation failed");
/// ```
#[allow(dead_code)]
pub fn normalize_vector<S>(
    array: &ArrayBase<S, Ix1>,
    method: NormalizationMethod,
) -> Result<Array1<f64>>
where
    S: Data,
    S::Elem: Float + NumCast,
{
    let array_f64 = array.mapv(|x| NumCast::from(x).unwrap_or(0.0));

    if array_f64.is_empty() {
        return Err(TransformError::InvalidInput(
            "Input array is empty".to_string(),
        ));
    }

    let mut normalized = Array1::zeros(array_f64.len());

    match method {
        NormalizationMethod::MinMax => {
            let min = array_f64.fold(f64::INFINITY, |acc, &x| acc.min(x));
            let max = array_f64.fold(f64::NEG_INFINITY, |acc, &x| acc.max(x));
            let range = max - min;

            if range.abs() > EPSILON {
                for (i, &value) in array_f64.iter().enumerate() {
                    normalized[i] = (value - min) / range;
                }
            } else {
                normalized.fill(0.5); // Default for constant features
            }
        }
        NormalizationMethod::MinMaxCustom(new_min, new_max) => {
            let min = array_f64.fold(f64::INFINITY, |acc, &x| acc.min(x));
            let max = array_f64.fold(f64::NEG_INFINITY, |acc, &x| acc.max(x));
            let range = max - min;
            let new_range = new_max - new_min;

            if range.abs() > EPSILON {
                for (i, &value) in array_f64.iter().enumerate() {
                    normalized[i] = (value - min) / range * new_range + new_min;
                }
            } else {
                normalized.fill((new_min + new_max) / 2.0); // Default for constant features
            }
        }
        NormalizationMethod::ZScore => {
            let mean = array_f64.iter().sum::<f64>() / array_f64.len() as f64;
            let variance =
                array_f64.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / array_f64.len() as f64;
            let std_dev = variance.sqrt();

            if std_dev > EPSILON {
                for (i, &value) in array_f64.iter().enumerate() {
                    normalized[i] = (value - mean) / std_dev;
                }
            } else {
                normalized.fill(0.0); // Default for constant features
            }
        }
        NormalizationMethod::MaxAbs => {
            let max_abs = array_f64.fold(0.0, |acc, &x| acc.max(x.abs()));

            if max_abs > EPSILON {
                for (i, &value) in array_f64.iter().enumerate() {
                    normalized[i] = value / max_abs;
                }
            } else {
                normalized.fill(0.0); // Default for constant features
            }
        }
        NormalizationMethod::L1 => {
            let l1_norm = array_f64.fold(0.0, |acc, &x| acc + x.abs());

            if l1_norm > EPSILON {
                for (i, &value) in array_f64.iter().enumerate() {
                    normalized[i] = value / l1_norm;
                }
            } else {
                normalized.fill(0.0); // Default for constant features
            }
        }
        NormalizationMethod::L2 => {
            let sum_squares = array_f64.iter().fold(0.0, |acc, &x| acc + x * x);
            let l2_norm = sum_squares.sqrt();

            if l2_norm > EPSILON {
                for (i, &value) in array_f64.iter().enumerate() {
                    normalized[i] = value / l2_norm;
                }
            } else {
                normalized.fill(0.0); // Default for constant features
            }
        }
        NormalizationMethod::Robust => {
            let mut data = array_f64.to_vec();
            data.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
            let n = data.len();

            // Calculate median
            let median = if n.is_multiple_of(2) {
                (data[n / 2 - 1] + data[n / 2]) / 2.0
            } else {
                data[n / 2]
            };

            // Calculate IQR (Interquartile Range)
            // Calculate Q1 (25th percentile)
            let q1_pos = 0.25 * (n - 1) as f64;
            let q1_idx_low = q1_pos.floor() as usize;
            let q1_idx_high = q1_pos.ceil() as usize;
            let q1 = if q1_idx_low == q1_idx_high {
                data[q1_idx_low]
            } else {
                let weight = q1_pos - q1_idx_low as f64;
                data[q1_idx_low] * (1.0 - weight) + data[q1_idx_high] * weight
            };

            // Calculate Q3 (75th percentile)
            let q3_pos = 0.75 * (n - 1) as f64;
            let q3_idx_low = q3_pos.floor() as usize;
            let q3_idx_high = q3_pos.ceil() as usize;
            let q3 = if q3_idx_low == q3_idx_high {
                data[q3_idx_low]
            } else {
                let weight = q3_pos - q3_idx_low as f64;
                data[q3_idx_low] * (1.0 - weight) + data[q3_idx_high] * weight
            };

            let iqr = q3 - q1;

            if iqr > EPSILON {
                for (i, &value) in array_f64.iter().enumerate() {
                    normalized[i] = (value - median) / iqr;
                }
            } else {
                normalized.fill(0.0); // Default for constant features
            }
        }
    }

    Ok(normalized)
}

/// Represents a fitted normalization model that can transform new data
#[derive(Clone)]
pub struct Normalizer {
    /// The axis along which to normalize (0 for columns, 1 for rows)
    axis: usize,
    /// Parameters from the fit (depends on method)
    params: NormalizerParams,
}

/// Parameters for different normalization methods
#[derive(Clone)]
enum NormalizerParams {
    /// Min and max values for MinMax normalization
    MinMax {
        min: Array1<f64>,
        max: Array1<f64>,
        new_min: f64,
        new_max: f64,
    },
    /// Mean and standard deviation for ZScore normalization
    ZScore {
        mean: Array1<f64>,
        std_dev: Array1<f64>,
    },
    /// Maximum absolute values for MaxAbs normalization
    MaxAbs { max_abs: Array1<f64> },
    /// L1 norms for L1 normalization
    L1 { l1_norm: Array1<f64> },
    /// L2 norms for L2 normalization
    L2 { l2_norm: Array1<f64> },
    /// Median and IQR for Robust normalization
    Robust {
        median: Array1<f64>,
        iqr: Array1<f64>,
    },
}

impl Normalizer {
    /// Creates a new Normalizer with the specified method and axis
    ///
    /// # Arguments
    /// * `method` - The normalization method to apply
    /// * `axis` - The axis along which to normalize (0 for columns, 1 for rows)
    ///
    /// # Returns
    /// * A new Normalizer instance
    pub fn new(method: NormalizationMethod, axis: usize) -> Self {
        let params = match method {
            NormalizationMethod::MinMax => NormalizerParams::MinMax {
                min: Array1::zeros(0),
                max: Array1::zeros(0),
                new_min: 0.0,
                new_max: 1.0,
            },
            NormalizationMethod::MinMaxCustom(min, max) => NormalizerParams::MinMax {
                min: Array1::zeros(0),
                max: Array1::zeros(0),
                new_min: min,
                new_max: max,
            },
            NormalizationMethod::ZScore => NormalizerParams::ZScore {
                mean: Array1::zeros(0),
                std_dev: Array1::zeros(0),
            },
            NormalizationMethod::MaxAbs => NormalizerParams::MaxAbs {
                max_abs: Array1::zeros(0),
            },
            NormalizationMethod::L1 => NormalizerParams::L1 {
                l1_norm: Array1::zeros(0),
            },
            NormalizationMethod::L2 => NormalizerParams::L2 {
                l2_norm: Array1::zeros(0),
            },
            NormalizationMethod::Robust => NormalizerParams::Robust {
                median: Array1::zeros(0),
                iqr: Array1::zeros(0),
            },
        };

        Normalizer { axis, params }
    }

    /// Fits the normalizer to the input data
    ///
    /// # Arguments
    /// * `array` - The input 2D array to fit the normalizer to
    ///
    /// # Returns
    /// * `Result<()>` - Ok if successful, Err otherwise
    pub fn fit<S>(&mut self, array: &ArrayBase<S, Ix2>) -> Result<()>
    where
        S: Data,
        S::Elem: Float + NumCast,
    {
        let array_f64 = array.mapv(|x| NumCast::from(x).unwrap_or(0.0));

        if !array_f64.is_standard_layout() {
            return Err(TransformError::InvalidInput(
                "Input array must be in standard memory layout".to_string(),
            ));
        }

        if array_f64.ndim() != 2 {
            return Err(TransformError::InvalidInput(
                "Only 2D arrays are supported".to_string(),
            ));
        }

        if self.axis >= array_f64.ndim() {
            return Err(TransformError::InvalidInput(format!(
                "Invalid axis {} for array with {} dimensions",
                self.axis,
                array_f64.ndim()
            )));
        }

        match &mut self.params {
            NormalizerParams::MinMax {
                min,
                max,
                new_min: _,
                new_max: _,
            } => {
                *min = array_f64.map_axis(Axis(self.axis), |view| {
                    view.fold(f64::INFINITY, |acc, &x| acc.min(x))
                });

                *max = array_f64.map_axis(Axis(self.axis), |view| {
                    view.fold(f64::NEG_INFINITY, |acc, &x| acc.max(x))
                });
            }
            NormalizerParams::ZScore { mean, std_dev } => {
                *mean = array_f64.map_axis(Axis(self.axis), |view| {
                    view.iter().sum::<f64>() / view.len() as f64
                });

                *std_dev = array_f64.map_axis(Axis(self.axis), |view| {
                    let m = view.iter().sum::<f64>() / view.len() as f64;
                    let variance =
                        view.iter().map(|&x| (x - m).powi(2)).sum::<f64>() / view.len() as f64;
                    variance.sqrt()
                });
            }
            NormalizerParams::MaxAbs { max_abs } => {
                *max_abs = array_f64.map_axis(Axis(self.axis), |view| {
                    view.fold(0.0, |acc, &x| acc.max(x.abs()))
                });
            }
            NormalizerParams::L1 { l1_norm } => {
                *l1_norm = array_f64.map_axis(Axis(self.axis), |view| {
                    view.fold(0.0, |acc, &x| acc + x.abs())
                });
            }
            NormalizerParams::L2 { l2_norm } => {
                *l2_norm = array_f64.map_axis(Axis(self.axis), |view| {
                    let sum_squares = view.iter().fold(0.0, |acc, &x| acc + x * x);
                    sum_squares.sqrt()
                });
            }
            NormalizerParams::Robust { median, iqr } => {
                *median = array_f64.map_axis(Axis(self.axis), |view| {
                    let mut data = view.to_vec();
                    data.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
                    let n = data.len();
                    if n % 2 == 0 {
                        (data[n / 2 - 1] + data[n / 2]) / 2.0
                    } else {
                        data[n / 2]
                    }
                });

                *iqr = array_f64.map_axis(Axis(self.axis), |view| {
                    let mut data = view.to_vec();
                    data.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
                    let n = data.len();

                    // Calculate Q1 (25th percentile)
                    let q1_pos = 0.25 * (n - 1) as f64;
                    let q1_idx_low = q1_pos.floor() as usize;
                    let q1_idx_high = q1_pos.ceil() as usize;
                    let q1 = if q1_idx_low == q1_idx_high {
                        data[q1_idx_low]
                    } else {
                        let weight = q1_pos - q1_idx_low as f64;
                        data[q1_idx_low] * (1.0 - weight) + data[q1_idx_high] * weight
                    };

                    // Calculate Q3 (75th percentile)
                    let q3_pos = 0.75 * (n - 1) as f64;
                    let q3_idx_low = q3_pos.floor() as usize;
                    let q3_idx_high = q3_pos.ceil() as usize;
                    let q3 = if q3_idx_low == q3_idx_high {
                        data[q3_idx_low]
                    } else {
                        let weight = q3_pos - q3_idx_low as f64;
                        data[q3_idx_low] * (1.0 - weight) + data[q3_idx_high] * weight
                    };

                    q3 - q1
                });
            }
        }

        Ok(())
    }

    /// Transforms the input data using the fitted normalizer
    ///
    /// # Arguments
    /// * `array` - The input 2D array to transform
    ///
    /// # Returns
    /// * `Result<Array2<f64>>` - The transformed array
    pub fn transform<S>(&self, array: &ArrayBase<S, Ix2>) -> Result<Array2<f64>>
    where
        S: Data,
        S::Elem: Float + NumCast,
    {
        let array_f64 = array.mapv(|x| NumCast::from(x).unwrap_or(0.0));

        if !array_f64.is_standard_layout() {
            return Err(TransformError::InvalidInput(
                "Input array must be in standard memory layout".to_string(),
            ));
        }

        if array_f64.ndim() != 2 {
            return Err(TransformError::InvalidInput(
                "Only 2D arrays are supported".to_string(),
            ));
        }

        // Check the dimension along the normalization axis
        let expected_size = match &self.params {
            NormalizerParams::MinMax { min, .. } => min.len(),
            NormalizerParams::ZScore { mean, .. } => mean.len(),
            NormalizerParams::MaxAbs { max_abs } => max_abs.len(),
            NormalizerParams::L1 { l1_norm } => l1_norm.len(),
            NormalizerParams::L2 { l2_norm } => l2_norm.len(),
            NormalizerParams::Robust { median, .. } => median.len(),
        };

        let actual_size = if self.axis == 0 {
            array_f64.shape()[1]
        } else {
            array_f64.shape()[0]
        };

        if expected_size != actual_size {
            return Err(TransformError::InvalidInput(format!(
                "Expected {expected_size} features, got {actual_size}"
            )));
        }

        let shape = array_f64.shape();
        let mut transformed = Array2::zeros((shape[0], shape[1]));

        match &self.params {
            NormalizerParams::MinMax {
                min,
                max,
                new_min,
                new_max,
            } => {
                let range = max - min;
                let new_range = new_max - new_min;

                for i in 0..shape[0] {
                    for j in 0..shape[1] {
                        let value = array_f64[[i, j]];
                        let idx = if self.axis == 0 { j } else { i };

                        if range[idx].abs() > EPSILON {
                            transformed[[i, j]] =
                                (value - min[idx]) / range[idx] * new_range + new_min;
                        } else {
                            transformed[[i, j]] = (new_min + new_max) / 2.0; // Default for constant features
                        }
                    }
                }
            }
            NormalizerParams::ZScore { mean, std_dev } => {
                for i in 0..shape[0] {
                    for j in 0..shape[1] {
                        let value = array_f64[[i, j]];
                        let idx = if self.axis == 0 { j } else { i };

                        if std_dev[idx] > EPSILON {
                            transformed[[i, j]] = (value - mean[idx]) / std_dev[idx];
                        } else {
                            transformed[[i, j]] = 0.0; // Default for constant features
                        }
                    }
                }
            }
            NormalizerParams::MaxAbs { max_abs } => {
                for i in 0..shape[0] {
                    for j in 0..shape[1] {
                        let value = array_f64[[i, j]];
                        let idx = if self.axis == 0 { j } else { i };

                        if max_abs[idx] > EPSILON {
                            transformed[[i, j]] = value / max_abs[idx];
                        } else {
                            transformed[[i, j]] = 0.0; // Default for constant features
                        }
                    }
                }
            }
            NormalizerParams::L1 { l1_norm } => {
                for i in 0..shape[0] {
                    for j in 0..shape[1] {
                        let value = array_f64[[i, j]];
                        let idx = if self.axis == 0 { j } else { i };

                        if l1_norm[idx] > EPSILON {
                            transformed[[i, j]] = value / l1_norm[idx];
                        } else {
                            transformed[[i, j]] = 0.0; // Default for constant features
                        }
                    }
                }
            }
            NormalizerParams::L2 { l2_norm } => {
                for i in 0..shape[0] {
                    for j in 0..shape[1] {
                        let value = array_f64[[i, j]];
                        let idx = if self.axis == 0 { j } else { i };

                        if l2_norm[idx] > EPSILON {
                            transformed[[i, j]] = value / l2_norm[idx];
                        } else {
                            transformed[[i, j]] = 0.0; // Default for constant features
                        }
                    }
                }
            }
            NormalizerParams::Robust { median, iqr } => {
                for i in 0..shape[0] {
                    for j in 0..shape[1] {
                        let value = array_f64[[i, j]];
                        let idx = if self.axis == 0 { j } else { i };

                        if iqr[idx] > EPSILON {
                            transformed[[i, j]] = (value - median[idx]) / iqr[idx];
                        } else {
                            transformed[[i, j]] = 0.0; // Default for constant features
                        }
                    }
                }
            }
        }

        Ok(transformed)
    }

    /// Fits the normalizer to the input data and transforms it
    ///
    /// # Arguments
    /// * `array` - The input 2D array to fit and transform
    ///
    /// # Returns
    /// * `Result<Array2<f64>>` - The transformed array
    pub fn fit_transform<S>(&mut self, array: &ArrayBase<S, Ix2>) -> Result<Array2<f64>>
    where
        S: Data,
        S::Elem: Float + NumCast,
    {
        self.fit(array)?;
        self.transform(array)
    }
}

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

    #[test]
    fn test_normalize_vector_minmax() {
        let data = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0]);
        let normalized =
            normalize_vector(&data, NormalizationMethod::MinMax).expect("Operation failed");

        let expected = Array::from_vec(vec![0.0, 0.25, 0.5, 0.75, 1.0]);

        for (a, b) in normalized.iter().zip(expected.iter()) {
            assert_abs_diff_eq!(a, b, epsilon = 1e-10);
        }
    }

    #[test]
    fn test_normalize_vector_zscore() {
        let data = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0]);
        let normalized =
            normalize_vector(&data, NormalizationMethod::ZScore).expect("Operation failed");

        let mean = 3.0;
        let std_dev = (10.0 / 5.0_f64).sqrt();
        let expected = data.mapv(|x| (x - mean) / std_dev);

        for (a, b) in normalized.iter().zip(expected.iter()) {
            assert_abs_diff_eq!(a, b, epsilon = 1e-10);
        }
    }

    #[test]
    fn test_normalize_array_minmax() {
        let data = Array::from_shape_vec((3, 3), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])
            .expect("Operation failed");

        // Normalize columns (axis 0)
        let normalized =
            normalize_array(&data, NormalizationMethod::MinMax, 0).expect("Operation failed");

        let expected =
            Array::from_shape_vec((3, 3), vec![0.0, 0.0, 0.0, 0.5, 0.5, 0.5, 1.0, 1.0, 1.0])
                .expect("Operation failed");

        for i in 0..3 {
            for j in 0..3 {
                assert_abs_diff_eq!(normalized[[i, j]], expected[[i, j]], epsilon = 1e-10);
            }
        }

        // Normalize rows (axis 1)
        let normalized =
            normalize_array(&data, NormalizationMethod::MinMax, 1).expect("Operation failed");

        let expected =
            Array::from_shape_vec((3, 3), vec![0.0, 0.5, 1.0, 0.0, 0.5, 1.0, 0.0, 0.5, 1.0])
                .expect("Operation failed");

        for i in 0..3 {
            for j in 0..3 {
                assert_abs_diff_eq!(normalized[[i, j]], expected[[i, j]], epsilon = 1e-10);
            }
        }
    }

    #[test]
    fn test_normalizer_fit_transform() {
        let data = Array::from_shape_vec((3, 3), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])
            .expect("Operation failed");

        // Test MinMax normalization
        let mut normalizer = Normalizer::new(NormalizationMethod::MinMax, 0);
        let transformed = normalizer.fit_transform(&data).expect("Operation failed");

        let expected =
            Array::from_shape_vec((3, 3), vec![0.0, 0.0, 0.0, 0.5, 0.5, 0.5, 1.0, 1.0, 1.0])
                .expect("Operation failed");

        for i in 0..3 {
            for j in 0..3 {
                assert_abs_diff_eq!(transformed[[i, j]], expected[[i, j]], epsilon = 1e-10);
            }
        }

        // Test with separate fit and transform
        let data2 = Array::from_shape_vec((2, 3), vec![2.0, 3.0, 4.0, 5.0, 6.0, 7.0])
            .expect("Operation failed");

        let transformed2 = normalizer.transform(&data2).expect("Operation failed");

        let expected2 = Array::from_shape_vec(
            (2, 3),
            vec![
                1.0 / 6.0,
                1.0 / 6.0,
                1.0 / 6.0,
                2.0 / 3.0,
                2.0 / 3.0,
                2.0 / 3.0,
            ],
        )
        .expect("Operation failed");

        for i in 0..2 {
            for j in 0..3 {
                assert_abs_diff_eq!(transformed2[[i, j]], expected2[[i, j]], epsilon = 1e-10);
            }
        }
    }

    #[test]
    fn test_normalize_vector_robust() {
        // Test with data containing outliers
        let data = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 100.0]); // 100 is an outlier
        let normalized =
            normalize_vector(&data, NormalizationMethod::Robust).expect("Operation failed");

        // For this data: sorted = [1, 2, 3, 4, 100]
        // median = 3.0 (middle value)
        // Q1 = 2.0 (at 25th percentile), Q3 = 4.0 (at 75th percentile), IQR = 2.0
        // Expected transformation: (x - 3) / 2
        let expected = Array::from_vec(vec![
            (1.0 - 3.0) / 2.0,   // -1.0
            (2.0 - 3.0) / 2.0,   // -0.5
            (3.0 - 3.0) / 2.0,   // 0
            (4.0 - 3.0) / 2.0,   // 0.5
            (100.0 - 3.0) / 2.0, // 48.5
        ]);

        for (a, b) in normalized.iter().zip(expected.iter()) {
            assert_abs_diff_eq!(a, b, epsilon = 1e-10);
        }
    }

    #[test]
    fn test_normalize_array_robust() {
        let data = Array::from_shape_vec((3, 2), vec![1.0, 10.0, 2.0, 20.0, 3.0, 30.0])
            .expect("Operation failed");

        // Normalize columns (axis 0)
        let normalized =
            normalize_array(&data, NormalizationMethod::Robust, 0).expect("Operation failed");

        // For column 0: [1, 2, 3] -> median=2, Q1=1.5, Q3=2.5, IQR=1.0
        // For column 1: [10, 20, 30] -> median=20, Q1=15, Q3=25, IQR=10
        let expected = Array::from_shape_vec(
            (3, 2),
            vec![
                (1.0 - 2.0) / 1.0,    // -1.0
                (10.0 - 20.0) / 10.0, // -1.0
                (2.0 - 2.0) / 1.0,    // 0.0
                (20.0 - 20.0) / 10.0, // 0.0
                (3.0 - 2.0) / 1.0,    // 1.0
                (30.0 - 20.0) / 10.0, // 1.0
            ],
        )
        .expect("Operation failed");

        for i in 0..3 {
            for j in 0..2 {
                assert_abs_diff_eq!(normalized[[i, j]], expected[[i, j]], epsilon = 1e-10);
            }
        }
    }

    #[test]
    fn test_robust_normalizer() {
        let data =
            Array::from_shape_vec((4, 2), vec![1.0, 100.0, 2.0, 200.0, 3.0, 300.0, 4.0, 400.0])
                .expect("Operation failed");

        let mut normalizer = Normalizer::new(NormalizationMethod::Robust, 0);
        let transformed = normalizer.fit_transform(&data).expect("Operation failed");

        // For column 0: [1, 2, 3, 4] -> median=2.5, Q1=1.75, Q3=3.25, IQR=1.5
        // For column 1: [100, 200, 300, 400] -> median=250, Q1=175, Q3=325, IQR=150
        let expected = Array::from_shape_vec(
            (4, 2),
            vec![
                (1.0 - 2.5) / 1.5,       // -1.0
                (100.0 - 250.0) / 150.0, // -1.0
                (2.0 - 2.5) / 1.5,       // -0.333...
                (200.0 - 250.0) / 150.0, // -0.333...
                (3.0 - 2.5) / 1.5,       // 0.333...
                (300.0 - 250.0) / 150.0, // 0.333...
                (4.0 - 2.5) / 1.5,       // 1.0
                (400.0 - 250.0) / 150.0, // 1.0
            ],
        )
        .expect("Operation failed");

        for i in 0..4 {
            for j in 0..2 {
                assert_abs_diff_eq!(transformed[[i, j]], expected[[i, j]], epsilon = 1e-10);
            }
        }
    }
}