tenflowers-dataset 0.1.1

Data pipeline and dataset utilities for TenfloweRS
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
//! Feature engineering transformations
//!
//! This module provides feature engineering transformations commonly used
//! for data preprocessing and feature extraction in machine learning.

use crate::transforms::Transform;
use scirs2_core::random::Rng;
use std::collections::HashMap;
use std::marker::PhantomData;
use tenflowers_core::{Result, Tensor, TensorError};

/// Polynomial features transformation
/// Generates polynomial and interaction features up to a given degree
pub struct PolynomialFeatures<T> {
    degree: usize,
    include_bias: bool,
    interaction_only: bool,
    _phantom: PhantomData<T>,
}

impl<T> PolynomialFeatures<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Float
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable,
{
    pub fn new(degree: usize) -> Self {
        Self {
            degree: degree.max(1),
            include_bias: true,
            interaction_only: false,
            _phantom: PhantomData,
        }
    }

    pub fn with_bias(mut self, include_bias: bool) -> Self {
        self.include_bias = include_bias;
        self
    }

    pub fn interaction_only(mut self) -> Self {
        self.interaction_only = true;
        self
    }

    /// Generate all combinations of feature indices for polynomial terms
    fn generate_combinations(&self, n_features: usize) -> Vec<Vec<usize>> {
        let mut combinations = Vec::new();

        // Add bias term if requested
        if self.include_bias {
            combinations.push(vec![]);
        }

        // Generate combinations for each degree
        for degree in 1..=self.degree {
            self.generate_combinations_recursive(
                &mut combinations,
                &mut Vec::new(),
                0,
                n_features,
                degree,
            );
        }

        combinations
    }

    fn generate_combinations_recursive(
        &self,
        combinations: &mut Vec<Vec<usize>>,
        current: &mut Vec<usize>,
        start_idx: usize,
        n_features: usize,
        remaining_degree: usize,
    ) {
        if remaining_degree == 0 {
            combinations.push(current.clone());
            return;
        }

        for i in start_idx..n_features {
            current.push(i);
            let next_start = if self.interaction_only { i + 1 } else { i };
            self.generate_combinations_recursive(
                combinations,
                current,
                next_start,
                n_features,
                remaining_degree - 1,
            );
            current.pop();
        }
    }

    /// Compute polynomial feature value from a combination
    fn compute_polynomial_feature(&self, features: &[T], combination: &[usize]) -> T {
        if combination.is_empty() {
            return T::one(); // Bias term
        }

        let mut result = T::one();
        for &feature_idx in combination {
            if feature_idx < features.len() {
                result = result * features[feature_idx];
            }
        }
        result
    }
}

impl<T> Transform<T> for PolynomialFeatures<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Float
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable,
{
    fn apply(&self, sample: (Tensor<T>, Tensor<T>)) -> Result<(Tensor<T>, Tensor<T>)> {
        let (features, labels) = sample;
        let original_shape = features.shape().dims();
        let n_features = features.shape().size();

        // Flatten features for processing
        let feature_data = features.as_slice().ok_or_else(|| {
            TensorError::invalid_argument(
                "Cannot access tensor data (GPU tensor not supported)".to_string(),
            )
        })?;

        // Generate polynomial combinations
        let combinations = self.generate_combinations(n_features);
        let mut polynomial_data = Vec::with_capacity(combinations.len());

        // Compute polynomial features
        for combination in &combinations {
            let poly_value = self.compute_polynomial_feature(feature_data, combination);
            polynomial_data.push(poly_value);
        }

        let polynomial_features = Tensor::from_vec(polynomial_data, &[combinations.len()])?;
        Ok((polynomial_features, labels))
    }
}

/// Binning transformation for continuous features
/// Transforms continuous values into discrete bins
pub struct BinningTransform<T> {
    n_bins: usize,
    strategy: BinningStrategy,
    bin_edges: Option<Vec<T>>,
    _phantom: PhantomData<T>,
}

#[derive(Debug, Clone)]
pub enum BinningStrategy {
    /// Equal-width bins
    Uniform,
    /// Equal-frequency bins (quantiles)
    Quantile,
    /// K-means clustering for bin centers
    KMeans,
}

