Skip to main content

AlgorithmPlugin

Trait AlgorithmPlugin 

Source
pub trait AlgorithmPlugin<X, Y, Output>:
    Plugin
    + Send
    + Sync {
    type Fitted: Predict<X, Output> + Send + Sync;

    // Required methods
    fn fit(&self, x: &X, y: &Y) -> Result<Self::Fitted>;
    fn predict(&self, fitted: &Self::Fitted, x: &X) -> Result<Output>;
    fn get_parameters(&self) -> HashMap<String, PluginParameter>;
    fn set_parameters(
        &mut self,
        params: HashMap<String, PluginParameter>,
    ) -> Result<()>;
}
Expand description

Trait for algorithm plugins that can fit and predict

This trait defines the interface for machine learning algorithms that follow the fit/predict pattern. It extends the base Plugin trait with algorithm-specific functionality.

§Type Parameters

  • X - The input feature type (e.g., Array2<f64>)
  • Y - The target/label type (e.g., Array1<f64>)
  • Output - The prediction output type

§Examples

use sklears_core::plugin::{Plugin, AlgorithmPlugin, PluginParameter};
use sklears_core::traits::Predict;
use sklears_core::error::Result;
use std::collections::HashMap;

#[derive(Debug)]
struct LinearRegression {
    // algorithm parameters
}

struct FittedLinearRegression {
    // fitted model state
}

impl Predict<Vec<f64>, Vec<f64>> for FittedLinearRegression {
    fn predict(&self, x: &Vec<f64>) -> Result<Vec<f64>> {
        // prediction implementation
        Ok(x.clone())
    }
}

impl AlgorithmPlugin<Vec<f64>, Vec<f64>, Vec<f64>> for LinearRegression {
    type Fitted = FittedLinearRegression;

    fn fit(&self, x: &Vec<f64>, y: &Vec<f64>) -> Result<Self::Fitted> {
        Ok(FittedLinearRegression {})
    }

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

    fn get_parameters(&self) -> HashMap<String, PluginParameter> {
        HashMap::new()
    }

    fn set_parameters(&mut self, _params: HashMap<String, PluginParameter>) -> Result<()> {
        Ok(())
    }
}

Required Associated Types§

Source

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

The fitted model type

This type represents the state of the algorithm after training. It must implement the Predict trait to enable predictions.

Required Methods§

Source

fn fit(&self, x: &X, y: &Y) -> Result<Self::Fitted>

Fit the algorithm to training data

This method trains the algorithm on the provided data and returns a fitted model that can be used for predictions.

§Arguments
  • x - Training features
  • y - Training targets/labels
§Returns

A fitted model instance or an error if training fails.

Source

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

Make predictions using the fitted model

This method uses the fitted model to make predictions on new data.

§Arguments
  • fitted - The fitted model from the fit method
  • x - Input features for prediction
§Returns

Predictions or an error if prediction fails.

Source

fn get_parameters(&self) -> HashMap<String, PluginParameter>

Get algorithm-specific parameters

Returns a map of all configurable parameters for this algorithm. This enables introspection and parameter tuning.

Source

fn set_parameters( &mut self, params: HashMap<String, PluginParameter>, ) -> Result<()>

Set algorithm-specific parameters

Allows updating the algorithm’s parameters. The algorithm should validate that the provided parameters are valid.

§Arguments
  • params - Map of parameter names to values
§Returns

Ok(()) if parameters were set successfully, or an error if any parameter is invalid.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§