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
//! Manifold Regularization for semi-supervised learning
//!
//! Manifold regularization combines supervised learning with unsupervised manifold learning
//! by adding a regularization term that penalizes functions that are not smooth with respect
//! to the intrinsic manifold structure.

use crate::graph::{graph_laplacian, knn_graph};
use scirs2_core::ndarray_ext::{Array1, Array2, ArrayView1, ArrayView2};
use sklears_core::{
    error::{Result as SklResult, SklearsError},
    traits::{Estimator, Fit, Predict, Untrained},
    types::Float,
};
use std::collections::HashSet;

/// Manifold Regularization classifier
///
/// Manifold regularization extends supervised learning by incorporating the geometry
/// of the marginal distribution P(X). It optimizes both the empirical risk on labeled
/// data and the smoothness of the function with respect to the data manifold.
///
/// The objective function is:
/// min Σ V(yi, f(xi)) + λA ||f||²_K + λI ∫ ||∇f||² dP(x)
///
/// where:
/// - V is the loss function
/// - λA controls Tikhonov regularization in RKHS
/// - λI controls manifold regularization
/// - The integral term enforces smoothness on the manifold
///
/// # Parameters
///
/// * `lambda_a` - Ambient regularization parameter (RKHS regularization)
/// * `lambda_i` - Intrinsic regularization parameter (manifold regularization)
/// * `kernel` - Kernel function ('rbf', 'linear', 'polynomial')
/// * `gamma` - Parameter for RBF kernel
/// * `degree` - Degree for polynomial kernel
/// * `graph_kernel` - Kernel for graph construction ('knn' or 'rbf')
/// * `n_neighbors` - Number of neighbors for graph construction
/// * `max_iter` - Maximum iterations for optimization
/// * `tol` - Convergence tolerance
///
/// # Examples
///
/// ```
/// use scirs2_core::array;
/// use sklears_semi_supervised::ManifoldRegularization;
/// use sklears_core::traits::{Predict, Fit};
///
///
/// 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);
/// let fitted = mr.fit(&X.view(), &y.view()).unwrap();
/// let predictions = fitted.predict(&X.view()).unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct ManifoldRegularization<S = Untrained> {
    state: S,
    lambda_a: f64,
    lambda_i: f64,
    kernel: String,
    gamma: f64,
    degree: usize,
    graph_kernel: String,
    n_neighbors: usize,
    max_iter: usize,
    tol: f64,
}

impl ManifoldRegularization<Untrained> {
    /// Create a new ManifoldRegularization instance
    pub fn new() -> Self {
        Self {
            state: Untrained,
            lambda_a: 0.01,
            lambda_i: 0.1,
            kernel: "rbf".to_string(),
            gamma: 1.0,
            degree: 3,
            graph_kernel: "knn".to_string(),
            n_neighbors: 7,
            max_iter: 1000,
            tol: 1e-6,
        }
    }

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

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

    /// Set the kernel function
    pub fn kernel(mut self, kernel: String) -> Self {
        self.kernel = kernel;
        self
    }

    /// Set gamma parameter for RBF kernel
    pub fn gamma(mut self, gamma: f64) -> Self {
        self.gamma = gamma;
        self
    }

    /// Set degree for polynomial kernel
    pub fn degree(mut self, degree: usize) -> Self {
        self.degree = degree;
        self
    }

    /// Set graph kernel for manifold construction
    pub fn graph_kernel(mut self, graph_kernel: String) -> Self {
        self.graph_kernel = graph_kernel;
        self
    }

    /// Set number of neighbors for graph construction
    pub fn n_neighbors(mut self, n_neighbors: usize) -> Self {
        self.n_neighbors = n_neighbors;
        self
    }

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

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

    #[allow(non_snake_case)] // standard ML notation
    fn compute_kernel_matrix(&self, X1: &Array2<f64>, X2: &Array2<f64>) -> SklResult<Array2<f64>> {
        let n1 = X1.nrows();
        let n2 = X2.nrows();
        let mut K = Array2::zeros((n1, n2));

        match self.kernel.as_str() {
            "rbf" => {
                for i in 0..n1 {
                    for j in 0..n2 {
                        let diff = &X1.row(i) - &X2.row(j);
                        let dist_sq = diff.mapv(|x| x * x).sum();
                        K[[i, j]] = (-self.gamma * dist_sq).exp();
                    }
                }
            }
            "linear" => {
                K = X1.dot(&X2.t());
            }
            "polynomial" => {
                let linear_kernel = X1.dot(&X2.t());
                for i in 0..n1 {
                    for j in 0..n2 {
                        K[[i, j]] =
                            (self.gamma * linear_kernel[[i, j]] + 1.0).powi(self.degree as i32);
                    }
                }
            }
            _ => {
                return Err(SklearsError::InvalidInput(format!(
                    "Unknown kernel: {}",
                    self.kernel
                )));
            }
        }

        Ok(K)
    }

