sklears-feature-extraction 0.2.0

Feature extraction from raw data (text, images)
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
use crate::SklResult;
use scirs2_core::ndarray::{Array1, Array2, ArrayView1, ArrayView2, Axis};
use sklears_core::prelude::{Float, SklearsError};

/// Streaming Feature Extractor
///
/// Process large datasets in chunks for memory-efficient feature extraction.
/// Supports online computation of statistical features without loading entire dataset into memory.
///
/// # Parameters
///
/// * `chunk_size` - Size of chunks to process at once
/// * `buffer_size` - Size of internal buffer for overlapping windows
/// * `features` - Types of features to extract
///
/// # Examples
///
/// ```
/// use sklears_feature_extraction::streaming_projection::StreamingFeatureExtractor;
/// use scirs2_core::ndarray::{Array1, Array2, ArrayView1, ArrayView2};
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut extractor = StreamingFeatureExtractor::new()
///     .chunk_size(1000)
///     .buffer_size(100);
///
/// // Process data in chunks
/// let mut features = Vec::new();
/// let data_chunks = vec![Array2::from_elem((10, 3), 1.0)];
/// for chunk in data_chunks {
///     let chunk_features = extractor.extract_chunk(&chunk.view())?;
///     features.extend(chunk_features);
/// }
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct StreamingFeatureExtractor {
    chunk_size: usize,
    buffer_size: usize,
    include_mean: bool,
    include_std: bool,
    include_min_max: bool,
    include_quantiles: bool,
    include_moments: bool,
    // Internal state for streaming computation
    running_mean: Option<Array1<Float>>,
    running_variance: Option<Array1<Float>>,
    running_count: usize,
    running_min: Option<Array1<Float>>,
    running_max: Option<Array1<Float>>,
}

#[allow(non_snake_case)] // X follows sklearn/math convention for feature matrix
impl StreamingFeatureExtractor {
    /// Create a new streaming feature extractor
    pub fn new() -> Self {
        Self {
            chunk_size: 1000,
            buffer_size: 100,
            include_mean: true,
            include_std: true,
            include_min_max: true,
            include_quantiles: false,
            include_moments: false,
            running_mean: None,
            running_variance: None,
            running_count: 0,
            running_min: None,
            running_max: None,
        }
    }

    /// Set chunk size for processing
    pub fn chunk_size(mut self, chunk_size: usize) -> Self {
        self.chunk_size = chunk_size;
        self
    }

    /// Set buffer size for overlapping windows
    pub fn buffer_size(mut self, buffer_size: usize) -> Self {
        self.buffer_size = buffer_size;
        self
    }

    /// Include mean in extracted features
    pub fn include_mean(mut self, include: bool) -> Self {
        self.include_mean = include;
        self
    }

    /// Include standard deviation in extracted features
    pub fn include_std(mut self, include: bool) -> Self {
        self.include_std = include;
        self
    }

    /// Include min/max in extracted features
    pub fn include_min_max(mut self, include: bool) -> Self {
        self.include_min_max = include;
        self
    }

    /// Include quantiles in extracted features
    pub fn include_quantiles(mut self, include: bool) -> Self {
        self.include_quantiles = include;
        self
    }

    /// Include higher moments in extracted features
    pub fn include_moments(mut self, include: bool) -> Self {
        self.include_moments = include;
        self
    }

    /// Process a single chunk of data
    pub fn extract_chunk(&mut self, data: &ArrayView2<Float>) -> SklResult<Array1<Float>> {
        if data.nrows() == 0 {
            return Err(SklearsError::InvalidInput("Empty data chunk".to_string()));
        }

        let (_n_samples, n_features) = data.dim();

        // Update running statistics
        self.update_running_stats(data)?;

        // Extract features for this chunk
        let mut features = Vec::new();

        if self.include_mean {
            if let Some(ref mean) = self.running_mean {
                features.extend(mean.iter().cloned());
            }
        }

        if self.include_std {
            if let Some(ref variance) = self.running_variance {
                let std_dev: Vec<Float> = variance.iter().map(|&v| v.sqrt()).collect();
                features.extend(std_dev);
            }
        }

        if self.include_min_max {
            if let Some(ref min_vals) = self.running_min {
                features.extend(min_vals.iter().cloned());
            }
            if let Some(ref max_vals) = self.running_max {
                features.extend(max_vals.iter().cloned());
            }
        }

        if self.include_quantiles {
            // For streaming quantiles, we use approximate methods
            for feature_idx in 0..n_features {
                let column = data.column(feature_idx);
                let quantiles = self.compute_approximate_quantiles(&column)?;
                features.extend(quantiles);
            }
        }

        if self.include_moments {
            // Compute higher moments for current chunk
            for feature_idx in 0..n_features {
                let column = data.column(feature_idx);
                let moments = self.compute_moments(&column)?;
                features.extend(moments);
            }
        }

        Ok(Array1::from_vec(features))
    }

