sklears-semi-supervised 0.1.2

Semi-supervised learning algorithms
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
//! Landmark-based methods for large-scale semi-supervised learning
//!
//! This module provides landmark-based algorithms that scale to very large datasets
//! by selecting representative points (landmarks) and building graphs based on
//! relationships to these landmarks rather than all pairwise relationships.

use scirs2_core::ndarray_ext::{Array1, Array2, ArrayView1, ArrayView2};
use scirs2_core::random::rand_prelude::*;
use scirs2_core::random::{Random, RngExt};
use sklears_core::error::SklearsError;
use std::collections::HashMap;

/// Landmark-based graph construction for scalable semi-supervised learning
#[derive(Clone)]
pub struct LandmarkGraphConstruction {
    /// Number of landmarks to select
    pub n_landmarks: usize,
    /// Number of neighbors to connect to each landmark
    pub k_neighbors: usize,
    /// Landmark selection strategy: "random", "kmeans", "farthest_first", "density_based"
    pub selection_strategy: String,
    /// Graph construction method: "knn_to_landmarks", "rbf_to_landmarks", "interpolation"
    pub construction_method: String,
    /// Bandwidth parameter for RBF connections
    pub bandwidth: f64,
    /// Maximum iterations for k-means landmark selection
    pub max_iter: usize,
    /// Random state for reproducibility
    pub random_state: Option<u64>,
}

impl LandmarkGraphConstruction {
    /// Create a new landmark graph construction instance
    pub fn new() -> Self {
        Self {
            n_landmarks: 100,
            k_neighbors: 5,
            selection_strategy: "kmeans".to_string(),
            construction_method: "knn_to_landmarks".to_string(),
            bandwidth: 1.0,
            max_iter: 100,
            random_state: None,
        }
    }

    /// Set the number of landmarks
    pub fn n_landmarks(mut self, n: usize) -> Self {
        self.n_landmarks = n;
        self
    }

    /// Set the number of neighbors per landmark
    pub fn k_neighbors(mut self, k: usize) -> Self {
        self.k_neighbors = k;
        self
    }

    /// Set the landmark selection strategy
    pub fn selection_strategy(mut self, strategy: String) -> Self {
        self.selection_strategy = strategy;
        self
    }

    /// Set the graph construction method
    pub fn construction_method(mut self, method: String) -> Self {
        self.construction_method = method;
        self
    }

    /// Set the bandwidth parameter
    pub fn bandwidth(mut self, bw: f64) -> Self {
        self.bandwidth = bw;
        self
    }

    /// Set the maximum iterations
    pub fn max_iter(mut self, max_iter: usize) -> Self {
        self.max_iter = max_iter;
        self
    }

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

    /// Construct landmark-based graph
    #[allow(non_snake_case)] // standard ML notation
    pub fn fit(&self, X: &ArrayView2<f64>) -> Result<LandmarkGraphResult, SklearsError> {
        let n_samples = X.nrows();

        if n_samples == 0 {
            return Err(SklearsError::InvalidInput(
                "No samples provided".to_string(),
            ));
        }

        let effective_landmarks = self.n_landmarks.min(n_samples);

        let mut rng = Random::default();

        // Select landmarks
        let (landmark_indices, landmarks) =
            self.select_landmarks(X, effective_landmarks, &mut rng)?;

        // Construct graph based on landmarks
        let adjacency_matrix = self.construct_landmark_graph(X, &landmarks, &landmark_indices)?;

        Ok(LandmarkGraphResult {
            adjacency_matrix,
            landmark_indices,
            landmarks,
        })
    }

    /// Select landmarks using different strategies
    #[allow(non_snake_case)] // standard ML notation
    fn select_landmarks(
        &self,
        X: &ArrayView2<f64>,
        n_landmarks: usize,
        rng: &mut Random,
    ) -> Result<(Vec<usize>, Array2<f64>), SklearsError> {
        match self.selection_strategy.as_str() {
            "random" => self.random_landmarks(X, n_landmarks, rng),
            "kmeans" => self.kmeans_landmarks(X, n_landmarks, rng),
            "farthest_first" => self.farthest_first_landmarks(X, n_landmarks, rng),
            "density_based" => self.density_based_landmarks(X, n_landmarks, rng),
            _ => Err(SklearsError::InvalidInput(format!(
                "Unknown selection strategy: {}",
                self.selection_strategy
            ))),
        }
    }

