sklears_ensemble/stacking/
config.rs1use sklears_core::types::Float;
7
8#[derive(Debug, Clone)]
10pub struct StackingConfig {
11 pub cv: usize,
13 pub use_probabilities: bool,
15 pub random_state: Option<u64>,
17 pub passthrough: bool,
19}
20
21impl Default for StackingConfig {
22 fn default() -> Self {
23 Self {
24 cv: 5,
25 use_probabilities: false,
26 random_state: None,
27 passthrough: false,
28 }
29 }
30}
31
32#[derive(Debug, Clone, Copy, PartialEq)]
34pub enum MetaLearningStrategy {
35 LinearRegression,
37 Ridge(Float),
39 Lasso(Float),
41 ElasticNet(Float, Float),
43 LogisticRegression,
45 SVM,
47 NeuralNetwork,
49 BayesianAveraging,
51}
52
53#[derive(Debug, Clone, PartialEq)]
55pub enum MetaFeatureStrategy {
56 Raw,
58 Statistical,
60 Interactions,
62 ConfidenceBased,
64 DiversityBased,
66 Comprehensive,
68 Temporal,
70 Spatial,
72 Spectral,
74 InformationTheoretic,
76 NeuralEmbedding,
78 KernelBased,
80 BasisExpansion,
82 MetaLearning,
84}
85
86impl Default for MetaFeatureStrategy {
87 fn default() -> Self {
88 Self::Raw
89 }
90}
91
92#[derive(Debug, Clone)]
94pub struct StackingLayerConfig {
95 pub n_estimators: usize,
97 pub meta_strategy: MetaLearningStrategy,
99 pub use_probabilities: bool,
101 pub cv_folds: usize,
103 pub passthrough: bool,
105 pub meta_regularization: Float,
107 pub meta_feature_strategy: MetaFeatureStrategy,
109 pub polynomial_features: bool,
111 pub polynomial_degree: usize,
113}
114
115impl Default for StackingLayerConfig {
116 fn default() -> Self {
117 Self {
118 n_estimators: 5,
119 meta_strategy: MetaLearningStrategy::LinearRegression,
120 use_probabilities: false,
121 cv_folds: 5,
122 passthrough: false,
123 meta_regularization: 0.1,
124 meta_feature_strategy: MetaFeatureStrategy::Raw,
125 polynomial_features: false,
126 polynomial_degree: 2,
127 }
128 }
129}
130
131#[derive(Debug, Clone)]
133pub struct MultiLayerStackingConfig {
134 pub layers: Vec<StackingLayerConfig>,
136 pub random_state: Option<u64>,
138 pub final_meta_strategy: MetaLearningStrategy,
140 pub enable_pruning: bool,
142 pub diversity_threshold: Float,
144 pub confidence_weighting: bool,
146}
147
148impl MultiLayerStackingConfig {
149 pub fn new() -> Self {
150 Self {
151 layers: vec![StackingLayerConfig::default()],
152 random_state: None,
153 final_meta_strategy: MetaLearningStrategy::LogisticRegression,
154 enable_pruning: false,
155 diversity_threshold: 0.1,
156 confidence_weighting: false,
157 }
158 }
159
160 pub fn add_layer(mut self, layer_config: StackingLayerConfig) -> Self {
162 self.layers.push(layer_config);
163 self
164 }
165
166 pub fn final_meta_strategy(mut self, strategy: MetaLearningStrategy) -> Self {
168 self.final_meta_strategy = strategy;
169 self
170 }
171
172 pub fn enable_pruning(mut self, enable: bool) -> Self {
174 self.enable_pruning = enable;
175 self
176 }
177
178 pub fn diversity_threshold(mut self, threshold: Float) -> Self {
180 self.diversity_threshold = threshold;
181 self
182 }
183
184 pub fn confidence_weighting(mut self, enable: bool) -> Self {
186 self.confidence_weighting = enable;
187 self
188 }
189
190 pub fn random_state(mut self, seed: u64) -> Self {
192 self.random_state = Some(seed);
193 self
194 }
195
196 pub fn deep_stacking(n_layers: usize, estimators_per_layer: usize) -> Self {
198 let mut config = Self::new();
199 config.layers.clear();
200
201 for i in 0..n_layers {
202 let layer_config = StackingLayerConfig {
203 n_estimators: estimators_per_layer,
204 meta_strategy: if i == 0 {
205 MetaLearningStrategy::Ridge(0.1)
206 } else {
207 MetaLearningStrategy::ElasticNet(0.1, 0.1)
208 },
209 use_probabilities: i > 0, cv_folds: 5,
211 passthrough: i == 0, meta_regularization: 0.1 * (i + 1) as Float, meta_feature_strategy: if i == 0 {
214 MetaFeatureStrategy::Statistical
215 } else {
216 MetaFeatureStrategy::Comprehensive
217 },
218 polynomial_features: i > 0, polynomial_degree: 2,
220 };
221 config.layers.push(layer_config);
222 }
223
224 config.final_meta_strategy = MetaLearningStrategy::BayesianAveraging;
225 config.enable_pruning = true;
226 config.diversity_threshold = 0.15;
227 config.confidence_weighting = true;
228 config
229 }
230
231 pub fn with_meta_feature_engineering() -> Self {
233 let mut config = Self::new();
234 config.layers[0].meta_feature_strategy = MetaFeatureStrategy::Comprehensive;
235 config.layers[0].polynomial_features = true;
236 config.layers[0].polynomial_degree = 2;
237 config
238 }
239
240 pub fn with_statistical_features() -> Self {
242 let mut config = Self::new();
243 config.layers[0].meta_feature_strategy = MetaFeatureStrategy::Statistical;
244 config.confidence_weighting = true;
245 config
246 }
247
248 pub fn with_interaction_features() -> Self {
250 let mut config = Self::new();
251 config.layers[0].meta_feature_strategy = MetaFeatureStrategy::Interactions;
252 config.layers[0].polynomial_features = true;
253 config.layers[0].polynomial_degree = 3;
254 config
255 }
256
257 pub fn with_diversity_features() -> Self {
259 let mut config = Self::new();
260 config.layers[0].meta_feature_strategy = MetaFeatureStrategy::DiversityBased;
261 config.enable_pruning = true;
262 config.diversity_threshold = 0.1;
263 config
264 }
265}
266
267impl Default for MultiLayerStackingConfig {
268 fn default() -> Self {
269 Self::new()
270 }
271}
272
273pub trait BaseEstimator {}
275pub trait MetaEstimator {}