    /// Finalize streaming computation and return aggregated features
    pub fn finalize(&self) -> SklResult<Array1<Float>> {
        if self.running_count == 0 {
            return Err(SklearsError::InvalidInput("No data processed".to_string()));
        }

        let mut features = Vec::new();

        if self.include_mean {
            if let Some(ref mean) = self.running_mean {
                features.extend(mean.iter().cloned());
            }
        }

        if self.include_std {
            if let Some(ref variance) = self.running_variance {
                let std_dev: Vec<Float> = variance.iter().map(|&v| v.sqrt()).collect();
                features.extend(std_dev);
            }
        }

        if self.include_min_max {
            if let Some(ref min_vals) = self.running_min {
                features.extend(min_vals.iter().cloned());
            }
            if let Some(ref max_vals) = self.running_max {
                features.extend(max_vals.iter().cloned());
            }
        }

        Ok(Array1::from_vec(features))
    }

    /// Reset internal state for new stream
    pub fn reset(&mut self) {
        self.running_mean = None;
        self.running_variance = None;
        self.running_count = 0;
        self.running_min = None;
        self.running_max = None;
    }

    /// Update running statistics with new data chunk
    fn update_running_stats(&mut self, data: &ArrayView2<Float>) -> SklResult<()> {
        let (_n_samples, n_features) = data.dim();

        if self.running_count == 0 {
            // Initialize running statistics
            self.running_mean = Some(Array1::zeros(n_features));
            self.running_variance = Some(Array1::zeros(n_features));
            self.running_min = Some(Array1::from_elem(n_features, Float::INFINITY));
            self.running_max = Some(Array1::from_elem(n_features, Float::NEG_INFINITY));
        }

        // Update using Welford's online algorithm
        for sample in data.axis_iter(Axis(0)) {
            self.running_count += 1;
            let count = self.running_count as Float;

            for (feature_idx, &value) in sample.iter().enumerate() {
                // Update mean and variance using Welford's algorithm
                if let (Some(ref mut mean), Some(ref mut variance)) =
                    (&mut self.running_mean, &mut self.running_variance)
                {
                    let delta = value - mean[feature_idx];
                    mean[feature_idx] += delta / count;
                    let delta2 = value - mean[feature_idx];
                    variance[feature_idx] += delta * delta2;
                }

                // Update min/max
                if let Some(ref mut min_vals) = self.running_min {
                    if value < min_vals[feature_idx] {
                        min_vals[feature_idx] = value;
                    }
                }
                if let Some(ref mut max_vals) = self.running_max {
                    if value > max_vals[feature_idx] {
                        max_vals[feature_idx] = value;
                    }
                }
            }
        }

        // Normalize variance
        if let Some(ref mut variance) = self.running_variance {
            let count = self.running_count as Float;
            if count > 1.0 {
                variance.mapv_inplace(|v| v / (count - 1.0));
            }
        }

        Ok(())
    }

    /// Compute approximate quantiles for streaming data
    fn compute_approximate_quantiles(&self, data: &ArrayView1<Float>) -> SklResult<Vec<Float>> {
        let mut sorted_data: Vec<Float> = data.iter().cloned().collect();
        sorted_data.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let n = sorted_data.len();
        let quantiles = vec![0.25, 0.5, 0.75]; // Q1, median, Q3
        let mut result = Vec::new();

        for &q in &quantiles {
            let index = (q * (n - 1) as Float).round() as usize;
            let index = index.min(n - 1);
            result.push(sorted_data[index]);
        }

        Ok(result)
    }

