Skip to main content

SGDOneClassSVM

Struct SGDOneClassSVM 

Source
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 (f32 or f64).

§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: F

The 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: bool

Whether to fit (update) the intercept. Defaults to true (_stochastic_gradient.py:2104-2105,2248).

§max_iter: usize

Maximum number of passes over the training data. Defaults to 1000 (_stochastic_gradient.py:2107,2249).

§tol: F

Convergence 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: bool

Whether 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: F

Initial learning rate for the constant/invscaling/adaptive schedules. Defaults to 0.0 (_stochastic_gradient.py:2145,2255).

§power_t: F

Power 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: usize

Number 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>

Source

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.

Source

pub fn with_nu(self, nu: F) -> Self

Set the nu parameter (upper bound on the fraction of training errors).

Source

pub fn with_fit_intercept(self, fit_intercept: bool) -> Self

Set whether the intercept (bias) term is fit.

Source

pub fn with_max_iter(self, max_iter: usize) -> Self

Set the maximum number of epochs.

Source

pub fn with_tol(self, tol: F) -> Self

Set the convergence tolerance.

Source

pub fn with_shuffle(self, shuffle: bool) -> Self

Set whether the training data is shuffled after each epoch.

Source

pub fn with_learning_rate(self, lr: LearningRateSchedule<F>) -> Self

Set the learning rate schedule.

Source

pub fn with_eta0(self, eta0: F) -> Self

Set the initial learning rate.

Source

pub fn with_power_t(self, power_t: F) -> Self

Set the power parameter for inverse scaling.

Source

pub fn with_random_state(self, seed: u64) -> Self

Set the random seed for reproducibility.

Source

pub fn with_n_iter_no_change(self, n_iter_no_change: usize) -> Self

Set the number of consecutive non-improving epochs before convergence.

Source

pub fn fit_one_class( &self, x: &Array2<F>, ) -> Result<FittedSGDOneClassSVM<F>, FerroError>
where F: Send + Sync + ScalarOperand + 'static,

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

Trait Implementations§

Source§

impl<F: Clone> Clone for SGDOneClassSVM<F>

Source§

fn clone(&self) -> SGDOneClassSVM<F>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F: Debug> Debug for SGDOneClassSVM<F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F: Float> Default for SGDOneClassSVM<F>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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>

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

See SGDOneClassSVM::fit_one_class.

Source§

type Fitted = FittedSGDOneClassSVM<F>

The fitted model type returned by fit.
Source§

type Error = FerroError

The error type returned by 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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ByRef<T> for T

Source§

fn by_ref(&self) -> &T

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DistributionExt for T
where T: ?Sized,

Source§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V