    /// Random landmark selection
    #[allow(non_snake_case)] // standard ML notation
    fn random_landmarks(
        &self,
        X: &ArrayView2<f64>,
        n_landmarks: usize,
        rng: &mut Random,
    ) -> Result<(Vec<usize>, Array2<f64>), SklearsError> {
        let n_samples = X.nrows();
        let indices: Vec<usize> = (0..n_samples)
            .sample(rng, n_landmarks)
            .into_iter()
            .collect();

        let mut landmarks = Array2::<f64>::zeros((n_landmarks, X.ncols()));
        for (i, &idx) in indices.iter().enumerate() {
            landmarks.row_mut(i).assign(&X.row(idx));
        }

        Ok((indices, landmarks))
    }

    /// K-means based landmark selection
    #[allow(non_snake_case)] // standard ML notation
    fn kmeans_landmarks(
        &self,
        X: &ArrayView2<f64>,
        n_landmarks: usize,
        rng: &mut Random,
    ) -> Result<(Vec<usize>, Array2<f64>), SklearsError> {
        let n_samples = X.nrows();
        let n_features = X.ncols();

        // Initialize centroids randomly
        let mut centroids = Array2::<f64>::zeros((n_landmarks, n_features));
        for i in 0..n_landmarks {
            let sample_idx = rng.gen_range(0..n_samples);
            centroids.row_mut(i).assign(&X.row(sample_idx));
        }

        let mut labels = Array1::<usize>::zeros(n_samples);

        // K-means iterations
        for _iter in 0..self.max_iter {
            let mut changed = false;

            // Assign points to nearest centroids
            for i in 0..n_samples {
                let mut min_dist = f64::INFINITY;
                let mut best_cluster = 0;

                for k in 0..n_landmarks {
                    let dist = self.euclidean_distance(&X.row(i), &centroids.row(k));
                    if dist < min_dist {
                        min_dist = dist;
                        best_cluster = k;
                    }
                }

                if labels[i] != best_cluster {
                    labels[i] = best_cluster;
                    changed = true;
                }
            }

            if !changed {
                break;
            }

            // Update centroids
            for k in 0..n_landmarks {
                let mut count = 0;
                let mut sum = Array1::<f64>::zeros(n_features);

                for i in 0..n_samples {
                    if labels[i] == k {
                        count += 1;
                        for j in 0..n_features {
                            sum[j] += X[[i, j]];
                        }
                    }
                }

                if count > 0 {
                    for j in 0..n_features {
                        centroids[[k, j]] = sum[j] / count as f64;
                    }
                }
            }
        }

        // Find closest actual points to centroids as landmarks
        let mut landmark_indices = Vec::new();
        for k in 0..n_landmarks {
            let mut min_dist = f64::INFINITY;
            let mut closest_idx = 0;

            for i in 0..n_samples {
                let dist = self.euclidean_distance(&X.row(i), &centroids.row(k));
                if dist < min_dist {
                    min_dist = dist;
                    closest_idx = i;
                }
            }

            landmark_indices.push(closest_idx);
        }

        // Remove duplicates and get unique landmarks
        landmark_indices.sort_unstable();
        landmark_indices.dedup();

        // If we have fewer unique landmarks than requested, add random ones
        while landmark_indices.len() < n_landmarks {
            let new_idx = rng.gen_range(0..n_samples);
            if !landmark_indices.contains(&new_idx) {
                landmark_indices.push(new_idx);
            }
        }

        let mut landmarks = Array2::<f64>::zeros((landmark_indices.len(), n_features));
        for (i, &idx) in landmark_indices.iter().enumerate() {
            landmarks.row_mut(i).assign(&X.row(idx));
        }

        Ok((landmark_indices, landmarks))
    }

