sklears-semi-supervised 0.2.0

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
//! Semi-supervised learning algorithms
//!
//! This module provides semi-supervised learning algorithms that can utilize
//! both labeled and unlabeled data for training.

// #![warn(missing_docs)]

mod active_learning;
mod adversarial_graph_learning;
mod approximate_graph_methods;
mod batch_active_learning;
mod bayesian_methods;
mod co_training;
mod composable_graph;
mod contrastive_learning;
mod convergence_tests;
mod cross_modal_contrastive;
mod deep_learning;
mod democratic_co_learning;
mod dynamic_graph_learning;
mod entropy_methods;
mod few_shot;
mod graph;
mod graph_learning;
mod harmonic_functions;
mod hierarchical_graph;
mod information_theory;
mod label_propagation;
mod label_spreading;
mod landmark_methods;
mod local_global_consistency;
mod manifold_regularization;
mod mixture_discriminant_analysis;
mod multi_armed_bandits;
mod multi_view_graph;
mod optimal_transport;
pub mod parallel_graph;
mod robust_graph_methods;
mod self_training;
mod self_training_classifier;
mod semi_supervised_gmm;
mod semi_supervised_naive_bayes;
pub mod simd_distances;
mod streaming_graph_learning;
mod tri_training;

pub use active_learning::*;
pub use adversarial_graph_learning::*;
pub use approximate_graph_methods::*;
pub use batch_active_learning::*;
pub use bayesian_methods::*;
pub use co_training::*;
pub use composable_graph::*;
pub use contrastive_learning::*;
pub use convergence_tests::*;
pub use cross_modal_contrastive::*;
pub use deep_learning::*;
pub use democratic_co_learning::*;
pub use dynamic_graph_learning::*;
pub use entropy_methods::*;
pub use few_shot::*;
pub use graph::*;
pub use graph_learning::*;
pub use harmonic_functions::*;
pub use hierarchical_graph::*;
pub use information_theory::*;
pub use label_propagation::*;
pub use label_spreading::*;
pub use landmark_methods::*;
pub use local_global_consistency::*;
pub use manifold_regularization::*;
pub use mixture_discriminant_analysis::*;
pub use multi_armed_bandits::*;
pub use multi_view_graph::*;
pub use optimal_transport::*;
pub use robust_graph_methods::*;
pub use self_training::*;
pub use self_training_classifier::*;
pub use semi_supervised_gmm::*;
pub use semi_supervised_naive_bayes::*;
pub use streaming_graph_learning::*;
pub use tri_training::*;