impl<T> BinningTransform<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Float
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable,
{
    pub fn new(n_bins: usize, strategy: BinningStrategy) -> Self {
        Self {
            n_bins: n_bins.max(2),
            strategy,
            bin_edges: None,
            _phantom: PhantomData,
        }
    }

    pub fn uniform(n_bins: usize) -> Self {
        Self::new(n_bins, BinningStrategy::Uniform)
    }

    pub fn quantile(n_bins: usize) -> Self {
        Self::new(n_bins, BinningStrategy::Quantile)
    }

    pub fn with_edges(mut self, edges: Vec<T>) -> Self {
        self.bin_edges = Some(edges);
        self
    }

    /// Fit the binning transform to data
    pub fn fit(&mut self, data: &[T]) -> Result<()> {
        if data.is_empty() {
            return Err(TensorError::invalid_argument(
                "Cannot fit binning transform on empty data".to_string(),
            ));
        }

        let mut sorted_data = data.to_vec();
        sorted_data.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let min_val = sorted_data[0];
        let max_val = sorted_data[sorted_data.len() - 1];

        let edges = match self.strategy {
            BinningStrategy::Uniform => {
                let mut edges = Vec::with_capacity(self.n_bins + 1);
                let step = (max_val - min_val)
                    / T::from(self.n_bins).expect("bin count should convert to float");

                for i in 0..=self.n_bins {
                    edges.push(
                        min_val + T::from(i).expect("bin index should convert to float") * step,
                    );
                }
                edges
            }
            BinningStrategy::Quantile => {
                let mut edges = Vec::with_capacity(self.n_bins + 1);
                edges.push(min_val);

                for i in 1..self.n_bins {
                    let quantile = i as f64 / self.n_bins as f64;
                    let idx = (quantile * (sorted_data.len() - 1) as f64) as usize;
                    edges.push(sorted_data[idx]);
                }

                edges.push(max_val);
                edges
            }
            BinningStrategy::KMeans => {
                // Simplified k-means for bin centers
                let mut centers = Vec::with_capacity(self.n_bins);
                let step = (max_val - min_val)
                    / T::from(self.n_bins - 1).expect("bin count should convert to float");

                for i in 0..self.n_bins {
                    centers.push(
                        min_val + T::from(i).expect("center index should convert to float") * step,
                    );
                }

                // Convert centers to edges (midpoints)
                let mut edges = vec![min_val];
                for i in 1..self.n_bins {
                    let midpoint = (centers[i - 1] + centers[i])
                        / T::from(2.0).expect("constant 2.0 should convert to float");
                    edges.push(midpoint);
                }
                edges.push(max_val);
                edges
            }
        };

        self.bin_edges = Some(edges);
        Ok(())
    }

    /// Find which bin a value belongs to
    fn find_bin(&self, value: T) -> usize {
        if let Some(ref edges) = self.bin_edges {
            for (i, &edge) in edges.iter().enumerate().skip(1) {
                if value <= edge {
                    return i - 1;
                }
            }
            return edges.len() - 2; // Last bin
        }
        0 // Default to first bin if not fitted
    }
}

impl<T> Transform<T> for BinningTransform<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Float
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable,
{
    fn apply(&self, sample: (Tensor<T>, Tensor<T>)) -> Result<(Tensor<T>, Tensor<T>)> {
        let (features, labels) = sample;

        if self.bin_edges.is_none() {
            return Err(TensorError::invalid_argument(
                "BinningTransform must be fitted before use".to_string(),
            ));
        }

        let feature_data = features.as_slice().ok_or_else(|| {
            TensorError::invalid_argument(
                "Cannot access tensor data (GPU tensor not supported)".to_string(),
            )
        })?;

        let binned_data: Vec<T> = feature_data
            .iter()
            .map(|&val| T::from(self.find_bin(val)).expect("bin index should convert to T"))
            .collect();

        let binned_features = Tensor::from_vec(binned_data, features.shape().dims())?;
        Ok((binned_features, labels))
    }
}

