pub struct LDA<F> { /* private fields */ }Expand description
Linear Discriminant Analysis configuration.
Holds hyperparameters. Calling Fit::fit runs sklearn’s default
solver="svd" path (discriminant_analysis.py:487-559) and returns a
FittedLDA.
Use LDA::with_priors to supply class priors (sklearn’s priors,
discriminant_analysis.py:359); the default None infers the empirical
priors n_k / n at fit time.
§Type Parameters
F: The floating-point scalar type (f32orf64).
Implementations§
Source§impl<F: Float + Send + Sync + 'static> LDA<F>
impl<F: Float + Send + Sync + 'static> LDA<F>
Sourcepub fn new(n_components: Option<usize>) -> Self
pub fn new(n_components: Option<usize>) -> Self
Create a new LDA.
n_components: number of discriminant directions to retain. PassNoneto usemin(n_classes - 1, n_features).
Sourcepub fn n_components(&self) -> Option<usize>
pub fn n_components(&self) -> Option<usize>
Return the configured number of components (may be None).
Sourcepub fn with_priors(self, priors: Array1<F>) -> Self
pub fn with_priors(self, priors: Array1<F>) -> Self
Set the class prior probabilities (sklearn’s priors,
discriminant_analysis.py:351,359).
The provided vector is used VERBATIM as priors_ (sklearn does not
normalize it here when it already sums to 1, :605). Its length must
equal the number of classes seen at fit time, or Fit::fit returns
FerroError::ShapeMismatch. Pass nothing (the None default) to infer
the empirical priors n_k / n from the training data (:601-603).
Sourcepub fn priors(&self) -> Option<&Array1<F>>
pub fn priors(&self) -> Option<&Array1<F>>
Return the configured class priors (None ⇒ empirical at fit time).
Mirrors sklearn’s constructor priors (discriminant_analysis.py:359).
Sourcepub fn with_solver(self, solver: Solver) -> Self
pub fn with_solver(self, solver: Solver) -> Self
Set the solver (sklearn’s solver, discriminant_analysis.py:204,349).
Default Solver::Svd. See Solver.
Solver::Lsqr enables the least-squares path (and shrinkage);
Solver::Eigen enables the generalized-eigenvalue path (also supports
shrinkage and transform).
Sourcepub fn solver(&self) -> Solver
pub fn solver(&self) -> Solver
Return the configured solver. Mirrors sklearn’s constructor solver
(discriminant_analysis.py:349, default "svd").
Sourcepub fn with_shrinkage(self, shrinkage: Shrinkage<F>) -> Self
pub fn with_shrinkage(self, shrinkage: Shrinkage<F>) -> Self
Set the covariance shrinkage (sklearn’s shrinkage,
discriminant_analysis.py:218,350). Default Shrinkage::None. See
Shrinkage.
Honored only by Solver::Lsqr here (sklearn note :225: shrinkage
works only with the lsqr/eigen solvers). Combined with
Solver::Svd, Fit::fit returns a FerroError mirroring sklearn’s
NotImplementedError("shrinkage not supported with 'svd' solver.")
(:628-629). Shrinkage::Fixed(s) requires 0 <= s <= 1 (sklearn
Interval(Real, 0, 1, closed="both"), :339), else Fit::fit returns
FerroError::InvalidParameter.
Sourcepub fn shrinkage(&self) -> Shrinkage<F>
pub fn shrinkage(&self) -> Shrinkage<F>
Return the configured covariance shrinkage. Mirrors sklearn’s constructor
shrinkage (discriminant_analysis.py:350, default None).
Sourcepub fn with_store_covariance(self, store_covariance: bool) -> Self
pub fn with_store_covariance(self, store_covariance: bool) -> Self
Set whether to compute and store the shared within-class covariance
matrix covariance_ during fit (sklearn’s store_covariance,
discriminant_analysis.py:353,361). Default false.
When true, Fit::fit computes covariance_ = Σ_k priors_[k] · cov(X_k) (:509-510, _class_cov :128-172) and
FittedLDA::covariance returns Some. When false it returns None
(matching sklearn, where the attribute only exists when the flag is set).
Sourcepub fn store_covariance(&self) -> bool
pub fn store_covariance(&self) -> bool
Return whether covariance_ will be stored during fit (sklearn’s
store_covariance, discriminant_analysis.py:353).
Trait Implementations§
Source§impl<F: LinalgFloat + ScalarOperand> Fit<ArrayBase<OwnedRepr<F>, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<usize>, Dim<[usize; 1]>>> for LDA<F>
impl<F: LinalgFloat + ScalarOperand> Fit<ArrayBase<OwnedRepr<F>, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<usize>, Dim<[usize; 1]>>> for LDA<F>
Source§fn fit(
&self,
x: &Array2<F>,
y: &Array1<usize>,
) -> Result<FittedLDA<F>, FerroError>
fn fit( &self, x: &Array2<F>, y: &Array1<usize>, ) -> Result<FittedLDA<F>, FerroError>
Fit the LDA model via sklearn’s default solver="svd" path
(discriminant_analysis.py:487-559): two SVDs whiten the within-class
data and project onto the between-class subspace, yielding scalings_,
xbar_, coef_, intercept_ (embedding log(priors_)), and
explained_variance_ratio_.
§Errors
FerroError::InsufficientSamplesif fewer than 2 samples / classes.FerroError::InvalidParameterifn_componentsis zero or exceedsmin(n_classes - 1, n_features).FerroError::ShapeMismatchifxandyhave different row counts.FerroError::NumericalInstabilityif an SVD fails.
Source§type Error = FerroError
type Error = FerroError
fit.Source§impl<F: LinalgFloat + ScalarOperand> PipelineEstimator<F> for LDA<F>
impl<F: LinalgFloat + ScalarOperand> PipelineEstimator<F> for LDA<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 LDA<F>where
F: Freeze,
impl<F> RefUnwindSafe for LDA<F>where
F: RefUnwindSafe,
impl<F> Send for LDA<F>where
F: Send,
impl<F> Sync for LDA<F>where
F: Sync,
impl<F> Unpin for LDA<F>where
F: Unpin,
impl<F> UnsafeUnpin for LDA<F>where
F: UnsafeUnpin,
impl<F> UnwindSafe for LDA<F>where
F: UnwindSafe + RefUnwindSafe,
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