grid_search_alpha

Function grid_search_alpha 

Source
pub fn grid_search_alpha(
    model_type: &str,
    alphas: &[f32],
    x: &Matrix<f32>,
    y: &Vector<f32>,
    cv: &KFold,
    l1_ratio: Option<f32>,
) -> Result<GridSearchResult, String>
Expand description

Performs grid search over alpha parameter for regularized linear models.

Exhaustively evaluates all provided alpha values using K-fold cross-validation and returns the alpha that achieves the highest cross-validation score.

§Arguments

  • model_type - Type of model: “ridge”, “lasso”, or “elastic_net”
  • alphas - Vector of alpha values to try
  • x - Feature matrix
  • y - Target vector
  • cv - Cross-validation splitter
  • l1_ratio - Optional l1_ratio for ElasticNet (ignored for Ridge/Lasso)

§Returns

GridSearchResult containing best alpha, best score, and all results

§Example

use aprender::model_selection::{grid_search_alpha, KFold};
use aprender::primitives::{Matrix, Vector};

let x_data: Vec<f32> = (0..50).map(|i| i as f32).collect();
let y_data: Vec<f32> = x_data.iter().map(|&x| 2.0 * x + 1.0).collect();

let x = Matrix::from_vec(50, 1, x_data).expect("Matrix creation should succeed with valid dimensions and data");
let y = Vector::from_vec(y_data);

let alphas = vec![0.001, 0.01, 0.1, 1.0, 10.0];
let kfold = KFold::new(5).with_random_state(42);

let result = grid_search_alpha("ridge", &alphas, &x, &y, &kfold, None).expect("Grid search should succeed with valid inputs");
println!("Best alpha: {}, Best score: {}", result.best_alpha, result.best_score);