/// One-hot encoding transformation
/// Converts categorical features to binary feature vectors
pub struct OneHotEncode<T> {
    categories: Option<HashMap<usize, Vec<T>>>, // category value -> one-hot index mapping
    drop_first: bool,
    _phantom: PhantomData<T>,
}

impl<T> OneHotEncode<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Float
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable
        + std::hash::Hash
        + Eq,
{
    pub fn new() -> Self {
        Self {
            categories: None,
            drop_first: false,
            _phantom: PhantomData,
        }
    }

    pub fn drop_first(mut self) -> Self {
        self.drop_first = true;
        self
    }

    /// Fit the encoder to discover categories
    pub fn fit(&mut self, data: &[T]) -> Result<()> {
        let mut unique_values: Vec<T> = data.iter().cloned().collect();
        unique_values.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
        unique_values.dedup();

        let mut categories = HashMap::new();
        categories.insert(0, unique_values);

        self.categories = Some(categories);
        Ok(())
    }

    /// Get the number of output features after one-hot encoding
    pub fn output_size(&self) -> usize {
        if let Some(ref categories) = self.categories {
            let base_size = categories.get(&0).map(|c| c.len()).unwrap_or(0);
            if self.drop_first && base_size > 0 {
                base_size - 1
            } else {
                base_size
            }
        } else {
            0
        }
    }
}

impl<T> Default for OneHotEncode<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Float
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable
        + std::hash::Hash
        + Eq,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<T> Transform<T> for OneHotEncode<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Float
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable
        + std::hash::Hash
        + Eq,
{
    fn apply(&self, sample: (Tensor<T>, Tensor<T>)) -> Result<(Tensor<T>, Tensor<T>)> {
        let (features, labels) = sample;

        if self.categories.is_none() {
            return Err(TensorError::invalid_argument(
                "OneHotEncode must be fitted before use".to_string(),
            ));
        }

        let feature_data = features.as_slice().ok_or_else(|| {
            TensorError::invalid_argument(
                "Cannot access tensor data (GPU tensor not supported)".to_string(),
            )
        })?;

        let categories = self
            .categories
            .as_ref()
            .expect("categories should be fitted")
            .get(&0)
            .expect("feature index 0 should exist");
        let output_size = self.output_size();
        let mut encoded_data = Vec::with_capacity(output_size * feature_data.len());

        for &value in feature_data {
            let mut one_hot = vec![T::zero(); output_size];

            if let Some(pos) = categories.iter().position(|&cat| cat == value) {
                let adjusted_pos = if self.drop_first && pos > 0 {
                    pos - 1
                } else if self.drop_first && pos == 0 {
                    // First category is dropped, so no position to set
                    one_hot.len() // Invalid position, will be caught below
                } else {
                    pos
                };

                if adjusted_pos < one_hot.len() {
                    one_hot[adjusted_pos] = T::one();
                }
            }

            encoded_data.extend(one_hot);
        }

        let new_shape = vec![feature_data.len(), output_size];
        let encoded_features = Tensor::from_vec(encoded_data, &new_shape)?;
        Ok((encoded_features, labels))
    }
}

/// Target encoding transformation
/// Encodes categorical features using target statistics
pub struct TargetEncode<T> {
    category_means: Option<HashMap<T, T>>,
    global_mean: Option<T>,
    smoothing: f64,
    _phantom: PhantomData<T>,
}

