Skip to main content

SGDClassifier

Struct SGDClassifier 

Source
pub struct SGDClassifier<F> {
    pub loss: ClassifierLoss,
    pub learning_rate: LearningRateSchedule<F>,
    pub eta0: F,
    pub alpha: F,
    pub max_iter: usize,
    pub tol: F,
    pub random_state: Option<u64>,
    pub power_t: F,
}
Expand description

Stochastic Gradient Descent classifier.

Supports binary classification via a decision boundary and multiclass classification via one-vs-all decomposition.

§Type Parameters

  • F: The floating-point type (f32 or f64).

§Examples

use ferrolearn_linear::sgd::SGDClassifier;
use ferrolearn_core::{Fit, Predict};
use ndarray::{array, Array2};

let x = Array2::from_shape_vec((6, 2), vec![
    1.0, 2.0, 2.0, 3.0, 3.0, 1.0,
    8.0, 7.0, 9.0, 8.0, 7.0, 9.0,
]).unwrap();
let y = array![0, 0, 0, 1, 1, 1];

let clf = SGDClassifier::<f64>::new();
let fitted = clf.fit(&x, &y).unwrap();
let preds = fitted.predict(&x).unwrap();

Fields§

§loss: ClassifierLoss

The loss function to use.

§learning_rate: LearningRateSchedule<F>

The learning rate schedule.

§eta0: F

Initial learning rate.

§alpha: F

L2 regularization strength.

§max_iter: usize

Maximum number of passes over the training data.

§tol: F

Convergence tolerance. Training stops when the loss improvement is below this threshold.

§random_state: Option<u64>

Optional random seed for sample shuffling.

§power_t: F

Power parameter for inverse scaling schedule.

Implementations§

Source§

impl<F: Float> SGDClassifier<F>

Source

pub fn new() -> Self

Create a new SGDClassifier with default settings.

Defaults: loss = Hinge, learning_rate = InvScaling, eta0 = 0.01, alpha = 0.0001, max_iter = 1000, tol = 1e-3, power_t = 0.25.

Source

pub fn with_loss(self, loss: ClassifierLoss) -> Self

Set the loss function.

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_alpha(self, alpha: F) -> Self

Set the L2 regularization strength.

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_random_state(self, seed: u64) -> Self

Set the random seed for reproducibility.

Source

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

Set the power parameter for inverse scaling.

Trait Implementations§

Source§

impl<F: Clone> Clone for SGDClassifier<F>

Source§

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

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<F: Debug> Debug for SGDClassifier<F>

Source§

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

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

impl<F: Float> Default for SGDClassifier<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]>>, ArrayBase<OwnedRepr<usize>, Dim<[usize; 1]>>> for SGDClassifier<F>

Source§

fn fit( &self, x: &Array2<F>, y: &Array1<usize>, ) -> Result<FittedSGDClassifier<F>, FerroError>

Fit the SGD classifier on the given data.

§Errors

Returns FerroError::ShapeMismatch if x and y have mismatched sample counts. Returns FerroError::InsufficientSamples if fewer than 2 classes are present. Returns FerroError::InvalidParameter if eta0 or alpha are not positive.

Source§

type Fitted = FittedSGDClassifier<F>

The fitted model type returned by fit.
Source§

type Error = FerroError

The error type returned by fit.
Source§

impl<F: Float + Send + Sync + ScalarOperand + 'static> PartialFit<ArrayBase<OwnedRepr<F>, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<usize>, Dim<[usize; 1]>>> for SGDClassifier<F>

Source§

fn partial_fit( self, x: &Array2<F>, y: &Array1<usize>, ) -> Result<FittedSGDClassifier<F>, FerroError>

Initial call to partial_fit on an unfitted classifier.

Equivalent to fit but with a single epoch, enabling subsequent incremental calls.

§Errors

Same as Fit::fit.

Source§

type FitResult = FittedSGDClassifier<F>

The result type returned by partial_fit. Read more
Source§

type Error = FerroError

The error type returned by partial_fit.
Source§

impl<F> PipelineEstimator<F> for SGDClassifier<F>
where F: Float + ToPrimitive + FromPrimitive + ScalarOperand + Send + Sync + 'static,

Source§

fn fit_pipeline( &self, x: &Array2<F>, y: &Array1<F>, ) -> Result<Box<dyn FittedPipelineEstimator<F>>, FerroError>

Fit this estimator on the given data. Read more

Auto Trait Implementations§

§

impl<F> Freeze for SGDClassifier<F>
where F: Freeze,

§

impl<F> RefUnwindSafe for SGDClassifier<F>
where F: RefUnwindSafe,

§

impl<F> Send for SGDClassifier<F>
where F: Send,

§

impl<F> Sync for SGDClassifier<F>
where F: Sync,

§

impl<F> Unpin for SGDClassifier<F>
where F: Unpin,

§

impl<F> UnsafeUnpin for SGDClassifier<F>
where F: UnsafeUnpin,

§

impl<F> UnwindSafe for SGDClassifier<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> 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

Source§

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