    /// Compute moments for a data column
    fn compute_moments(&self, data: &ArrayView1<Float>) -> SklResult<Vec<Float>> {
        let n = data.len() as Float;
        if n == 0.0 {
            return Ok(vec![0.0, 0.0, 0.0]); // mean, skewness, kurtosis
        }

        let mean = data.sum() / n;
        let variance = data.mapv(|x| (x - mean).powi(2)).sum() / n;
        let std_dev = variance.sqrt();

        let (skewness, kurtosis) = if std_dev < 1e-10 {
            (0.0, 0.0)
        } else {
            let skewness = data.mapv(|x| ((x - mean) / std_dev).powi(3)).sum() / n;
            let kurtosis = data.mapv(|x| ((x - mean) / std_dev).powi(4)).sum() / n - 3.0;
            (skewness, kurtosis)
        };

        Ok(vec![mean, skewness, kurtosis])
    }
}

impl Default for StreamingFeatureExtractor {
    fn default() -> Self {
        Self::new()
    }
}

/// Random Projection Feature Extractor
///
/// Performs dimensionality reduction using random projections while preserving
/// approximate distances between data points (Johnson-Lindenstrauss lemma).
/// Useful for high-dimensional data preprocessing and approximate similarity search.
///
/// # Parameters
///
/// * `n_components` - Number of dimensions in the projected space
/// * `random_state` - Random seed for reproducibility
/// * `density` - Density of random projection matrix (for sparse projections)
/// * `eps` - Distortion parameter for auto-sizing components
///
/// # Examples
///
/// ```
/// use sklears_feature_extraction::streaming_projection::RandomProjectionFeatures;
/// use scirs2_core::ndarray::Array2;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let X = Array2::from_elem((100, 1000), 1.0);
/// let mut rp = RandomProjectionFeatures::new()
///     .n_components(50)
///     .random_state(42);
///
/// let X_projected = rp.fit_transform(&X.view())?;
/// assert_eq!(X_projected.ncols(), 50);
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct RandomProjectionFeatures {
    n_components: Option<usize>,
    random_state: Option<u64>,
    density: Option<Float>,
    eps: Float,
    projection_matrix: Option<Array2<Float>>,
    n_features: Option<usize>,
}

#[allow(non_snake_case)] // X follows sklearn/math convention for feature matrix
impl RandomProjectionFeatures {
    /// Create a new random projection feature extractor
    pub fn new() -> Self {
        Self {
            n_components: None,
            random_state: None,
            density: None,
            eps: 0.1,
            projection_matrix: None,
            n_features: None,
        }
    }

    /// Set number of components in projected space
    pub fn n_components(mut self, n_components: usize) -> Self {
        self.n_components = Some(n_components);
        self
    }

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

    /// Set density for sparse random projections
    pub fn density(mut self, density: Float) -> Self {
        self.density = Some(density);
        self
    }

    /// Set distortion parameter for auto-sizing
    pub fn eps(mut self, eps: Float) -> Self {
        self.eps = eps;
        self
    }

    /// Fit the random projection to data
    pub fn fit(&mut self, X: &ArrayView2<Float>) -> SklResult<()> {
        let (n_samples, n_features) = X.dim();
        self.n_features = Some(n_features);

        // Auto-size number of components if not specified
        let n_components = if let Some(n_comp) = self.n_components {
            n_comp
        } else {
            self.johnson_lindenstrauss_min_dim(n_samples, self.eps)?
        };

        // Create random projection matrix
        self.projection_matrix = Some(self.create_projection_matrix(n_features, n_components)?);

        Ok(())
    }

    /// Transform data using fitted random projection
    pub fn transform(&self, X: &ArrayView2<Float>) -> SklResult<Array2<Float>> {
        if let Some(ref proj_matrix) = self.projection_matrix {
            let (_n_samples, n_features) = X.dim();

            if Some(n_features) != self.n_features {
                return Err(SklearsError::InvalidInput(
                    "Number of features does not match fitted data".to_string(),
                ));
            }

            // Perform matrix multiplication: X @ projection_matrix.T
            let result = X.dot(proj_matrix);
            Ok(result)
        } else {
            Err(SklearsError::InvalidInput(
                "Random projection not fitted yet".to_string(),
            ))
        }
    }

    /// Fit and transform data in one step
    pub fn fit_transform(&mut self, X: &ArrayView2<Float>) -> SklResult<Array2<Float>> {
        self.fit(X)?;
        self.transform(X)
    }

