pub struct SGDOneClassSVM<F> {
pub nu: F,
pub fit_intercept: bool,
pub max_iter: usize,
pub tol: F,
pub shuffle: bool,
pub learning_rate: LearningRateSchedule<F>,
pub eta0: F,
pub power_t: F,
pub random_state: Option<u64>,
pub n_iter_no_change: usize,
}Expand description
Linear One-Class SVM trained by Stochastic Gradient Descent.
Mirrors scikit-learn’s
SGDOneClassSVM
(_stochastic_gradient.py:2084-2668). It solves the linear One-Class SVM
primal via the same SGD kernel as SGDClassifier, with the targets fixed
to y = ones(n), the Hinge loss (threshold = 1), the L2 penalty, and
alpha = nu / 2 (_stochastic_gradient.py:2479,2588). The SGD intercept
b relates to the One-Class offset rho by offset_ = 1 - b
(_stochastic_gradient.py:2325,2377), and the per-sample intercept update
gains an extra - 2*eta*alpha term (_sgd_fast.pyx.tp:641-642).
The decision function is decision_function(X) = X · coef_ - offset_
(_stochastic_gradient.py:2622); predict returns +1 (inlier) where the
decision is >= 0 and -1 (outlier) otherwise (:2655-2657).
§Type Parameters
F: The floating-point type (f32orf64).
§Examples
use ferrolearn_linear::sgd::SGDOneClassSVM;
use ferrolearn_core::{Fit, Predict};
use ndarray::{array, Array2};
let x = Array2::from_shape_vec((4, 2), vec![
-1.0, -1.0, -2.0, -1.0, 1.0, 1.0, 2.0, 1.0,
]).unwrap();
let model = SGDOneClassSVM::<f64>::new()
.with_learning_rate(ferrolearn_linear::sgd::LearningRateSchedule::Constant)
.with_eta0(0.05)
.with_max_iter(10)
.with_shuffle(false);
let fitted = model.fit(&x, &()).unwrap();
let preds = fitted.predict(&x).unwrap();
assert_eq!(preds.len(), 4);Fields§
§nu: FThe nu parameter — an upper bound on the fraction of training errors
and a lower bound on the fraction of support vectors. Must be in
(0, 1]. Defaults to 0.5 (_stochastic_gradient.py:2098-2102,2247).
fit_intercept: boolWhether to fit (update) the intercept. Defaults to true
(_stochastic_gradient.py:2104-2105,2248).
max_iter: usizeMaximum number of passes over the training data. Defaults to 1000
(_stochastic_gradient.py:2107,2249).
tol: FConvergence tolerance. Defaults to 1e-3
(_stochastic_gradient.py:2113,2250). Set to F::neg_infinity() to
disable the early-stop rule (the analog of sklearn’s tol=None,
_stochastic_gradient.py:2310).
shuffle: boolWhether to shuffle the training data after each epoch. Defaults to
true (_stochastic_gradient.py:2118,2251).
learning_rate: LearningRateSchedule<F>The learning rate schedule. Defaults to Optimal
(_stochastic_gradient.py:2132,2254).
eta0: FInitial learning rate for the constant/invscaling/adaptive
schedules. Defaults to 0.0 (_stochastic_gradient.py:2145,2255).
power_t: FPower parameter for the inverse-scaling schedule. Defaults to 0.5
(_stochastic_gradient.py:2151,2256).
random_state: Option<u64>Optional random seed for sample shuffling
(_stochastic_gradient.py:2125).
n_iter_no_change: usizeNumber of consecutive non-improving epochs before convergence (or, under
the adaptive schedule, before eta is divided by 5). Defaults to 5
(_stochastic_gradient.py:2278).
Implementations§
Source§impl<F: Float> SGDOneClassSVM<F>
impl<F: Float> SGDOneClassSVM<F>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new SGDOneClassSVM with default settings.
Defaults match scikit-learn’s SGDOneClassSVM.__init__
(_stochastic_gradient.py:2245-2281): nu = 0.5,
fit_intercept = true, max_iter = 1000, tol = 1e-3,
shuffle = true, learning_rate = Optimal, eta0 = 0.0,
power_t = 0.5, n_iter_no_change = 5.
Sourcepub fn with_nu(self, nu: F) -> Self
pub fn with_nu(self, nu: F) -> Self
Set the nu parameter (upper bound on the fraction of training errors).
Sourcepub fn with_fit_intercept(self, fit_intercept: bool) -> Self
pub fn with_fit_intercept(self, fit_intercept: bool) -> Self
Set whether the intercept (bias) term is fit.
Sourcepub fn with_max_iter(self, max_iter: usize) -> Self
pub fn with_max_iter(self, max_iter: usize) -> Self
Set the maximum number of epochs.
Sourcepub fn with_shuffle(self, shuffle: bool) -> Self
pub fn with_shuffle(self, shuffle: bool) -> Self
Set whether the training data is shuffled after each epoch.
Sourcepub fn with_learning_rate(self, lr: LearningRateSchedule<F>) -> Self
pub fn with_learning_rate(self, lr: LearningRateSchedule<F>) -> Self
Set the learning rate schedule.
Sourcepub fn with_power_t(self, power_t: F) -> Self
pub fn with_power_t(self, power_t: F) -> Self
Set the power parameter for inverse scaling.
Sourcepub fn with_random_state(self, seed: u64) -> Self
pub fn with_random_state(self, seed: u64) -> Self
Set the random seed for reproducibility.
Sourcepub fn with_n_iter_no_change(self, n_iter_no_change: usize) -> Self
pub fn with_n_iter_no_change(self, n_iter_no_change: usize) -> Self
Set the number of consecutive non-improving epochs before convergence.
Sourcepub fn fit_one_class(
&self,
x: &Array2<F>,
) -> Result<FittedSGDOneClassSVM<F>, FerroError>
pub fn fit_one_class( &self, x: &Array2<F>, ) -> Result<FittedSGDOneClassSVM<F>, FerroError>
Fit the linear One-Class SVM on x (the X-only fit shape).
This is the inherent entry point mirroring sklearn’s fit(X)
(_stochastic_gradient.py:2554-2600). The Fit trait impl with a
unit target () delegates here.
§Errors
FerroError::InvalidParameterifnuis not in(0, 1](_stochastic_gradient.py:2236,Interval(Real, 0.0, 1.0, closed="right")).FerroError::InvalidParameterifeta0is not positive for theconstant/invscaling/adaptiveschedules.FerroError::InsufficientSamplesifxhas no rows.
Trait Implementations§
Source§impl<F: Clone> Clone for SGDOneClassSVM<F>
impl<F: Clone> Clone for SGDOneClassSVM<F>
Source§fn clone(&self) -> SGDOneClassSVM<F>
fn clone(&self) -> SGDOneClassSVM<F>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<F: Debug> Debug for SGDOneClassSVM<F>
impl<F: Debug> Debug for SGDOneClassSVM<F>
Source§impl<F: Float> Default for SGDOneClassSVM<F>
impl<F: Float> Default for SGDOneClassSVM<F>
Source§impl<F: Float + Send + Sync + ScalarOperand + 'static> Fit<ArrayBase<OwnedRepr<F>, Dim<[usize; 2]>>, ()> for SGDOneClassSVM<F>
impl<F: Float + Send + Sync + ScalarOperand + 'static> Fit<ArrayBase<OwnedRepr<F>, Dim<[usize; 2]>>, ()> for SGDOneClassSVM<F>
Source§fn fit(
&self,
x: &Array2<F>,
_y: &(),
) -> Result<FittedSGDOneClassSVM<F>, FerroError>
fn fit( &self, x: &Array2<F>, _y: &(), ) -> Result<FittedSGDOneClassSVM<F>, FerroError>
Fit the linear One-Class SVM. The target y is ignored (present for API
consistency, mirroring sklearn’s fit(X, y=None),
_stochastic_gradient.py:2554); the fit uses y = ones(n) internally.
§Errors
Source§type Fitted = FittedSGDOneClassSVM<F>
type Fitted = FittedSGDOneClassSVM<F>
fit.Source§type Error = FerroError
type Error = FerroError
fit.Auto Trait Implementations§
impl<F> Freeze for SGDOneClassSVM<F>where
F: Freeze,
impl<F> RefUnwindSafe for SGDOneClassSVM<F>where
F: RefUnwindSafe,
impl<F> Send for SGDOneClassSVM<F>where
F: Send,
impl<F> Sync for SGDOneClassSVM<F>where
F: Sync,
impl<F> Unpin for SGDOneClassSVM<F>where
F: Unpin,
impl<F> UnsafeUnpin for SGDOneClassSVM<F>where
F: UnsafeUnpin,
impl<F> UnwindSafe for SGDOneClassSVM<F>where
F: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T, U> Imply<T> for U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more