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 (f32orf64).
§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: ClassifierLossThe loss function to use.
learning_rate: LearningRateSchedule<F>The learning rate schedule.
eta0: FInitial learning rate.
alpha: FL2 regularization strength.
max_iter: usizeMaximum number of passes over the training data.
tol: FConvergence tolerance. Training stops when the loss improvement is below this threshold.
random_state: Option<u64>Optional random seed for sample shuffling.
power_t: FPower parameter for inverse scaling schedule.
Implementations§
Source§impl<F: Float> SGDClassifier<F>
impl<F: Float> SGDClassifier<F>
Sourcepub fn new() -> Self
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.
Sourcepub fn with_loss(self, loss: ClassifierLoss) -> Self
pub fn with_loss(self, loss: ClassifierLoss) -> Self
Set the loss function.
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_alpha(self, alpha: F) -> Self
pub fn with_alpha(self, alpha: F) -> Self
Set the L2 regularization strength.
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_random_state(self, seed: u64) -> Self
pub fn with_random_state(self, seed: u64) -> Self
Set the random seed for reproducibility.
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.
Trait Implementations§
Source§impl<F: Clone> Clone for SGDClassifier<F>
impl<F: Clone> Clone for SGDClassifier<F>
Source§fn clone(&self) -> SGDClassifier<F>
fn clone(&self) -> SGDClassifier<F>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<F: Debug> Debug for SGDClassifier<F>
impl<F: Debug> Debug for SGDClassifier<F>
Source§impl<F: Float> Default for SGDClassifier<F>
impl<F: Float> Default for SGDClassifier<F>
Source§impl<F: Float + Send + Sync + ScalarOperand + 'static> Fit<ArrayBase<OwnedRepr<F>, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<usize>, Dim<[usize; 1]>>> for SGDClassifier<F>
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>
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>
type Fitted = FittedSGDClassifier<F>
fit.Source§type Error = FerroError
type Error = FerroError
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>
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>
fn partial_fit( self, x: &Array2<F>, y: &Array1<usize>, ) -> Result<FittedSGDClassifier<F>, FerroError>
Source§type FitResult = FittedSGDClassifier<F>
type FitResult = FittedSGDClassifier<F>
partial_fit. Read moreSource§type Error = FerroError
type Error = FerroError
partial_fit.Source§impl<F> PipelineEstimator<F> for SGDClassifier<F>
impl<F> PipelineEstimator<F> for SGDClassifier<F>
Source§fn fit_pipeline(
&self,
x: &Array2<F>,
y: &Array1<F>,
) -> Result<Box<dyn FittedPipelineEstimator<F>>, FerroError>
fn fit_pipeline( &self, x: &Array2<F>, y: &Array1<F>, ) -> Result<Box<dyn FittedPipelineEstimator<F>>, FerroError>
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> 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,
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