    /// Farthest-first landmark selection
    #[allow(non_snake_case)] // standard ML notation
    fn farthest_first_landmarks(
        &self,
        X: &ArrayView2<f64>,
        n_landmarks: usize,
        rng: &mut Random,
    ) -> Result<(Vec<usize>, Array2<f64>), SklearsError> {
        let n_samples = X.nrows();
        let n_features = X.ncols();

        let mut landmark_indices = Vec::new();

        // Select first landmark randomly
        let first_idx = rng.gen_range(0..n_samples);
        landmark_indices.push(first_idx);

        // Select remaining landmarks by maximizing minimum distance
        for _ in 1..n_landmarks {
            let mut max_min_dist = 0.0;
            let mut best_idx = 0;

            for i in 0..n_samples {
                if landmark_indices.contains(&i) {
                    continue;
                }

                // Find minimum distance to existing landmarks
                let mut min_dist = f64::INFINITY;
                for &landmark_idx in &landmark_indices {
                    let dist = self.euclidean_distance(&X.row(i), &X.row(landmark_idx));
                    if dist < min_dist {
                        min_dist = dist;
                    }
                }

                // Update if this point has larger minimum distance
                if min_dist > max_min_dist {
                    max_min_dist = min_dist;
                    best_idx = i;
                }
            }

            landmark_indices.push(best_idx);
        }

        let mut landmarks = Array2::<f64>::zeros((n_landmarks, n_features));
        for (i, &idx) in landmark_indices.iter().enumerate() {
            landmarks.row_mut(i).assign(&X.row(idx));
        }

        Ok((landmark_indices, landmarks))
    }

    /// Density-based landmark selection
    #[allow(non_snake_case)] // standard ML notation
    fn density_based_landmarks(
        &self,
        X: &ArrayView2<f64>,
        n_landmarks: usize,
        rng: &mut Random,
    ) -> Result<(Vec<usize>, Array2<f64>), SklearsError> {
        let n_samples = X.nrows();
        let n_features = X.ncols();

        // Estimate local density for each point
        let mut densities = Array1::<f64>::zeros(n_samples);
        let radius = self.estimate_density_radius(X)?;

        for i in 0..n_samples {
            let mut neighbor_count = 0;
            for j in 0..n_samples {
                if i != j {
                    let dist = self.euclidean_distance(&X.row(i), &X.row(j));
                    if dist <= radius {
                        neighbor_count += 1;
                    }
                }
            }
            densities[i] = neighbor_count as f64;
        }

        // Select landmarks with probability proportional to density
        let mut landmark_indices = Vec::new();
        let total_density: f64 = densities.sum();

        if total_density > 0.0 {
            for _iteration in 0..n_landmarks {
                let threshold = rng.random::<f64>() * total_density;
                let mut cumulative = 0.0;
                let previous_len = landmark_indices.len();

                for i in 0..n_samples {
                    if landmark_indices.contains(&i) {
                        continue;
                    }

                    cumulative += densities[i];
                    if cumulative >= threshold {
                        landmark_indices.push(i);
                        break;
                    }
                }

                // If we couldn't find a new landmark, add a random one
                if landmark_indices.len() == previous_len {
                    for i in 0..n_samples {
                        if !landmark_indices.contains(&i) {
                            landmark_indices.push(i);
                            break;
                        }
                    }
                }
            }
        } else {
            // Fallback to random selection if density calculation fails
            return self.random_landmarks(X, n_landmarks, rng);
        }

        let mut landmarks = Array2::<f64>::zeros((landmark_indices.len(), n_features));
        for (i, &idx) in landmark_indices.iter().enumerate() {
            landmarks.row_mut(i).assign(&X.row(idx));
        }

        Ok((landmark_indices, landmarks))
    }

    /// Estimate radius for density computation
    #[allow(non_snake_case)] // standard ML notation
    fn estimate_density_radius(&self, X: &ArrayView2<f64>) -> Result<f64, SklearsError> {
        let n_samples = X.nrows();
        let sample_size = (n_samples / 10).clamp(10, 100);

        let mut distances = Vec::new();

        // Sample pairwise distances
        for i in 0..sample_size {
            for j in (i + 1)..sample_size {
                if i < n_samples && j < n_samples {
                    let dist = self.euclidean_distance(&X.row(i), &X.row(j));
                    distances.push(dist);
                }
            }
        }

        if distances.is_empty() {
            return Ok(1.0);
        }

        distances.sort_by(|a, b| a.partial_cmp(b).expect("operation should succeed"));

        // Use median distance as radius
        let median_idx = distances.len() / 2;
        Ok(distances[median_idx])
    }

