Skip to main content

BayesSearchCV

Struct BayesSearchCV 

Source
#[non_exhaustive]
pub struct BayesSearchCV { /* private fields */ }
Expand description

Bayesian hyperparameter optimization with cross-validation.

Uses a Tree-structured Parzen Estimator (TPE) to model the objective function and focus evaluations on the most promising hyperparameter combinations.

§Algorithm

  1. Evaluate n_initial random samples to bootstrap the surrogate model.
  2. For each remaining iteration, split observed results at the gamma quantile into “good” and “bad” groups.
  3. Build factored 1D kernel density estimates for each group.
  4. Draw 100 random candidates and pick the one maximizing l(x) / g(x).
  5. Evaluate the chosen candidate and add it to the history.

§Examples

use scry_learn::prelude::*;
use scry_learn::search::*;

let mut space = ParamSpace::new();
space.insert("max_depth".into(), ParamDistribution::IntUniform { low: 2, high: 10 });

let result = BayesSearchCV::new(DecisionTreeClassifier::new(), space)
    .n_iter(20)
    .cv(3)
    .fit(&data)
    .unwrap();

println!("Best score: {:.3}", result.best_score());

Implementations§

Source§

impl BayesSearchCV

Source

pub fn new(model: impl Tunable + 'static, param_space: ParamSpace) -> Self

Create a Bayesian search over the given model and parameter space.

Defaults: 30 iterations, 10 initial random samples, gamma 0.25, 5-fold CV, accuracy scorer, seed 42, non-stratified.

Source

pub fn n_iter(self, n: usize) -> Self

Set the total number of iterations (default: 30).

Source

pub fn n_initial(self, n: usize) -> Self

Set the number of initial random exploration samples (default: 10).

Source

pub fn gamma(self, gamma: f64) -> Self

Set the quantile threshold for splitting good/bad observations (default: 0.25).

Source

pub fn cv(self, k: usize) -> Self

Set the number of cross-validation folds (default: 5).

Source

pub fn scoring(self, scorer: ScoringFn) -> Self

Set the scoring function (default: accuracy).

Source

pub fn seed(self, seed: u64) -> Self

Set the random seed (default: 42).

Source

pub fn stratified(self, stratified: bool) -> Self

Enable stratified k-fold CV (default: false).

When true, uses stratified_k_fold to preserve class proportions in each fold.

Source

pub fn fit(self, data: &Dataset) -> Result<Self>

Run the Bayesian optimization search.

Returns self for chained accessor calls.

Source

pub fn best_params(&self) -> &HashMap<String, ParamValue>

The best parameter combination found.

§Panics

Panics if called before fit.

Source

pub fn best_score(&self) -> f64

The best mean CV score achieved.

Source

pub fn cv_results(&self) -> &[CvResult]

All evaluated combinations with their scores.

Trait Implementations§

Source§

impl Debug for BayesSearchCV

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.