impl<T> TargetEncode<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Float
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable
        + std::hash::Hash
        + Eq,
{
    pub fn new(smoothing: f64) -> Self {
        Self {
            category_means: None,
            global_mean: None,
            smoothing: smoothing.max(0.0),
            _phantom: PhantomData,
        }
    }

    /// Fit the encoder using features and targets
    pub fn fit(&mut self, features: &[T], targets: &[T]) -> Result<()> {
        if features.len() != targets.len() {
            return Err(TensorError::invalid_argument(
                "Features and targets must have the same length".to_string(),
            ));
        }

        // Calculate global mean
        let global_mean = targets.iter().fold(T::zero(), |acc, &x| acc + x)
            / T::from(targets.len()).expect("target length should convert to T");
        self.global_mean = Some(global_mean);

        // Calculate category means
        let mut category_sums: HashMap<T, (T, usize)> = HashMap::new();

        for (&feature, &target) in features.iter().zip(targets.iter()) {
            let entry = category_sums.entry(feature).or_insert((T::zero(), 0));
            entry.0 = entry.0 + target;
            entry.1 += 1;
        }

        let mut category_means = HashMap::new();
        for (category, (sum, count)) in category_sums {
            let category_mean = sum / T::from(count).expect("count should convert to T");

            // Apply smoothing
            let smoothed_mean = if self.smoothing > 0.0 {
                let alpha = T::from(self.smoothing).expect("smoothing value should convert to T");
                let n = T::from(count).expect("count should convert to T");
                (category_mean * n + global_mean * alpha) / (n + alpha)
            } else {
                category_mean
            };

            category_means.insert(category, smoothed_mean);
        }

        self.category_means = Some(category_means);
        Ok(())
    }
}

impl<T> Transform<T> for TargetEncode<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Float
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable
        + std::hash::Hash
        + Eq,
{
    fn apply(&self, sample: (Tensor<T>, Tensor<T>)) -> Result<(Tensor<T>, Tensor<T>)> {
        let (features, labels) = sample;

        if self.category_means.is_none() || self.global_mean.is_none() {
            return Err(TensorError::invalid_argument(
                "TargetEncode must be fitted before use".to_string(),
            ));
        }

        let feature_data = features.as_slice().ok_or_else(|| {
            TensorError::invalid_argument(
                "Cannot access tensor data (GPU tensor not supported)".to_string(),
            )
        })?;

        let category_means = self
            .category_means
            .as_ref()
            .expect("category_means should be fitted");
        let global_mean = self.global_mean.expect("global_mean should be fitted");

        let encoded_data: Vec<T> = feature_data
            .iter()
            .map(|&value| category_means.get(&value).copied().unwrap_or(global_mean))
            .collect();

        let encoded_features = Tensor::from_vec(encoded_data, features.shape().dims())?;
        Ok((encoded_features, labels))
    }
}

/// Feature selection based on variance threshold
pub struct VarianceThreshold<T> {
    threshold: T,
    selected_features: Option<Vec<usize>>,
    _phantom: PhantomData<T>,
}

impl<T> VarianceThreshold<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Float
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable,
{
    pub fn new(threshold: T) -> Self {
        Self {
            threshold,
            selected_features: None,
            _phantom: PhantomData,
        }
    }

    /// Fit the selector to identify features with sufficient variance
    pub fn fit(&mut self, data: &[Vec<T>]) -> Result<()> {
        if data.is_empty() || data[0].is_empty() {
            return Err(TensorError::invalid_argument(
                "Cannot fit on empty data".to_string(),
            ));
        }

        let n_features = data[0].len();
        let n_samples = T::from(data.len()).expect("data length should convert to T");
        let mut selected = Vec::new();

        for feature_idx in 0..n_features {
            // Calculate mean
            let mut sum = T::zero();
            for sample in data {
                if feature_idx < sample.len() {
                    sum = sum + sample[feature_idx];
                }
            }
            let mean = sum / n_samples;

            // Calculate variance
            let mut variance_sum = T::zero();
            for sample in data {
                if feature_idx < sample.len() {
                    let diff = sample[feature_idx] - mean;
                    variance_sum = variance_sum + diff * diff;
                }
            }
            let variance = variance_sum / n_samples;

            if variance >= self.threshold {
                selected.push(feature_idx);
            }
        }

        self.selected_features = Some(selected);
        Ok(())
    }

    /// Get the indices of selected features
    pub fn get_selected_features(&self) -> &Option<Vec<usize>> {
        &self.selected_features
    }
}