    /// Construct graph based on landmarks
    #[allow(non_snake_case)] // standard ML notation
    fn construct_landmark_graph(
        &self,
        X: &ArrayView2<f64>,
        landmarks: &Array2<f64>,
        landmark_indices: &[usize],
    ) -> Result<Array2<f64>, SklearsError> {
        match self.construction_method.as_str() {
            "knn_to_landmarks" => self.knn_to_landmarks_graph(X, landmarks, landmark_indices),
            "rbf_to_landmarks" => self.rbf_to_landmarks_graph(X, landmarks, landmark_indices),
            "interpolation" => self.interpolation_graph(X, landmarks, landmark_indices),
            _ => Err(SklearsError::InvalidInput(format!(
                "Unknown construction method: {}",
                self.construction_method
            ))),
        }
    }

    /// K-NN to landmarks graph construction
    #[allow(non_snake_case)] // standard ML notation
    fn knn_to_landmarks_graph(
        &self,
        X: &ArrayView2<f64>,
        landmarks: &Array2<f64>,
        _landmark_indices: &[usize],
    ) -> Result<Array2<f64>, SklearsError> {
        let n_samples = X.nrows();
        let n_landmarks = landmarks.nrows();
        let mut adjacency = Array2::<f64>::zeros((n_samples, n_samples));

        for i in 0..n_samples {
            // Find k nearest landmarks for each point
            let mut landmark_distances: Vec<(f64, usize)> = Vec::new();

            for j in 0..n_landmarks {
                let dist = self.euclidean_distance(&X.row(i), &landmarks.row(j));
                landmark_distances.push((dist, j));
            }

            landmark_distances
                .sort_by(|a, b| a.0.partial_cmp(&b.0).expect("operation should succeed"));

            let k_landmarks = self.k_neighbors.min(n_landmarks);
            let mut weights = Vec::new();
            let mut total_weight = 0.0;
            #[allow(clippy::needless_range_loop)]
            for k in 0..k_landmarks {
                let weight =
                    (-landmark_distances[k].0.powi(2) / (2.0 * self.bandwidth.powi(2))).exp();
                weights.push(weight);
                total_weight += weight;
            }

            // Normalize weights
            if total_weight > 0.0 {
                for weight in &mut weights {
                    *weight /= total_weight;
                }
            }

            // Connect to other points through shared landmarks
            for j in (i + 1)..n_samples {
                let mut shared_weight = 0.0;

                // Find landmarks for point j
                let mut j_landmark_distances: Vec<(f64, usize)> = Vec::new();
                for l in 0..n_landmarks {
                    let dist = self.euclidean_distance(&X.row(j), &landmarks.row(l));
                    j_landmark_distances.push((dist, l));
                }
                j_landmark_distances
                    .sort_by(|a, b| a.0.partial_cmp(&b.0).expect("operation should succeed"));

                let mut j_weights = Vec::new();
                let mut j_total_weight = 0.0;

                #[allow(clippy::needless_range_loop)]
                for k in 0..k_landmarks {
                    let weight =
                        (-j_landmark_distances[k].0.powi(2) / (2.0 * self.bandwidth.powi(2))).exp();
                    j_weights.push(weight);
                    j_total_weight += weight;
                }

                // Normalize weights for point j
                if j_total_weight > 0.0 {
                    for weight in &mut j_weights {
                        *weight /= j_total_weight;
                    }
                }

                // Compute shared landmark weight
                for k in 0..k_landmarks {
                    for l in 0..k_landmarks {
                        if landmark_distances[k].1 == j_landmark_distances[l].1 {
                            shared_weight += weights[k] * j_weights[l];
                        }
                    }
                }

                adjacency[[i, j]] = shared_weight;
                adjacency[[j, i]] = shared_weight;
            }
        }

        Ok(adjacency)
    }