    #[allow(non_snake_case)] // standard ML notation
    fn build_manifold_graph(&self, X: &Array2<f64>) -> SklResult<Array2<f64>> {
        match self.graph_kernel.as_str() {
            "knn" => knn_graph(X, self.n_neighbors, "connectivity"),
            "rbf" => {
                let n_samples = X.nrows();
                let mut W = Array2::zeros((n_samples, n_samples));
                for i in 0..n_samples {
                    for j in 0..n_samples {
                        if i != j {
                            let diff = &X.row(i) - &X.row(j);
                            let dist_sq = diff.mapv(|x| x * x).sum();
                            W[[i, j]] = (-self.gamma * dist_sq).exp();
                        }
                    }
                }
                Ok(W)
            }
            _ => Err(SklearsError::InvalidInput(format!(
                "Unknown graph kernel: {}",
                self.graph_kernel
            ))),
        }
    }

    #[allow(non_snake_case)] // standard ML notation
    fn solve_manifold_regularized_problem(
        &self,
        K: &Array2<f64>,
        L: &Array2<f64>,
        labeled_indices: &[usize],
        y_labeled: &Array1<i32>,
        classes: &[i32],
    ) -> SklResult<Array2<f64>> {
        let n_samples = K.nrows();
        let n_classes = classes.len();
        let n_labeled = labeled_indices.len();

        // Create labeled data matrix Y_l
        let mut Y_l = Array2::zeros((n_labeled, n_classes));
        for (i, &_idx) in labeled_indices.iter().enumerate() {
            if let Some(class_idx) = classes.iter().position(|&c| c == y_labeled[i]) {
                Y_l[[i, class_idx]] = 1.0;
            }
        }

        // Extract kernel matrix for labeled samples
        let mut K_ll = Array2::zeros((n_labeled, n_labeled));
        for (i, &idx_i) in labeled_indices.iter().enumerate() {
            for (j, &idx_j) in labeled_indices.iter().enumerate() {
                K_ll[[i, j]] = K[[idx_i, idx_j]];
            }
        }

        // Solve the system: (K_ll + λ_A * I + λ_I * K_l * L * K_l) * α = Y_l
        // For simplicity, we use the labeled part of the Laplacian
        let mut L_ll = Array2::zeros((n_labeled, n_labeled));
        for (i, &idx_i) in labeled_indices.iter().enumerate() {
            for (j, &idx_j) in labeled_indices.iter().enumerate() {
                L_ll[[i, j]] = L[[idx_i, idx_j]];
            }
        }

        // Construct the regularized matrix
        let mut A = K_ll.clone();

        // Add ambient regularization
        for i in 0..n_labeled {
            A[[i, i]] += self.lambda_a;
        }

        // Add manifold regularization (simplified)
        A = A + self.lambda_i * &L_ll;

        // Solve the linear system A * α = Y_l using iterative method
        let mut alpha = Array2::zeros((n_labeled, n_classes));

        // Simple iterative solver (Jacobi method)
        for _iter in 0..self.max_iter {
            let mut new_alpha = Array2::zeros((n_labeled, n_classes));

            for i in 0..n_labeled {
                for k in 0..n_classes {
                    let mut sum = Y_l[[i, k]];
                    for j in 0..n_labeled {
                        if i != j {
                            sum -= A[[i, j]] * alpha[[j, k]];
                        }
                    }
                    new_alpha[[i, k]] = sum / A[[i, i]];
                }
            }

            // Check convergence
            let diff = (&new_alpha - &alpha).mapv(|x| x.abs()).sum();
            alpha = new_alpha;

            if diff < self.tol {
                break;
            }
        }

        // Compute function values for all samples
        let mut F = Array2::zeros((n_samples, n_classes));

        for i in 0..n_samples {
            for k in 0..n_classes {
                let mut sum = 0.0;
                for (j, &labeled_idx) in labeled_indices.iter().enumerate() {
                    sum += K[[i, labeled_idx]] * alpha[[j, k]];
                }
                F[[i, k]] = sum;
            }
        }

        Ok(F)
    }
}

impl Default for ManifoldRegularization<Untrained> {
    fn default() -> Self {
        Self::new()
    }
}

impl Estimator for ManifoldRegularization<Untrained> {
    type Config = ();
    type Error = SklearsError;
    type Float = Float;

    fn config(&self) -> &Self::Config {
        &()
    }
}

impl Fit<ArrayView2<'_, Float>, ArrayView1<'_, i32>> for ManifoldRegularization<Untrained> {
    type Fitted = ManifoldRegularization<ManifoldRegularizationTrained>;

