pub struct TargetEncoder { /* private fields */ }
Expand description
TargetEncoder for supervised categorical encoding
This encoder transforms categorical features using the target variable values, encoding each category with a statistic (mean, median, etc.) of the target values for that category. This is useful for high-cardinality categorical features.
§Examples
use ndarray::Array2;
use scirs2_transform::encoding::TargetEncoder;
let x = Array2::from_shape_vec((6, 1), vec![0.0, 1.0, 2.0, 0.0, 1.0, 2.0]).unwrap();
let y = vec![1.0, 2.0, 3.0, 1.5, 2.5, 3.5];
let mut encoder = TargetEncoder::new("mean", 1.0, 0.0).unwrap();
let encoded = encoder.fit_transform(&x, &y).unwrap();
Implementations§
Source§impl TargetEncoder
impl TargetEncoder
Sourcepub fn with_mean(smoothing: f64) -> Self
pub fn with_mean(smoothing: f64) -> Self
Creates a TargetEncoder with mean strategy and default smoothing
Sourcepub fn with_median(smoothing: f64) -> Self
pub fn with_median(smoothing: f64) -> Self
Creates a TargetEncoder with median strategy
Sourcepub fn fit_transform<S>(
&mut self,
x: &ArrayBase<S, Ix2>,
y: &[f64],
) -> Result<Array2<f64>>
pub fn fit_transform<S>( &mut self, x: &ArrayBase<S, Ix2>, y: &[f64], ) -> Result<Array2<f64>>
Sourcepub fn encodings(&self) -> Option<&Vec<HashMap<u64, f64>>>
pub fn encodings(&self) -> Option<&Vec<HashMap<u64, f64>>>
Returns the learned encodings for each feature
§Returns
Option<&Vec<HashMap<u64, f64>>>
- The category encodings for each feature
Sourcepub fn global_mean(&self) -> f64
pub fn global_mean(&self) -> f64
Returns the global mean computed during fitting
Sourcepub fn fit_transform_cv<S>(
&mut self,
x: &ArrayBase<S, Ix2>,
y: &[f64],
cv_folds: usize,
) -> Result<Array2<f64>>
pub fn fit_transform_cv<S>( &mut self, x: &ArrayBase<S, Ix2>, y: &[f64], cv_folds: usize, ) -> Result<Array2<f64>>
Applies cross-validation target encoding to prevent overfitting
This method performs k-fold cross-validation to compute target encodings, which helps prevent overfitting when the same data is used for both fitting and transforming.
§Arguments
x
- The input categorical data, shape (n_samples, n_features)y
- The target values, length n_samplescv_folds
- Number of cross-validation folds (default: 5)
§Returns
Result<Array2<f64>>
- The cross-validated target-encoded data
Trait Implementations§
Source§impl Clone for TargetEncoder
impl Clone for TargetEncoder
Source§fn clone(&self) -> TargetEncoder
fn clone(&self) -> TargetEncoder
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl Freeze for TargetEncoder
impl RefUnwindSafe for TargetEncoder
impl Send for TargetEncoder
impl Sync for TargetEncoder
impl Unpin for TargetEncoder
impl UnwindSafe for TargetEncoder
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
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 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>
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 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>
The inverse inclusion map: attempts to construct
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
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
Use with care! Same as
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self
to the equivalent element of its superset.