    /// RBF to landmarks graph construction
    #[allow(non_snake_case)] // standard ML notation
    fn rbf_to_landmarks_graph(
        &self,
        X: &ArrayView2<f64>,
        landmarks: &Array2<f64>,
        _landmark_indices: &[usize],
    ) -> Result<Array2<f64>, SklearsError> {
        let n_samples = X.nrows();
        let n_landmarks = landmarks.nrows();
        let mut adjacency = Array2::<f64>::zeros((n_samples, n_samples));

        // Compute point-to-landmark weights
        let mut point_landmark_weights = Array2::<f64>::zeros((n_samples, n_landmarks));

        for i in 0..n_samples {
            let mut total_weight = 0.0;
            for j in 0..n_landmarks {
                let dist = self.euclidean_distance(&X.row(i), &landmarks.row(j));
                let weight = (-dist.powi(2) / (2.0 * self.bandwidth.powi(2))).exp();
                point_landmark_weights[[i, j]] = weight;
                total_weight += weight;
            }

            // Normalize weights
            if total_weight > 0.0 {
                for j in 0..n_landmarks {
                    point_landmark_weights[[i, j]] /= total_weight;
                }
            }
        }

        // Compute point-to-point similarities through landmarks
        for i in 0..n_samples {
            for j in (i + 1)..n_samples {
                let mut similarity = 0.0;

                for l in 0..n_landmarks {
                    similarity += point_landmark_weights[[i, l]] * point_landmark_weights[[j, l]];
                }

                adjacency[[i, j]] = similarity;
                adjacency[[j, i]] = similarity;
            }
        }

        Ok(adjacency)
    }

    /// Interpolation-based graph construction
    #[allow(non_snake_case)] // standard ML notation
    fn interpolation_graph(
        &self,
        X: &ArrayView2<f64>,
        _landmarks: &Array2<f64>,
        landmark_indices: &[usize],
    ) -> Result<Array2<f64>, SklearsError> {
        let n_samples = X.nrows();
        let mut adjacency = Array2::<f64>::zeros((n_samples, n_samples));

        // Connect landmarks to each other first
        for i in 0..landmark_indices.len() {
            for j in (i + 1)..landmark_indices.len() {
                let idx_i = landmark_indices[i];
                let idx_j = landmark_indices[j];
                let dist = self.euclidean_distance(&X.row(idx_i), &X.row(idx_j));
                let weight = (-dist.powi(2) / (2.0 * self.bandwidth.powi(2))).exp();
                adjacency[[idx_i, idx_j]] = weight;
                adjacency[[idx_j, idx_i]] = weight;
            }
        }

        // Connect non-landmark points to landmarks and interpolate
        for i in 0..n_samples {
            if landmark_indices.contains(&i) {
                continue; // Skip landmarks
            }

            // Find nearest landmarks
            let mut landmark_distances: Vec<(f64, usize)> = Vec::new();
            for &landmark_idx in landmark_indices.iter() {
                let dist = self.euclidean_distance(&X.row(i), &X.row(landmark_idx));
                landmark_distances.push((dist, landmark_idx));
            }

            landmark_distances
                .sort_by(|a, b| a.0.partial_cmp(&b.0).expect("operation should succeed"));

            let k_connect = self.k_neighbors.min(landmark_distances.len());

            // Connect to nearest landmarks
            #[allow(clippy::needless_range_loop)]
            for k in 0..k_connect {
                let landmark_idx = landmark_distances[k].1;
                let weight =
                    (-landmark_distances[k].0.powi(2) / (2.0 * self.bandwidth.powi(2))).exp();
                adjacency[[i, landmark_idx]] = weight;
                adjacency[[landmark_idx, i]] = weight;
            }
        }

        Ok(adjacency)
    }

    /// Compute Euclidean distance between two vectors
    fn euclidean_distance(&self, x1: &ArrayView1<f64>, x2: &ArrayView1<f64>) -> f64 {
        x1.iter()
            .zip(x2.iter())
            .map(|(a, b)| (a - b).powi(2))
            .sum::<f64>()
            .sqrt()
    }
}

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

/// Result of landmark graph construction
#[derive(Clone, Debug)]
pub struct LandmarkGraphResult {
    /// The constructed adjacency matrix
    pub adjacency_matrix: Array2<f64>,
    /// Indices of selected landmarks in original data
    pub landmark_indices: Vec<usize>,
    /// Landmark points
    pub landmarks: Array2<f64>,
}

impl LandmarkGraphResult {
    /// Get the number of landmarks
    pub fn n_landmarks(&self) -> usize {
        self.landmarks.nrows()
    }