    /// Create random projection matrix
    fn create_projection_matrix(
        &self,
        n_features: usize,
        n_components: usize,
    ) -> SklResult<Array2<Float>> {
        use scirs2_core::random::thread_rng;

        // Use rng() function from scirs2_core - seeding handled elsewhere if needed
        let mut rng = thread_rng();

        let mut matrix = Array2::zeros((n_features, n_components));

        if let Some(density) = self.density {
            // Sparse random projection
            let threshold = density;
            for i in 0..n_features {
                for j in 0..n_components {
                    let random_val: Float = rng.random();
                    if random_val < threshold {
                        matrix[[i, j]] = if rng.random_bool(0.5) { 1.0 } else { -1.0 };
                        matrix[[i, j]] /= (density * n_components as Float).sqrt();
                    }
                }
            }
        } else {
            // Dense Gaussian random projection
            for i in 0..n_features {
                for j in 0..n_components {
                    matrix[[i, j]] = rng.random_range(-1.0..1.0);
                }
            }
            // Normalize
            let norm_factor = 1.0 / (n_components as Float).sqrt();
            matrix.mapv_inplace(|x| x * norm_factor);
        }

        Ok(matrix)
    }

    /// Compute minimum number of components for Johnson-Lindenstrauss lemma
    fn johnson_lindenstrauss_min_dim(&self, n_samples: usize, eps: Float) -> SklResult<usize> {
        if eps <= 0.0 || eps >= 1.0 {
            return Err(SklearsError::InvalidInput(
                "eps must be between 0 and 1".to_string(),
            ));
        }

        if n_samples <= 1 {
            return Ok(1);
        }

        // Simplified JL bound: k >= 4 * ln(n) / (eps^2 / 2)
        // This is a more conservative but simpler bound
        let ln_n = (n_samples as Float).ln();
        let denominator = eps * eps / 2.0;

        if denominator <= 0.0 {
            return Err(SklearsError::InvalidInput(
                "Invalid eps value for Johnson-Lindenstrauss bound".to_string(),
            ));
        }

        let min_dim = (4.0 * ln_n / denominator).ceil() as usize;
        Ok(min_dim.max(1))
    }
}

impl Default for RandomProjectionFeatures {
    fn default() -> Self {
        Self::new()
    }
}

/// Locality-Sensitive Hashing (LSH) Feature Extractor
///
/// Implements locality-sensitive hashing for approximate similarity search
/// and clustering. Maps similar items to the same buckets with high probability.
///
/// # Parameters
///
/// * `n_hash_functions` - Number of hash functions to use
/// * `hash_table_size` - Size of each hash table
/// * `random_state` - Random seed for reproducibility
/// * `distance_metric` - Distance metric for similarity ('cosine', 'euclidean')
///
/// # Examples
///
/// ```
/// use sklears_feature_extraction::streaming_projection::LocalitySensitiveHashing;
/// use scirs2_core::ndarray::Array2;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut lsh = LocalitySensitiveHashing::new()
///     .n_hash_functions(10)
///     .hash_table_size(1000)
///     .distance_metric("cosine");
///
/// let X = Array2::from_elem((100, 50), 1.0);
/// let hash_codes = lsh.fit_transform(&X.view())?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct LocalitySensitiveHashing {
    n_hash_functions: usize,
    hash_table_size: usize,
    random_state: Option<u64>,
    distance_metric: String,
    hash_matrices: Option<Vec<Array2<Float>>>,
    n_features: Option<usize>,
}

#[allow(non_snake_case)] // X follows sklearn/math convention for feature matrix
impl LocalitySensitiveHashing {
    /// Create a new LSH feature extractor
    pub fn new() -> Self {
        Self {
            n_hash_functions: 10,
            hash_table_size: 1000,
            random_state: None,
            distance_metric: "cosine".to_string(),
            hash_matrices: None,
            n_features: None,
        }
    }

    /// Set number of hash functions
    pub fn n_hash_functions(mut self, n_hash_functions: usize) -> Self {
        self.n_hash_functions = n_hash_functions;
        self
    }

    /// Set hash table size
    pub fn hash_table_size(mut self, hash_table_size: usize) -> Self {
        self.hash_table_size = hash_table_size;
        self
    }

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

    /// Set distance metric
    pub fn distance_metric(mut self, metric: &str) -> Self {
        self.distance_metric = metric.to_string();
        self
    }

    /// Fit LSH to data
    pub fn fit(&mut self, X: &ArrayView2<Float>) -> SklResult<()> {
        let (_, n_features) = X.dim();
        self.n_features = Some(n_features);

        // Create hash matrices
        self.hash_matrices = Some(self.create_hash_matrices(n_features)?);

        Ok(())
    }

