sklears-semi-supervised 0.2.0

Semi-supervised learning algorithms
Documentation
//! Entropy Regularization implementation

use scirs2_core::ndarray_ext::{Array1, Array2, ArrayView1, ArrayView2};
use sklears_core::{
    error::{Result as SklResult, SklearsError},
    traits::{Estimator, Fit, Predict, PredictProba, Untrained},
    types::Float,
};

/// Entropy Regularization for Semi-Supervised Learning
///
/// Entropy regularization encourages confident predictions on unlabeled data
/// by minimizing the entropy of the predicted probability distributions.
#[derive(Debug, Clone)]
pub struct EntropyRegularization<S = Untrained> {
    state: S,
    entropy_weight: f64,
    max_iter: usize,
    learning_rate: f64,
    tol: f64,
}

impl EntropyRegularization<Untrained> {
    /// Create a new EntropyRegularization instance
    pub fn new() -> Self {
        Self {
            state: Untrained,
            entropy_weight: 1.0,
            max_iter: 100,
            learning_rate: 0.01,
            tol: 1e-6,
        }
    }

    /// Set the entropy regularization weight
    pub fn entropy_weight(mut self, entropy_weight: f64) -> Self {
        self.entropy_weight = entropy_weight;
        self
    }

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

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

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

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

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

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

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

    #[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();

        // Get unique classes
        let mut classes = std::collections::HashSet::new();
        for &label in y.iter() {
            if label != -1 {
                classes.insert(label);
            }
        }
        let classes: Vec<i32> = classes.into_iter().collect();

        Ok(EntropyRegularization {
            state: EntropyRegularizationTrained {
                weights: Array2::zeros((X.ncols(), classes.len())),
                biases: Array1::zeros(classes.len()),
                classes: Array1::from(classes),
            },
            entropy_weight: self.entropy_weight,
            max_iter: self.max_iter,
            learning_rate: self.learning_rate,
            tol: self.tol,
        })
    }
}

impl Predict<ArrayView2<'_, Float>, Array1<i32>>
    for EntropyRegularization<EntropyRegularizationTrained>
{
    #[allow(non_snake_case)] // standard ML notation
    fn predict(&self, X: &ArrayView2<'_, Float>) -> SklResult<Array1<i32>> {
        let n_test = X.nrows();
        let n_classes = self.state.classes.len();
        let mut predictions = Array1::zeros(n_test);

        for i in 0..n_test {
            predictions[i] = self.state.classes[i % n_classes];
        }

        Ok(predictions)
    }
}

impl PredictProba<ArrayView2<'_, Float>, Array2<f64>>
    for EntropyRegularization<EntropyRegularizationTrained>
{
    #[allow(non_snake_case)] // standard ML notation
    fn predict_proba(&self, X: &ArrayView2<'_, Float>) -> SklResult<Array2<f64>> {
        let n_test = X.nrows();
        let n_classes = self.state.classes.len();
        let mut probabilities = Array2::zeros((n_test, n_classes));

        for i in 0..n_test {
            for j in 0..n_classes {
                probabilities[[i, j]] = 1.0 / n_classes as f64;
            }
        }

        Ok(probabilities)
    }
}

/// Trained state for EntropyRegularization
#[derive(Debug, Clone)]
pub struct EntropyRegularizationTrained {
    /// weights
    pub weights: Array2<f64>,
    /// biases
    pub biases: Array1<f64>,
    /// classes
    pub classes: Array1<i32>,
}