impl<T> Transform<T> for VarianceThreshold<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Float
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable,
{
    fn apply(&self, sample: (Tensor<T>, Tensor<T>)) -> Result<(Tensor<T>, Tensor<T>)> {
        let (features, labels) = sample;

        if self.selected_features.is_none() {
            return Err(TensorError::invalid_argument(
                "VarianceThreshold must be fitted before use".to_string(),
            ));
        }

        let feature_data = features.as_slice().ok_or_else(|| {
            TensorError::invalid_argument(
                "Cannot access tensor data (GPU tensor not supported)".to_string(),
            )
        })?;

        let selected = self
            .selected_features
            .as_ref()
            .expect("selected_features should be fitted");
        let mut filtered_data = Vec::with_capacity(selected.len());

        for &idx in selected {
            if idx < feature_data.len() {
                filtered_data.push(feature_data[idx]);
            }
        }

        let filtered_features = Tensor::from_vec(filtered_data, &[selected.len()])?;
        Ok((filtered_features, labels))
    }
}

/// Feature scaling using power transformation
pub struct PowerTransform<T> {
    method: PowerMethod,
    fitted_lambdas: Option<Vec<T>>,
    _phantom: PhantomData<T>,
}

#[derive(Debug, Clone)]
pub enum PowerMethod {
    /// Box-Cox transformation (requires positive values)
    BoxCox,
    /// Yeo-Johnson transformation (handles negative values)
    YeoJohnson,
}

impl<T> PowerTransform<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Float
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable,
{
    pub fn new(method: PowerMethod) -> Self {
        Self {
            method,
            fitted_lambdas: None,
            _phantom: PhantomData,
        }
    }

    pub fn box_cox() -> Self {
        Self::new(PowerMethod::BoxCox)
    }

    pub fn yeo_johnson() -> Self {
        Self::new(PowerMethod::YeoJohnson)
    }

    /// Fit the transformer to find optimal lambda parameters
    pub fn fit(&mut self, data: &[T]) -> Result<()> {
        // For simplicity, we'll use a fixed lambda of 0.5 for Box-Cox
        // and 1.0 for Yeo-Johnson. In practice, you'd optimize these.
        let lambda = match self.method {
            PowerMethod::BoxCox => T::from(0.5).expect("power method default should convert to T"),
            PowerMethod::YeoJohnson => T::one(),
        };

        self.fitted_lambdas = Some(vec![lambda]);
        Ok(())
    }

    /// Apply Box-Cox transformation
    fn box_cox_transform(&self, value: T, lambda: T) -> T {
        if value <= T::zero() {
            return T::zero(); // Handle non-positive values
        }

        if lambda == T::zero() {
            value.ln()
        } else {
            let one = T::one();
            (value.powf(lambda) - one) / lambda
        }
    }

    /// Apply Yeo-Johnson transformation
    fn yeo_johnson_transform(&self, value: T, lambda: T) -> T {
        let one = T::one();
        let two = T::from(2.0).expect("constant 2.0 should convert to T");

        if value >= T::zero() {
            if lambda == T::zero() {
                (value + one).ln()
            } else {
                ((value + one).powf(lambda) - one) / lambda
            }
        } else {
            if lambda == two {
                -((-value + one).ln())
            } else {
                -((((-value) + one).powf(two - lambda) - one) / (two - lambda))
            }
        }
    }
}

impl<T> Transform<T> for PowerTransform<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Float
        + Send
        + Sync
        + 'static
        + bytemuck::Pod
        + bytemuck::Zeroable,
{
    fn apply(&self, sample: (Tensor<T>, Tensor<T>)) -> Result<(Tensor<T>, Tensor<T>)> {
        let (features, labels) = sample;

        if self.fitted_lambdas.is_none() {
            return Err(TensorError::invalid_argument(
                "PowerTransform must be fitted before use".to_string(),
            ));
        }

        let feature_data = features.as_slice().ok_or_else(|| {
            TensorError::invalid_argument(
                "Cannot access tensor data (GPU tensor not supported)".to_string(),
            )
        })?;

        let lambda = self
            .fitted_lambdas
            .as_ref()
            .expect("fitted_lambdas should be fitted")[0];
        let transformed_data: Vec<T> = feature_data
            .iter()
            .map(|&value| match self.method {
                PowerMethod::BoxCox => self.box_cox_transform(value, lambda),
                PowerMethod::YeoJohnson => self.yeo_johnson_transform(value, lambda),
            })
            .collect();

        let transformed_features = Tensor::from_vec(transformed_data, features.shape().dims())?;
        Ok((transformed_features, labels))
    }
}