    /// Get the sparsity of the resulting graph
    pub fn sparsity(&self) -> f64 {
        let total_edges = self.adjacency_matrix.len();
        let non_zero_edges = self.adjacency_matrix.iter().filter(|&&x| x > 0.0).count();
        1.0 - (non_zero_edges as f64 / total_edges as f64)
    }

    /// Get landmark coverage statistics
    pub fn landmark_coverage(&self) -> HashMap<String, f64> {
        let mut stats = HashMap::new();
        let n_samples = self.adjacency_matrix.nrows();
        let n_landmarks = self.landmarks.nrows();

        stats.insert(
            "landmark_ratio".to_string(),
            n_landmarks as f64 / n_samples as f64,
        );
        stats.insert("sparsity".to_string(), self.sparsity());
        stats.insert("n_landmarks".to_string(), n_landmarks as f64);
        stats.insert("n_samples".to_string(), n_samples as f64);

        stats
    }
}

/// Landmark-based label propagation for large-scale semi-supervised learning
#[derive(Clone)]
pub struct LandmarkLabelPropagation {
    /// Graph construction parameters
    pub graph_constructor: LandmarkGraphConstruction,
    /// Maximum iterations for label propagation
    pub max_iter: usize,
    /// Convergence tolerance
    pub tolerance: f64,
    /// Alpha parameter for label spreading
    pub alpha: f64,
}

impl LandmarkLabelPropagation {
    /// Create a new landmark label propagation instance
    pub fn new() -> Self {
        Self {
            graph_constructor: LandmarkGraphConstruction::new(),
            max_iter: 1000,
            tolerance: 1e-6,
            alpha: 0.2,
        }
    }

    /// Set the graph constructor
    pub fn graph_constructor(mut self, constructor: LandmarkGraphConstruction) -> Self {
        self.graph_constructor = constructor;
        self
    }

    /// Set the maximum iterations
    pub fn max_iter(mut self, max_iter: usize) -> Self {
        self.max_iter = max_iter;
        self
    }

    /// Set the convergence tolerance
    pub fn tolerance(mut self, tol: f64) -> Self {
        self.tolerance = tol;
        self
    }

    /// Set the alpha parameter
    pub fn alpha(mut self, alpha: f64) -> Self {
        self.alpha = alpha;
        self
    }

    /// Perform landmark-based label propagation
    #[allow(non_snake_case)] // standard ML notation
    pub fn fit_predict(
        &self,
        X: &ArrayView2<f64>,
        y: &ArrayView1<i32>,
    ) -> Result<Array1<i32>, SklearsError> {
        let n_samples = X.nrows();

        if y.len() != n_samples {
            return Err(SklearsError::ShapeMismatch {
                expected: format!("X and y should have same number of samples: {}", n_samples),
                actual: format!("X has {} samples, y has {} samples", n_samples, y.len()),
            });
        }

        // Construct landmark graph
        let graph_result = self.graph_constructor.fit(X)?;

        // Perform label propagation on the landmark graph
        self.propagate_labels(&graph_result.adjacency_matrix, y)
    }