    #[allow(non_snake_case)]
    fn fit(self, X: &ArrayView2<'_, Float>, y: &ArrayView1<'_, i32>) -> SklResult<Self::Fitted> {
        let X = X.to_owned();
        let y = y.to_owned();

        // Identify labeled samples
        let mut labeled_indices = Vec::new();
        let mut y_labeled = Vec::new();
        let mut classes = HashSet::new();

        for (i, &label) in y.iter().enumerate() {
            if label != -1 {
                labeled_indices.push(i);
                y_labeled.push(label);
                classes.insert(label);
            }
        }

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

        let classes: Vec<i32> = classes.into_iter().collect();
        let y_labeled = Array1::from(y_labeled);

        // Build kernel matrix
        let K = self.compute_kernel_matrix(&X, &X)?;

        // Build manifold graph and Laplacian
        let W = self.build_manifold_graph(&X)?;
        let L = graph_laplacian(&W, false)?;

        // Solve manifold regularized problem
        let F = self.solve_manifold_regularized_problem(
            &K,
            &L,
            &labeled_indices,
            &y_labeled,
            &classes,
        )?;

        Ok(ManifoldRegularization {
            state: ManifoldRegularizationTrained {
                X_train: X.clone(),
                y_train: y,
                classes: Array1::from(classes),
                alpha: F,
                kernel_matrix: K,
                manifold_laplacian: L,
                labeled_indices,
            },
            lambda_a: self.lambda_a,
            lambda_i: self.lambda_i,
            kernel: self.kernel,
            gamma: self.gamma,
            degree: self.degree,
            graph_kernel: self.graph_kernel,
            n_neighbors: self.n_neighbors,
            max_iter: self.max_iter,
            tol: self.tol,
        })
    }
}

impl Predict<ArrayView2<'_, Float>, Array1<i32>>
    for ManifoldRegularization<ManifoldRegularizationTrained>
{
    #[allow(non_snake_case)]
    fn predict(&self, X: &ArrayView2<'_, Float>) -> SklResult<Array1<i32>> {
        let X = X.to_owned();
        let n_test = X.nrows();
        let mut predictions = Array1::zeros(n_test);

        // Compute kernel matrix between test and training data
        let K_test = self.compute_kernel_matrix(&X, &self.state.X_train)?;

        for i in 0..n_test {
            // Compute function values for test sample
            let mut max_value = f64::NEG_INFINITY;
            let mut best_class_idx = 0;

            for k in 0..self.state.classes.len() {
                let mut value = 0.0;
                for j in 0..self.state.X_train.nrows() {
                    value += K_test[[i, j]] * self.state.alpha[[j, k]];
                }

                if value > max_value {
                    max_value = value;
                    best_class_idx = k;
                }
            }

            predictions[i] = self.state.classes[best_class_idx];
        }

        Ok(predictions)
    }
}

impl ManifoldRegularization<ManifoldRegularizationTrained> {
    #[allow(non_snake_case)] // standard ML notation
    fn compute_kernel_matrix(&self, X1: &Array2<f64>, X2: &Array2<f64>) -> SklResult<Array2<f64>> {
        let n1 = X1.nrows();
        let n2 = X2.nrows();
        let mut K = Array2::zeros((n1, n2));

        match self.kernel.as_str() {
            "rbf" => {
                for i in 0..n1 {
                    for j in 0..n2 {
                        let diff = &X1.row(i) - &X2.row(j);
                        let dist_sq = diff.mapv(|x| x * x).sum();
                        K[[i, j]] = (-self.gamma * dist_sq).exp();
                    }
                }
            }
            "linear" => {
                K = X1.dot(&X2.t());
            }
            "polynomial" => {
                let linear_kernel = X1.dot(&X2.t());
                for i in 0..n1 {
                    for j in 0..n2 {
                        K[[i, j]] =
                            (self.gamma * linear_kernel[[i, j]] + 1.0).powi(self.degree as i32);
                    }
                }
            }
            _ => {
                return Err(SklearsError::InvalidInput(format!(
                    "Unknown kernel: {}",
                    self.kernel
                )));
            }
        }

        Ok(K)
    }
}

/// Trained state for ManifoldRegularization
#[derive(Debug, Clone)]
#[allow(non_snake_case)] // standard ML notation
pub struct ManifoldRegularizationTrained {
    /// X_train
    pub X_train: Array2<f64>,
    /// y_train
    pub y_train: Array1<i32>,
    /// classes
    pub classes: Array1<i32>,
    /// alpha
    pub alpha: Array2<f64>,
    /// kernel_matrix
    pub kernel_matrix: Array2<f64>,
    /// manifold_laplacian
    pub manifold_laplacian: Array2<f64>,
    /// labeled_indices
    pub labeled_indices: Vec<usize>,
}