sklears_svm/hyperparameter_optimization/
mod.rs1pub mod bayesian_optimization;
14pub mod evolutionary_optimization;
15pub mod grid_search;
16pub mod random_search;
17
18pub use bayesian_optimization::BayesianOptimizationCV;
19pub use evolutionary_optimization::EvolutionaryOptimizationCV;
20pub use grid_search::GridSearchCV;
21pub use random_search::RandomSearchCV;
22
23use crate::kernels::KernelType;
24
25#[derive(Debug, Clone)]
27pub enum ParameterSpec {
28 Fixed(f64),
30 Uniform { min: f64, max: f64 },
32 LogUniform { min: f64, max: f64 },
34 Choice(Vec<f64>),
36 KernelChoice(Vec<KernelType>),
38}
39
40#[derive(Debug, Clone)]
42pub struct SearchSpace {
43 pub c: ParameterSpec,
45 pub gamma: Option<ParameterSpec>,
47 pub degree: Option<ParameterSpec>,
49 pub coef0: Option<ParameterSpec>,
51 pub kernel: Option<ParameterSpec>,
53 pub tol: Option<ParameterSpec>,
55 pub max_iter: Option<ParameterSpec>,
57}
58
59impl Default for SearchSpace {
60 fn default() -> Self {
61 Self {
62 c: ParameterSpec::LogUniform {
63 min: 1e-3,
64 max: 1e3,
65 },
66 gamma: Some(ParameterSpec::LogUniform {
67 min: 1e-4,
68 max: 1e2,
69 }),
70 degree: Some(ParameterSpec::Choice(vec![2.0, 3.0, 4.0, 5.0])),
71 coef0: Some(ParameterSpec::Uniform { min: 0.0, max: 1.0 }),
72 kernel: Some(ParameterSpec::KernelChoice(vec![
73 KernelType::Linear,
74 KernelType::Rbf { gamma: 1.0 },
75 KernelType::Polynomial {
76 gamma: 1.0,
77 degree: 3.0,
78 coef0: 1.0,
79 },
80 ])),
81 tol: Some(ParameterSpec::LogUniform {
82 min: 1e-6,
83 max: 1e-2,
84 }),
85 max_iter: Some(ParameterSpec::Choice(vec![100.0, 500.0, 1000.0, 5000.0])),
86 }
87 }
88}
89
90#[derive(Debug, Clone)]
92pub struct OptimizationConfig {
93 pub n_iterations: usize,
95 pub cv_folds: usize,
97 pub scoring: ScoringMetric,
99 pub random_state: Option<u64>,
101 pub n_jobs: Option<usize>,
103 pub verbose: bool,
105 pub early_stopping_patience: Option<usize>,
107}
108
109impl Default for OptimizationConfig {
110 fn default() -> Self {
111 Self {
112 n_iterations: 100,
113 cv_folds: 5,
114 scoring: ScoringMetric::Accuracy,
115 random_state: Some(42),
116 n_jobs: None,
117 verbose: false,
118 early_stopping_patience: Some(10),
119 }
120 }
121}
122
123#[derive(Debug, Clone)]
125pub enum ScoringMetric {
126 Accuracy,
127 Precision,
128 Recall,
129 F1Score,
130 AUC,
131 MeanSquaredError,
132 MeanAbsoluteError,
133 R2Score,
134}
135
136#[derive(Debug, Clone)]
138pub struct ParameterSet {
139 pub c: f64,
140 pub kernel: KernelType,
141 pub tol: f64,
142 pub max_iter: usize,
143}
144
145impl ParameterSet {
146 pub fn new() -> Self {
147 Self {
148 c: 1.0,
149 kernel: KernelType::Rbf { gamma: 1.0 },
150 tol: 1e-3,
151 max_iter: 1000,
152 }
153 }
154}
155
156impl Default for ParameterSet {
157 fn default() -> Self {
158 Self::new()
159 }
160}
161
162#[derive(Debug, Clone)]
164pub struct OptimizationResult {
165 pub best_params: ParameterSet,
167 pub best_score: f64,
169 pub cv_results: Vec<(ParameterSet, f64)>,
171 pub n_iterations: usize,
173 pub optimization_time: f64,
175 pub score_history: Vec<f64>,
177}