    /// Propagate labels on the constructed graph
    #[allow(non_snake_case)]
    fn propagate_labels(
        &self,
        adjacency: &Array2<f64>,
        y: &ArrayView1<i32>,
    ) -> Result<Array1<i32>, SklearsError> {
        let n_samples = adjacency.nrows();

        // Identify labeled and unlabeled samples
        let labeled_mask: Array1<bool> = y.iter().map(|&label| label != -1).collect();
        let unique_labels: Vec<i32> = y
            .iter()
            .filter(|&&label| label != -1)
            .cloned()
            .collect::<std::collections::HashSet<_>>()
            .into_iter()
            .collect();

        if unique_labels.is_empty() {
            return Err(SklearsError::InvalidInput(
                "No labeled samples found".to_string(),
            ));
        }

        let n_classes = unique_labels.len();

        // Initialize label probability matrix
        let mut F = Array2::<f64>::zeros((n_samples, n_classes));

        // Set initial labels for labeled samples
        for i in 0..n_samples {
            if labeled_mask[i] {
                if let Some(class_idx) = unique_labels.iter().position(|&x| x == y[i]) {
                    F[[i, class_idx]] = 1.0;
                }
            }
        }

        // Normalize adjacency matrix to get transition matrix
        let P = self.normalize_adjacency(adjacency)?;

        // Iterative label propagation
        for _iter in 0..self.max_iter {
            let F_old = F.clone();

            // Propagate labels: F = α * P * F + (1-α) * Y
            let propagated = P.dot(&F);
            F = &propagated * self.alpha;

            // Add back original labels with weight (1-α)
            for i in 0..n_samples {
                if labeled_mask[i] {
                    if let Some(class_idx) = unique_labels.iter().position(|&x| x == y[i]) {
                        F[[i, class_idx]] += 1.0 - self.alpha;
                    }
                }
            }

            // Normalize probabilities
            for i in 0..n_samples {
                let row_sum: f64 = F.row(i).sum();
                if row_sum > 0.0 {
                    for j in 0..n_classes {
                        F[[i, j]] /= row_sum;
                    }
                }
            }

            // Check convergence
            let change = (&F - &F_old).iter().map(|x| x.abs()).sum::<f64>();
            if change < self.tolerance {
                break;
            }
        }

        // Convert probabilities to labels
        let mut labels = Array1::zeros(n_samples);
        for i in 0..n_samples {
            let mut max_prob = 0.0;
            let mut max_class = 0;

            for j in 0..n_classes {
                if F[[i, j]] > max_prob {
                    max_prob = F[[i, j]];
                    max_class = j;
                }
            }

            labels[i] = unique_labels[max_class];
        }

        Ok(labels)
    }

