Skip to main content

LDA

Struct LDA 

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

Implementations§

Source§

impl<F: Float + Send + Sync + 'static> LDA<F>

Source

pub fn new(n_components: Option<usize>) -> Self

Create a new LDA.

  • n_components: number of discriminant directions to retain. Pass None to use min(n_classes - 1, n_features).
Source

pub fn n_components(&self) -> Option<usize>

Return the configured number of components (may be None).

Source

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).

Source

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).

Source

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).

Source

pub fn solver(&self) -> Solver

Return the configured solver. Mirrors sklearn’s constructor solver (discriminant_analysis.py:349, default "svd").

Source

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.

Source

pub fn shrinkage(&self) -> Shrinkage<F>

Return the configured covariance shrinkage. Mirrors sklearn’s constructor shrinkage (discriminant_analysis.py:350, default None).

Source

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).

Source

pub fn store_covariance(&self) -> bool

Return whether covariance_ will be stored during fit (sklearn’s store_covariance, discriminant_analysis.py:353).

Source

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

Set the singular-value rank threshold tol used by the svd-solver (sklearn’s tol, discriminant_analysis.py:354,362). Default 1e-4.

It drives the two rank cutoffs rank = Σ(S > tol) (:532) and rank2 = Σ(S2 > tol·S2[0]) (:554).

Source

pub fn tol(&self) -> F

Return the configured svd-solver rank threshold tol. Mirrors sklearn’s constructor tol (discriminant_analysis.py:354, default 1e-4).

Trait Implementations§

Source§

impl<F: Clone> Clone for LDA<F>

Source§

fn clone(&self) -> LDA<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 LDA<F>

Source§

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

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

impl<F: Float + Send + Sync + 'static> Default for LDA<F>

Source§

fn default() -> Self

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

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>

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

type Fitted = FittedLDA<F>

The fitted model type returned by fit.
Source§

type Error = FerroError

The error type returned by fit.
Source§

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>

Fit LDA using the pipeline interface.

§Errors

Propagates errors from Fit::fit.

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>

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