TransformerPlugin

Trait TransformerPlugin 

Source
pub trait TransformerPlugin<X, Output = X>:
    Plugin
    + Send
    + Sync {
    type Fitted: Transform<X, Output> + Send + Sync;

    // Required methods
    fn fit_transform(&self, x: &X) -> Result<(Self::Fitted, Output)>;
    fn transform(&self, fitted: &Self::Fitted, x: &X) -> Result<Output>;
}
Expand description

Trait for transformer plugins

This trait defines the interface for data transformation algorithms that can fit to data and then transform new data using the learned transformation.

§Type Parameters

  • X - The input data type
  • Output - The transformed output type (defaults to X)

§Examples

use sklears_core::plugin::{Plugin, TransformerPlugin};
use sklears_core::traits::Transform;
use sklears_core::error::Result;

#[derive(Debug)]
struct StandardScaler {
    // scaler parameters
}

struct FittedStandardScaler {
    // fitted scaler state
}

impl Transform<Vec<f64>, Vec<f64>> for FittedStandardScaler {
    fn transform(&self, x: &Vec<f64>) -> Result<Vec<f64>> {
        // transformation implementation
        Ok(x.clone())
    }
}

impl TransformerPlugin<Vec<f64>, Vec<f64>> for StandardScaler {
    type Fitted = FittedStandardScaler;

    fn fit_transform(&self, x: &Vec<f64>) -> Result<(Self::Fitted, Vec<f64>)> {
        let fitted = FittedStandardScaler {};
        let transformed = fitted.transform(x)?;
        Ok((fitted, transformed))
    }

    fn transform(&self, fitted: &Self::Fitted, x: &Vec<f64>) -> Result<Vec<f64>> {
        fitted.transform(x)
    }
}

Required Associated Types§

Source

type Fitted: Transform<X, Output> + Send + Sync

The fitted transformer type

This type represents the state of the transformer after fitting. It must implement the Transform trait to enable transformations.

Required Methods§

Source

fn fit_transform(&self, x: &X) -> Result<(Self::Fitted, Output)>

Fit the transformer to data and return the fitted transformer along with transformed data

This method fits the transformer to the input data and immediately applies the transformation, returning both the fitted transformer and the transformed data.

§Arguments
  • x - Input data to fit and transform
§Returns

A tuple of (fitted transformer, transformed data) or an error.

Source

fn transform(&self, fitted: &Self::Fitted, x: &X) -> Result<Output>

Transform data using the fitted transformer

This method applies the previously fitted transformation to new data.

§Arguments
  • fitted - The fitted transformer from fit_transform
  • x - Input data to transform
§Returns

Transformed data or an error if transformation fails.

Implementors§