    /// Normalize adjacency matrix to transition matrix
    #[allow(non_snake_case)] // standard ML notation
    fn normalize_adjacency(&self, adjacency: &Array2<f64>) -> Result<Array2<f64>, SklearsError> {
        let n_samples = adjacency.nrows();
        let mut P = adjacency.clone();

        for i in 0..n_samples {
            let row_sum: f64 = P.row(i).sum();
            if row_sum > 0.0 {
                for j in 0..n_samples {
                    P[[i, j]] /= row_sum;
                }
            }
        }

        Ok(P)
    }
}

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

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

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

        let lgc = LandmarkGraphConstruction::new()
            .n_landmarks(3)
            .selection_strategy("random".to_string())
            .random_state(42);

        let result = lgc.fit(&X.view());
        assert!(result.is_ok());

        let graph_result = result.expect("operation should succeed");
        assert_eq!(graph_result.adjacency_matrix.dim(), (6, 6));
        assert_eq!(graph_result.n_landmarks(), 3);
        assert_eq!(graph_result.landmark_indices.len(), 3);
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_landmark_graph_construction_kmeans() {
        let X = array![
            [1.0, 1.0],
            [1.5, 1.5],
            [2.0, 2.0],
            [8.0, 8.0],
            [8.5, 8.5],
            [9.0, 9.0]
        ];

        let lgc = LandmarkGraphConstruction::new()
            .n_landmarks(2)
            .selection_strategy("kmeans".to_string())
            .random_state(42);

        let result = lgc.fit(&X.view());
        assert!(result.is_ok());

        let graph_result = result.expect("operation should succeed");
        assert_eq!(graph_result.adjacency_matrix.dim(), (6, 6));
        assert_eq!(graph_result.n_landmarks(), 2);
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_landmark_graph_construction_farthest_first() {
        let X = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]];

        let lgc = LandmarkGraphConstruction::new()
            .n_landmarks(2)
            .selection_strategy("farthest_first".to_string())
            .random_state(42);

        let result = lgc.fit(&X.view());
        assert!(result.is_ok());

        let graph_result = result.expect("operation should succeed");
        assert_eq!(graph_result.adjacency_matrix.dim(), (4, 4));
        assert_eq!(graph_result.n_landmarks(), 2);
    }

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

        let lgc = LandmarkGraphConstruction::new()
            .n_landmarks(2)
            .selection_strategy("density_based".to_string())
            .random_state(42);

        let result = lgc.fit(&X.view());
        assert!(result.is_ok());

        let graph_result = result.expect("operation should succeed");
        assert_eq!(graph_result.adjacency_matrix.dim(), (4, 4));
        assert_eq!(graph_result.n_landmarks(), 2);
    }

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

        let methods = vec!["knn_to_landmarks", "rbf_to_landmarks", "interpolation"];

        for method in methods {
            let lgc = LandmarkGraphConstruction::new()
                .n_landmarks(2)
                .construction_method(method.to_string())
                .random_state(42);

            let result = lgc.fit(&X.view());
            assert!(result.is_ok());

            let graph_result = result.expect("operation should succeed");
            assert_eq!(graph_result.adjacency_matrix.dim(), (4, 4));
        }
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_landmark_label_propagation() {
        let X = array![
            [1.0, 2.0],
            [2.0, 3.0],
            [3.0, 4.0],
            [4.0, 5.0],
            [5.0, 6.0],
            [6.0, 7.0]
        ];
        let y = array![0, 1, -1, -1, -1, -1]; // First two are labeled

        let llp = LandmarkLabelPropagation::new();
        let graph_constructor = LandmarkGraphConstruction::new()
            .n_landmarks(3)
            .random_state(42);

        let llp = llp.graph_constructor(graph_constructor);

        let result = llp.fit_predict(&X.view(), &y.view());
        assert!(result.is_ok());

        let labels = result.expect("operation should succeed");
        assert_eq!(labels.len(), 6);

        // Check that labeled samples keep their labels
        assert_eq!(labels[0], 0);
        assert_eq!(labels[1], 1);

        // Check that all labels are valid
        for &label in labels.iter() {
            assert!(label == 0 || label == 1);
        }
    }

    #[test]
    fn test_graph_result_methods() {
        let adjacency = array![
            [0.0, 0.8, 0.0, 0.2],
            [0.8, 0.0, 0.3, 0.0],
            [0.0, 0.3, 0.0, 0.9],
            [0.2, 0.0, 0.9, 0.0]
        ];

        let landmark_indices = vec![0, 2];
        let landmarks = array![[1.0, 2.0], [3.0, 4.0]];

        let result = LandmarkGraphResult {
            adjacency_matrix: adjacency,
            landmark_indices,
            landmarks,
        };

        assert_eq!(result.n_landmarks(), 2);
        assert!(result.sparsity() > 0.0);
        assert!(result.sparsity() < 1.0);

        let coverage = result.landmark_coverage();
        assert!(coverage.contains_key("landmark_ratio"));
        assert!(coverage.contains_key("sparsity"));
        assert_eq!(coverage["n_landmarks"], 2.0);
        assert_eq!(coverage["n_samples"], 4.0);
    }

    #[test]
    fn test_landmark_graph_construction_builder() {
        let lgc = LandmarkGraphConstruction::new()
            .n_landmarks(50)
            .k_neighbors(8)
            .selection_strategy("farthest_first".to_string())
            .construction_method("rbf_to_landmarks".to_string())
            .bandwidth(2.0)
            .max_iter(200)
            .random_state(123);

        assert_eq!(lgc.n_landmarks, 50);
        assert_eq!(lgc.k_neighbors, 8);
        assert_eq!(lgc.selection_strategy, "farthest_first");
        assert_eq!(lgc.construction_method, "rbf_to_landmarks");
        assert_eq!(lgc.bandwidth, 2.0);
        assert_eq!(lgc.max_iter, 200);
        assert_eq!(lgc.random_state, Some(123));
    }

    #[test]
    #[allow(non_snake_case)]
    fn test_error_cases() {
        let lgc =
            LandmarkGraphConstruction::new().selection_strategy("invalid_strategy".to_string());

        let X = array![[1.0, 2.0], [2.0, 3.0]];
        let result = lgc.fit(&X.view());
        assert!(result.is_err());

        let lgc =
            LandmarkGraphConstruction::new().construction_method("invalid_method".to_string());

        let result = lgc.fit(&X.view());
        assert!(result.is_err());

        // Test with empty dataset
        let empty_X = Array2::<f64>::zeros((0, 2));
        let lgc = LandmarkGraphConstruction::new();
        let result = lgc.fit(&empty_X.view());
        assert!(result.is_err());

        // Test label propagation with mismatched dimensions
        let llp = LandmarkLabelPropagation::new();
        let y = array![0]; // Wrong size
        let result = llp.fit_predict(&X.view(), &y.view());
        assert!(result.is_err());

        // Test with no labeled samples
        let y_unlabeled = array![-1, -1];
        let result = llp.fit_predict(&X.view(), &y_unlabeled.view());
        assert!(result.is_err());
    }
}