#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::array;
    use scirs2_core::ndarray_ext::Array2;
    use sklears_core::traits::{Fit, Predict, PredictProba};

    #[test]
    fn test_label_propagation() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
        let y = array![0, 1, -1, -1]; // -1 indicates unlabeled

        let lp = LabelPropagation::new()
            .kernel("rbf".to_string())
            .gamma(20.0);
        let fitted = lp
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        let predictions = fitted.predict(&X.view()).expect("operation should succeed");
        assert_eq!(predictions.len(), 4);

        let probas = fitted
            .predict_proba(&X.view())
            .expect("operation should succeed");
        assert_eq!(probas.dim(), (4, 2));
    }

    #[test]
    fn test_label_spreading() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
        let y = array![0, 1, -1, -1]; // -1 indicates unlabeled

        let ls = LabelSpreading::new()
            .kernel("rbf".to_string())
            .gamma(20.0)
            .alpha(0.2);
        let fitted = ls
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        let predictions = fitted.predict(&X.view()).expect("operation should succeed");
        assert_eq!(predictions.len(), 4);

        let probas = fitted
            .predict_proba(&X.view())
            .expect("operation should succeed");
        assert_eq!(probas.dim(), (4, 2));
    }

    #[test]
    fn test_self_training() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
        let y = array![0, 1, -1, -1]; // -1 indicates unlabeled

        let stc = SelfTrainingClassifier::new().threshold(0.5).max_iter(5);
        let fitted = stc
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        let predictions = fitted.predict(&X.view()).expect("operation should succeed");
        assert_eq!(predictions.len(), 4);
    }

    // Note: Commented out test for private method
    // #[test]
    // fn test_affinity_matrix_rbf() {
    //     let lp = LabelPropagation::new().kernel("rbf".to_string()).gamma(1.0);
    //     let X = array![[1.0, 2.0], [3.0, 4.0]];
    //
    //     let W = lp.build_affinity_matrix(&X).expect("operation should succeed");
    //     assert_eq!(W.dim(), (2, 2));
    //     assert_eq!(W[[0, 0]], 0.0); // Diagonal should be 0
    //     assert_eq!(W[[1, 1]], 0.0);
    //     assert!(W[[0, 1]] > 0.0); // Off-diagonal should be positive
    //     assert!(W[[1, 0]] > 0.0);
    // }

    // Note: Commented out test for private method
    // #[test]
    // fn test_affinity_matrix_knn() {
    //     let lp = LabelPropagation::new()
    //         .kernel("knn".to_string())
    //         .n_neighbors(1);
    //     let X = array![[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
    //
    //     let W = lp.build_affinity_matrix(&X).expect("operation should succeed");
    //     assert_eq!(W.dim(), (3, 3));
    //
    //     // Check that each row has exactly n_neighbors non-zero entries
    //     for i in 0..3 {
    //         let non_zero_count = W.row(i).iter().filter(|&&x| x > 0.0).count();
    //         assert!(non_zero_count <= 2); // At most 2 (should be 1 for n_neighbors=1, but symmetric)
    //     }
    // }

    #[test]
    fn test_enhanced_self_training() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
        let y = array![0, 1, -1, -1]; // -1 indicates unlabeled

        let est = EnhancedSelfTraining::new()
            .threshold(0.6)
            .confidence_method("entropy".to_string())
            .max_iter(5);
        let fitted = est
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        let predictions = fitted.predict(&X.view()).expect("operation should succeed");
        assert_eq!(predictions.len(), 4);
    }

    #[test]
    fn test_co_training() {
        let X = array![
            [1.0, 2.0, 3.0, 4.0],
            [2.0, 3.0, 4.0, 5.0],
            [3.0, 4.0, 5.0, 6.0],
            [4.0, 5.0, 6.0, 7.0]
        ];
        let y = array![0, 1, -1, -1]; // -1 indicates unlabeled

        let ct = CoTraining::new()
            .view1_features(vec![0, 1])
            .view2_features(vec![2, 3])
            .p(1)
            .n(1)
            .max_iter(5);
        let fitted = ct
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        let predictions = fitted.predict(&X.view()).expect("operation should succeed");
        assert_eq!(predictions.len(), 4);
    }

    #[test]
    fn test_tri_training() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
        let y = array![0, 1, -1, -1]; // -1 indicates unlabeled

        let tt = TriTraining::new().max_iter(5).theta(0.2);
        let fitted = tt
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        let predictions = fitted.predict(&X.view()).expect("operation should succeed");
        assert_eq!(predictions.len(), 4);
    }

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

        let W = knn_graph(&X, 1, "connectivity").expect("operation should succeed");
        assert_eq!(W.dim(), (3, 3));

        // Check that each row has at most n_neighbors non-zero entries
        for i in 0..3 {
            let non_zero_count = W.row(i).iter().filter(|&&x| x > 0.0).count();
            assert!(non_zero_count <= 1);
        }
    }

    #[test]
    fn test_epsilon_graph() {
        let X = array![[1.0, 2.0], [1.1, 2.1], [5.0, 6.0]];

        let W = epsilon_graph(&X, 1.0, "connectivity").expect("operation should succeed");
        assert_eq!(W.dim(), (3, 3));

        // Points 0 and 1 should be connected (distance < 1.0)
        assert!(W[[0, 1]] > 0.0 || W[[1, 0]] > 0.0);
    }

    #[test]
    fn test_graph_laplacian() {
        let W = array![[0.0, 1.0, 0.0], [1.0, 0.0, 1.0], [0.0, 1.0, 0.0]];

        let L = graph_laplacian(&W, false).expect("operation should succeed");
        assert_eq!(L.dim(), (3, 3));

        // Check Laplacian properties
        assert_eq!(L[[0, 0]], 1.0); // degree of node 0
        assert_eq!(L[[1, 1]], 2.0); // degree of node 1
        assert_eq!(L[[0, 1]], -1.0); // -adjacency
    }

    #[test]
    fn test_democratic_co_learning() {
        let X = array![
            [1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
            [2.0, 3.0, 4.0, 5.0, 6.0, 7.0],
            [3.0, 4.0, 5.0, 6.0, 7.0, 8.0],
            [4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
        ];
        let y = array![0, 1, -1, -1]; // -1 indicates unlabeled

        let dcl = DemocraticCoLearning::new()
            .views(vec![vec![0, 1], vec![2, 3], vec![4, 5]])
            .k_add(1)
            .min_agreement(2)
            .max_iter(5);
        let fitted = dcl
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        let predictions = fitted.predict(&X.view()).expect("operation should succeed");
        assert_eq!(predictions.len(), 4);
    }

    #[test]
    fn test_harmonic_functions() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
        let y = array![0, 1, -1, -1]; // -1 indicates unlabeled

        let hf = HarmonicFunctions::new()
            .kernel("rbf".to_string())
            .gamma(20.0)
            .max_iter(100);
        let fitted = hf
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        let predictions = fitted.predict(&X.view()).expect("operation should succeed");
        assert_eq!(predictions.len(), 4);

        let probas = fitted
            .predict_proba(&X.view())
            .expect("operation should succeed");
        assert_eq!(probas.dim(), (4, 2));
    }

    #[test]
    fn test_local_global_consistency() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
        let y = array![0, 1, -1, -1]; // -1 indicates unlabeled

        let lgc = LocalGlobalConsistency::new()
            .kernel("rbf".to_string())
            .gamma(20.0)
            .alpha(0.99)
            .max_iter(100);
        let fitted = lgc
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        let predictions = fitted.predict(&X.view()).expect("operation should succeed");
        assert_eq!(predictions.len(), 4);

        let probas = fitted
            .predict_proba(&X.view())
            .expect("operation should succeed");
        assert_eq!(probas.dim(), (4, 2));
    }

    #[test]
    fn test_manifold_regularization() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
        let y = array![0, 1, -1, -1]; // -1 indicates unlabeled

        let mr = ManifoldRegularization::new()
            .lambda_a(0.01)
            .lambda_i(0.1)
            .kernel("rbf".to_string())
            .gamma(1.0)
            .max_iter(100);
        let fitted = mr
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        let predictions = fitted.predict(&X.view()).expect("operation should succeed");
        assert_eq!(predictions.len(), 4);
    }

    #[test]
    fn test_semi_supervised_gmm() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
        let y = array![0, 1, -1, -1]; // -1 indicates unlabeled

        let gmm = SemiSupervisedGMM::new()
            .n_components(2)
            .max_iter(50)
            .labeled_weight(10.0);
        let fitted = gmm
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        let predictions = fitted.predict(&X.view()).expect("operation should succeed");
        assert_eq!(predictions.len(), 4);

        let probas = fitted
            .predict_proba(&X.view())
            .expect("operation should succeed");
        assert_eq!(probas.dim(), (4, 2));
    }

    #[test]
    fn test_multi_view_co_training() {
        let X = array![
            [1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
            [2.0, 3.0, 4.0, 5.0, 6.0, 7.0],
            [3.0, 4.0, 5.0, 6.0, 7.0, 8.0],
            [4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
            [5.0, 6.0, 7.0, 8.0, 9.0, 10.0],
            [6.0, 7.0, 8.0, 9.0, 10.0, 11.0]
        ];
        let y = array![0, 1, -1, -1, -1, -1]; // -1 indicates unlabeled

        let mvct = MultiViewCoTraining::new()
            .views(vec![vec![0, 1], vec![2, 3], vec![4, 5]])
            .k_add(1)
            .confidence_threshold(0.5)
            .max_iter(5);
        let fitted = mvct
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        let predictions = fitted.predict(&X.view()).expect("operation should succeed");
        assert_eq!(predictions.len(), 6);

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

    #[test]
    fn test_semi_supervised_naive_bayes() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]];
        let y = array![0, 1, -1, -1]; // -1 indicates unlabeled

        let nb = SemiSupervisedNaiveBayes::new()
            .alpha(1.0)
            .max_iter(50)
            .class_weight(1.0);
        let fitted = nb
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");

        let predictions = fitted.predict(&X.view()).expect("operation should succeed");
        assert_eq!(predictions.len(), 4);

        let probas = fitted
            .predict_proba(&X.view())
            .expect("operation should succeed");
        assert_eq!(probas.dim(), (4, 2));

        // Check that probabilities sum to 1
        for i in 0..4 {
            let sum: f64 = probas.row(i).sum();
            assert!((sum - 1.0).abs() < 1e-10);
        }
    }

    #[test]
    fn test_random_walk_laplacian() {
        let W = array![[0.0, 1.0, 0.0], [1.0, 0.0, 1.0], [0.0, 1.0, 0.0]];

        let L_rw = random_walk_laplacian(&W).expect("operation should succeed");
        assert_eq!(L_rw.dim(), (3, 3));

        // Check that L_rw has 1s on the diagonal
        assert!((L_rw[[0, 0]] - 1.0).abs() < 1e-10);
        assert!((L_rw[[1, 1]] - 1.0).abs() < 1e-10);
        assert!((L_rw[[2, 2]] - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_diffusion_matrix() {
        let W = array![[0.0, 1.0, 0.0], [1.0, 0.0, 1.0], [0.0, 1.0, 0.0]];

        let P = diffusion_matrix(&W, 2).expect("operation should succeed");
        assert_eq!(P.dim(), (3, 3));

        // Check that probabilities are non-negative
        for i in 0..3 {
            for j in 0..3 {
                assert!(P[[i, j]] >= 0.0);
            }
        }
    }

    #[test]
    fn test_adaptive_knn_graph() {
        let X = array![[1.0, 2.0], [1.1, 2.1], [5.0, 6.0]];

        let W = adaptive_knn_graph(&X, "connectivity").expect("operation should succeed");
        assert_eq!(W.dim(), (3, 3));

        // Check symmetry
        assert_eq!(W[[0, 1]], W[[1, 0]]);
        assert_eq!(W[[0, 2]], W[[2, 0]]);
        assert_eq!(W[[1, 2]], W[[2, 1]]);
    }

    #[test]
    fn test_sparsify_graph() {
        let W = array![
            [0.0, 0.8, 0.2, 0.1],
            [0.8, 0.0, 0.9, 0.3],
            [0.2, 0.9, 0.0, 0.7],
            [0.1, 0.3, 0.7, 0.0]
        ];

        let W_sparse = sparsify_graph(&W, 0.5).expect("operation should succeed");
        assert_eq!(W_sparse.dim(), (4, 4));

        // Count non-zero edges in original and sparse graphs
        let original_edges = W.iter().filter(|&&x| x > 0.0).count();
        let sparse_edges = W_sparse.iter().filter(|&&x| x > 0.0).count();

        // Sparse graph should have fewer edges
        assert!(sparse_edges <= original_edges);
    }

    #[test]
    fn test_spectral_clustering() {
        let W = array![
            [0.0, 1.0, 0.1, 0.0, 0.0],
            [1.0, 0.0, 0.2, 0.0, 0.0],
            [0.1, 0.2, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0, 1.0],
            [0.0, 0.0, 0.0, 1.0, 0.0]
        ];

        let labels = spectral_clustering(&W, 2, true, Some(42)).expect("operation should succeed");
        assert_eq!(labels.len(), 5);

        // Check that labels are valid
        for &label in labels.iter() {
            assert!((0..2).contains(&label));
        }
    }

    #[test]
    fn test_spectral_embedding() {
        let W = array![[0.0, 1.0, 0.1], [1.0, 0.0, 0.2], [0.1, 0.2, 0.0]];

        let embedding = spectral_embedding(&W, 2, true).expect("operation should succeed");
        assert_eq!(embedding.dim(), (3, 2));
    }

    // Robustness tests with label noise
    #[test]
    fn test_label_propagation_robustness() {
        use scirs2_core::random::Random;

        let mut rng = Random::seed(42);

        // Generate synthetic dataset - create it manually to avoid distribution conflicts
        let mut X = Array2::<f64>::zeros((50, 5));
        for i in 0..50 {
            for j in 0..5 {
                X[(i, j)] = rng.random_range(-1.0..1.0);
            }
        }
        let y_true = array![
            0, 1, 0, 1, 0, 1, 0, 1, 0, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1
        ];

        // Test without noise
        let lp = LabelPropagation::new()
            .kernel("rbf".to_string())
            .gamma(20.0);
        let fitted_clean = lp
            .fit(&X.view(), &y_true.view())
            .expect("operation should succeed");
        let pred_clean = fitted_clean
            .predict(&X.view())
            .expect("operation should succeed");

        // Test with label noise (flip some labels)
        let mut y_noisy = y_true.clone();
        y_noisy[0] = 1; // Flip first label
        y_noisy[2] = 1; // Flip third label

        let lp_noisy = LabelPropagation::new()
            .kernel("rbf".to_string())
            .gamma(20.0);
        let fitted_noisy = lp_noisy
            .fit(&X.view(), &y_noisy.view())
            .expect("operation should succeed");
        let pred_noisy = fitted_noisy
            .predict(&X.view())
            .expect("operation should succeed");

        // Calculate robustness (predictions should not change dramatically)
        let different = pred_clean
            .iter()
            .zip(pred_noisy.iter())
            .filter(|(a, b)| a != b)
            .count();
        let robustness = 1.0 - (different as f64 / pred_clean.len() as f64);

        assert!(
            robustness > 0.6,
            "Label propagation should be somewhat robust to label noise"
        );
    }

    #[test]
    fn test_self_training_robustness() {
        use scirs2_core::random::Random;

        let mut rng = Random::seed(42);

        // Generate synthetic dataset - create it manually to avoid distribution conflicts
        let mut X = Array2::<f64>::zeros((30, 4));
        for i in 0..30 {
            for j in 0..4 {
                X[(i, j)] = rng.random_range(-1.0..1.0);
            }
        }
        let y_clean = array![
            0, 1, 0, 1, 0, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1
        ];

        // Test without noise
        let stc_clean = SelfTrainingClassifier::new().threshold(0.8).max_iter(10);
        let fitted_clean = stc_clean
            .fit(&X.view(), &y_clean.view())
            .expect("operation should succeed");
        let pred_clean = fitted_clean
            .predict(&X.view())
            .expect("operation should succeed");

        // Test with noise
        let mut y_noisy = y_clean.clone();
        y_noisy[1] = 0; // Flip a label

        let stc_noisy = SelfTrainingClassifier::new().threshold(0.8).max_iter(10);
        let fitted_noisy = stc_noisy
            .fit(&X.view(), &y_noisy.view())
            .expect("operation should succeed");
        let pred_noisy = fitted_noisy
            .predict(&X.view())
            .expect("operation should succeed");

        // Check that algorithm still produces valid predictions
        assert!(
            pred_clean.iter().all(|&p| (0..=1).contains(&p)),
            "Clean predictions should be valid"
        );
        assert!(
            pred_noisy.iter().all(|&p| (0..=1).contains(&p)),
            "Noisy predictions should be valid"
        );
    }

    #[test]
    fn test_co_training_robustness() {
        use scirs2_core::random::Random;

        let mut rng = Random::seed(42);

        // Generate synthetic dataset - create it manually to avoid distribution conflicts
        let mut X = Array2::<f64>::zeros((20, 6));
        for i in 0..20 {
            for j in 0..6 {
                X[(i, j)] = rng.random_range(-1.0..1.0);
            }
        }
        let y_clean =
            array![0, 1, 0, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1];

        // Test clean performance
        let ct_clean = CoTraining::new()
            .view1_features(vec![0, 1, 2])
            .view2_features(vec![3, 4, 5])
            .p(1)
            .n(1)
            .max_iter(5);
        let fitted_clean = ct_clean
            .fit(&X.view(), &y_clean.view())
            .expect("operation should succeed");
        let pred_clean = fitted_clean
            .predict(&X.view())
            .expect("operation should succeed");

        // Test with noise
        let mut y_noisy = y_clean.clone();
        y_noisy[0] = 1; // Flip a label

        let ct_noisy = CoTraining::new()
            .view1_features(vec![0, 1, 2])
            .view2_features(vec![3, 4, 5])
            .p(1)
            .n(1)
            .max_iter(5);
        let fitted_noisy = ct_noisy
            .fit(&X.view(), &y_noisy.view())
            .expect("operation should succeed");
        let pred_noisy = fitted_noisy
            .predict(&X.view())
            .expect("operation should succeed");

        // Check that predictions are valid
        assert!(
            pred_clean.iter().all(|&p| (0..=1).contains(&p)),
            "Clean predictions should be valid"
        );
        assert!(
            pred_noisy.iter().all(|&p| (0..=1).contains(&p)),
            "Noisy predictions should be valid"
        );
    }

    // Label efficiency tests
    #[test]
    fn test_label_efficiency_comparison() {
        use scirs2_core::random::Random;
        let mut rng = Random::seed(42);

        // Generate synthetic dataset - create it manually to avoid distribution conflicts
        let mut X = Array2::<f64>::zeros((40, 5));
        for i in 0..40 {
            for j in 0..5 {
                X[(i, j)] = rng.random_range(-1.0..1.0);
            }
        }

        // Test with different labeling ratios
        let small_labeled = array![
            0, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
        ];

        let large_labeled = array![
            0, 1, 0, 1, 0, 1, 0, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
        ];

        // Test label propagation with different label amounts
        let lp_small = LabelPropagation::new()
            .kernel("rbf".to_string())
            .gamma(20.0);
        let fitted_small = lp_small
            .fit(&X.view(), &small_labeled.view())
            .expect("operation should succeed");
        let pred_small = fitted_small
            .predict(&X.view())
            .expect("operation should succeed");

        let lp_large = LabelPropagation::new()
            .kernel("rbf".to_string())
            .gamma(20.0);
        let fitted_large = lp_large
            .fit(&X.view(), &large_labeled.view())
            .expect("operation should succeed");
        let pred_large = fitted_large
            .predict(&X.view())
            .expect("operation should succeed");

        // Both should produce valid predictions
        assert!(
            pred_small.iter().all(|&p| (0..=1).contains(&p)),
            "Small labeled predictions should be valid"
        );
        assert!(
            pred_large.iter().all(|&p| (0..=1).contains(&p)),
            "Large labeled predictions should be valid"
        );
    }

    // Convergence tests
    #[test]
    fn test_algorithm_convergence() {
        let X = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0], [5.0, 6.0]];
        let y = array![0, 1, -1, -1, -1];

        // Test that harmonic functions converge
        let hf = HarmonicFunctions::new()
            .kernel("rbf".to_string())
            .gamma(20.0)
            .max_iter(100);
        let fitted = hf
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");
        let predictions = fitted.predict(&X.view()).expect("operation should succeed");

        // Check that predictions are stable and valid
        assert!(
            predictions.iter().all(|&p| (0..=1).contains(&p)),
            "Predictions should be stable and valid"
        );

        // Test that local-global consistency converges
        let lgc = LocalGlobalConsistency::new()
            .kernel("rbf".to_string())
            .gamma(20.0)
            .alpha(0.99)
            .max_iter(100);
        let fitted_lgc = lgc
            .fit(&X.view(), &y.view())
            .expect("operation should succeed");
        let predictions_lgc = fitted_lgc
            .predict(&X.view())
            .expect("operation should succeed");

        assert!(
            predictions_lgc.iter().all(|&p| (0..=1).contains(&p)),
            "LGC predictions should be stable and valid"
        );
    }
}