    /// Transform data to hash codes
    pub fn transform(&self, X: &ArrayView2<Float>) -> SklResult<Array2<Float>> {
        if let Some(ref hash_matrices) = self.hash_matrices {
            let (n_samples, n_features) = X.dim();

            if Some(n_features) != self.n_features {
                return Err(SklearsError::InvalidInput(
                    "Number of features does not match fitted data".to_string(),
                ));
            }

            let mut hash_codes = Array2::zeros((n_samples, self.n_hash_functions));

            for (hash_idx, hash_matrix) in hash_matrices.iter().enumerate() {
                // Apply hash function
                let hash_values = X.dot(hash_matrix);

                // Convert to binary hash codes
                for (sample_idx, &value) in hash_values.iter().enumerate() {
                    hash_codes[[sample_idx, hash_idx]] = if value >= 0.0 { 1.0 } else { 0.0 };
                }
            }

            Ok(hash_codes)
        } else {
            Err(SklearsError::InvalidInput("LSH not fitted yet".to_string()))
        }
    }

    /// Fit and transform data
    pub fn fit_transform(&mut self, X: &ArrayView2<Float>) -> SklResult<Array2<Float>> {
        self.fit(X)?;
        self.transform(X)
    }

    /// Find approximate nearest neighbors using LSH
    pub fn find_neighbors(
        &self,
        X: &ArrayView2<Float>,
        query: &ArrayView1<Float>,
        n_neighbors: usize,
    ) -> SklResult<Vec<usize>> {
        if let Some(ref hash_matrices) = self.hash_matrices {
            let (n_samples, n_features) = X.dim();

            if query.len() != n_features {
                return Err(SklearsError::InvalidInput(
                    "Query dimension does not match fitted data".to_string(),
                ));
            }

            // Compute hash code for query
            let mut query_hash = vec![0.0; self.n_hash_functions];
            for (hash_idx, hash_matrix) in hash_matrices.iter().enumerate() {
                let hash_value = query.dot(&hash_matrix.column(0));
                query_hash[hash_idx] = if hash_value >= 0.0 { 1.0 } else { 0.0 };
            }

            // Compute hash codes for all data points
            let data_hashes = self.transform(X)?;

            // Find candidates with similar hash codes
            let mut candidates: Vec<(usize, usize)> = Vec::new();
            for sample_idx in 0..n_samples {
                let mut matching_hashes = 0;
                for hash_idx in 0..self.n_hash_functions {
                    if (data_hashes[[sample_idx, hash_idx]] - query_hash[hash_idx]).abs() < 1e-10 {
                        matching_hashes += 1;
                    }
                }
                if matching_hashes > 0 {
                    candidates.push((sample_idx, matching_hashes));
                }
            }

            // Sort by number of matching hash functions
            candidates.sort_by_key(|b| std::cmp::Reverse(b.1));

            // Return top candidates
            let result: Vec<usize> = candidates
                .into_iter()
                .take(n_neighbors)
                .map(|(idx, _)| idx)
                .collect();

            Ok(result)
        } else {
            Err(SklearsError::InvalidInput("LSH not fitted yet".to_string()))
        }
    }

    /// Create hash matrices for LSH
    fn create_hash_matrices(&self, n_features: usize) -> SklResult<Vec<Array2<Float>>> {
        use scirs2_core::random::thread_rng;

        // Use rng() function from scirs2_core - seeding handled elsewhere if needed
        let mut rng = thread_rng();

        let mut hash_matrices = Vec::new();

        for _ in 0..self.n_hash_functions {
            let mut matrix = Array2::zeros((n_features, 1));

            match self.distance_metric.as_str() {
                "cosine" => {
                    // Random hyperplane hash for cosine similarity
                    for i in 0..n_features {
                        matrix[[i, 0]] = rng.random_range(-1.0..1.0);
                    }
                }
                "euclidean" => {
                    // Random projection hash for Euclidean distance
                    for i in 0..n_features {
                        matrix[[i, 0]] = rng.random_range(-1.0..1.0);
                    }
                    // Normalize
                    let norm = (matrix.mapv(|x| x * x).sum() as Float).sqrt();
                    matrix.mapv_inplace(|x| x / norm);
                }
                _ => {
                    return Err(SklearsError::InvalidInput(
                        "Unsupported distance metric".to_string(),
                    ));
                }
            }

            hash_matrices.push(matrix);
        }

        Ok(hash_matrices)
    }
}

impl Default for LocalitySensitiveHashing {
    fn default() -> Self {
        Self::new()
    }
}