pub struct SparseRandomProjection<F> { /* private fields */ }Expand description
Sparse random projection transformer.
Projects data into a lower-dimensional space using a sparse random matrix
with entries {-1, 0, +1} drawn with probabilities
{d/2, 1 - d, d/2}, scaled by sqrt(1 / (d * n_components)).
The default density d = 1 / sqrt(n_features) is used when not specified.
§Examples
use ferrolearn_preprocess::random_projection::SparseRandomProjection;
use ferrolearn_core::traits::{Fit, Transform};
use ndarray::Array2;
let x = Array2::<f64>::ones((10, 100));
let proj = SparseRandomProjection::<f64>::new(5).random_state(42);
let fitted = proj.fit(&x, &()).unwrap();
let out = fitted.transform(&x).unwrap();
assert_eq!(out.shape(), &[10, 5]);Implementations§
Source§impl<F: Float + Send + Sync + 'static> SparseRandomProjection<F>
impl<F: Float + Send + Sync + 'static> SparseRandomProjection<F>
Sourcepub fn new(n_components: usize) -> Self
pub fn new(n_components: usize) -> Self
Create a new sparse random projection with an explicit n_components
number of output dimensions (NComponents::Fixed).
Sourcepub fn new_auto() -> Self
pub fn new_auto() -> Self
Create a new sparse random projection with n_components='auto'
(sklearn default): resolved at fit time from the Johnson-Lindenstrauss
bound for the configured eps (random_projection.py:388-391).
Sourcepub fn random_state(self, seed: u64) -> Self
pub fn random_state(self, seed: u64) -> Self
Set the random seed for reproducibility.
Trait Implementations§
Source§impl<F: Clone> Clone for SparseRandomProjection<F>
impl<F: Clone> Clone for SparseRandomProjection<F>
Source§fn clone(&self) -> SparseRandomProjection<F>
fn clone(&self) -> SparseRandomProjection<F>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<F: Debug> Debug for SparseRandomProjection<F>
impl<F: Debug> Debug for SparseRandomProjection<F>
Source§impl<F: Float + Send + Sync + 'static> Fit<ArrayBase<OwnedRepr<F>, Dim<[usize; 2]>>, ()> for SparseRandomProjection<F>
impl<F: Float + Send + Sync + 'static> Fit<ArrayBase<OwnedRepr<F>, Dim<[usize; 2]>>, ()> for SparseRandomProjection<F>
Source§fn fit(
&self,
x: &Array2<F>,
_y: &(),
) -> Result<FittedSparseRandomProjection<F>, FerroError>
fn fit( &self, x: &Array2<F>, _y: &(), ) -> Result<FittedSparseRandomProjection<F>, FerroError>
Fit the projection by generating a sparse random matrix.
When n_components is NComponents::Auto the target dimension is
resolved from the Johnson-Lindenstrauss bound for eps
(random_projection.py:388-391).
§Errors
Returns FerroError::InvalidParameter if the resolved n_components
is 0 or density is not in (0, 1].
Returns FerroError::InsufficientSamples if x has zero rows.
Source§type Fitted = FittedSparseRandomProjection<F>
type Fitted = FittedSparseRandomProjection<F>
fit.Source§type Error = FerroError
type Error = FerroError
fit.Source§impl<F: Float + Send + Sync + 'static> FitTransform<ArrayBase<OwnedRepr<F>, Dim<[usize; 2]>>> for SparseRandomProjection<F>
impl<F: Float + Send + Sync + 'static> FitTransform<ArrayBase<OwnedRepr<F>, Dim<[usize; 2]>>> for SparseRandomProjection<F>
Source§fn fit_transform(&self, x: &Array2<F>) -> Result<Array2<F>, FerroError>
fn fit_transform(&self, x: &Array2<F>) -> Result<Array2<F>, FerroError>
Fit and transform in one step.
Source§type FitError = FerroError
type FitError = FerroError
Source§impl<F: Float + Send + Sync + 'static> PipelineTransformer<F> for SparseRandomProjection<F>
impl<F: Float + Send + Sync + 'static> PipelineTransformer<F> for SparseRandomProjection<F>
Source§fn fit_pipeline(
&self,
x: &Array2<F>,
_y: &Array1<F>,
) -> Result<Box<dyn FittedPipelineTransformer<F>>, FerroError>
fn fit_pipeline( &self, x: &Array2<F>, _y: &Array1<F>, ) -> Result<Box<dyn FittedPipelineTransformer<F>>, FerroError>
Auto Trait Implementations§
impl<F> Freeze for SparseRandomProjection<F>
impl<F> RefUnwindSafe for SparseRandomProjection<F>where
F: RefUnwindSafe,
impl<F> Send for SparseRandomProjection<F>where
F: Send,
impl<F> Sync for SparseRandomProjection<F>where
F: Sync,
impl<F> Unpin for SparseRandomProjection<F>where
F: Unpin,
impl<F> UnsafeUnpin for SparseRandomProjection<F>
impl<F> UnwindSafe for SparseRandomProjection<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,
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§unsafe fn to_subset_unchecked(&self) -> SS
unsafe fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.