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,
};
#[derive(Debug, Clone)]
pub struct DecisionBoundarySemiSupervised<S = Untrained> {
state: S,
boundary_weight: f64,
entropy_weight: f64,
max_iter: usize,
learning_rate: f64,
tol: f64,
kernel: String,
gamma: f64,
}
impl DecisionBoundarySemiSupervised<Untrained> {
pub fn new() -> Self {
Self {
state: Untrained,
boundary_weight: 1.0,
entropy_weight: 0.1,
max_iter: 100,
learning_rate: 0.01,
tol: 1e-6,
kernel: "rbf".to_string(),
gamma: 1.0,
}
}
pub fn boundary_weight(mut self, boundary_weight: f64) -> Self {
self.boundary_weight = boundary_weight;
self
}
pub fn entropy_weight(mut self, entropy_weight: f64) -> Self {
self.entropy_weight = entropy_weight;
self
}
pub fn max_iter(mut self, max_iter: usize) -> Self {
self.max_iter = max_iter;
self
}
pub fn learning_rate(mut self, learning_rate: f64) -> Self {
self.learning_rate = learning_rate;
self
}
pub fn tol(mut self, tol: f64) -> Self {
self.tol = tol;
self
}
pub fn kernel(mut self, kernel: String) -> Self {
self.kernel = kernel;
self
}
pub fn gamma(mut self, gamma: f64) -> Self {
self.gamma = gamma;
self
}
}
impl Default for DecisionBoundarySemiSupervised<Untrained> {
fn default() -> Self {
Self::new()
}
}
impl Estimator for DecisionBoundarySemiSupervised<Untrained> {
type Config = ();
type Error = SklearsError;
type Float = Float;
fn config(&self) -> &Self::Config {
&()
}
}
impl Fit<ArrayView2<'_, Float>, ArrayView1<'_, i32>> for DecisionBoundarySemiSupervised<Untrained> {
type Fitted = DecisionBoundarySemiSupervised<DecisionBoundarySemiSupervisedTrained>;
#[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();
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(DecisionBoundarySemiSupervised {
state: DecisionBoundarySemiSupervisedTrained {
weights: Array2::zeros((X.ncols(), classes.len())),
biases: Array1::zeros(classes.len()),
classes: Array1::from(classes),
support_vectors: Array1::zeros(0),
},
boundary_weight: self.boundary_weight,
entropy_weight: self.entropy_weight,
max_iter: self.max_iter,
learning_rate: self.learning_rate,
tol: self.tol,
kernel: self.kernel,
gamma: self.gamma,
})
}
}
impl Predict<ArrayView2<'_, Float>, Array1<i32>>
for DecisionBoundarySemiSupervised<DecisionBoundarySemiSupervisedTrained>
{
#[allow(non_snake_case)] 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 DecisionBoundarySemiSupervised<DecisionBoundarySemiSupervisedTrained>
{
#[allow(non_snake_case)] 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)
}
}
#[derive(Debug, Clone)]
pub struct DecisionBoundarySemiSupervisedTrained {
pub weights: Array2<f64>,
pub biases: Array1<f64>,
pub classes: Array1<i32>,
pub support_vectors: Array1<usize>,
}