Skip to main content

Estimator

Trait Estimator 

Source
pub trait Estimator {
    // Required methods
    fn fit(&mut self, x: &Matrix<f32>, y: &Vector<f32>) -> Result<()>;
    fn predict(&self, x: &Matrix<f32>) -> Vector<f32>;
    fn score(&self, x: &Matrix<f32>, y: &Vector<f32>) -> f32;
}
Expand description

Primary trait for supervised learning estimators.

Estimators implement fit/predict/score following sklearn conventions.

§Examples

use aprender::prelude::*;

// Create training data: y = 2x + 1
let x_train = Matrix::from_vec(4, 1, vec![1.0, 2.0, 3.0, 4.0]).unwrap();
let y_train = Vector::from_slice(&[3.0, 5.0, 7.0, 9.0]);

// Test data
let x_test = Matrix::from_vec(2, 1, vec![5.0, 6.0]).unwrap();
let y_test = Vector::from_slice(&[11.0, 13.0]);

let mut model = LinearRegression::new();
model.fit(&x_train, &y_train).unwrap();
let predictions = model.predict(&x_test);
let score = model.score(&x_test, &y_test);
assert!(score > 0.99);

Required Methods§

Source

fn fit(&mut self, x: &Matrix<f32>, y: &Vector<f32>) -> Result<()>

Fits the model to training data.

§Errors

Returns an error if fitting fails (dimension mismatch, singular matrix, etc.).

Source

fn predict(&self, x: &Matrix<f32>) -> Vector<f32>

Predicts target values for input data.

Source

fn score(&self, x: &Matrix<f32>, y: &Vector<f32>) -> f32

Computes the score (R² for regression, accuracy for classification).

Implementors§