Ensemble Adaptive#
adaptive
#
| CLASS | DESCRIPTION |
|---|---|
AdaptiveWeightedEnsemble |
Ensemble with adaptive weight learning based on performance metrics. |
Attributes#
Classes#
AdaptiveWeightedEnsemble(estimators: list[ModelType] | dict[str, ModelType], weight_method: Literal['cv_score', 'reconstruction', 'stability', 'embedding_quality', 'spectral_quality', 'uniform'] = 'cv_score', n_folds: int = 5, regularization: float = 0.0, eps: float = torch.finfo(torch.float32).eps, fit_manager: FitManager | None = None)
#
Bases: Ensemble
Ensemble with adaptive weight learning based on performance metrics.
Learns optimal estimator weights for combining estimators using various strategies: cross-validation scores, reconstruction quality, stability measures, embedding quality, or spectral quality.
| PARAMETER | DESCRIPTION |
|---|---|
estimators
|
Estimators to combine adaptively. |
weight_method
|
Weight learning method.
TYPE:
|
reduce
|
How to reduce predictions (must be "weighted_mean" for adaptive).
TYPE:
|
n_folds
|
Number of CV folds for
TYPE:
|
regularization
|
L2 regularization on estimator weights (encourages equal weights).
TYPE:
|
eps
|
Small constant to avoid division by zero when normalizing weights.
TYPE:
|
fit_manager
|
Manager for unified training of both Estimator and Parametric models. Required for using Parametric (PyTorch Lightning) models in adaptive ensemble. See FitManager for configuration options.
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
estimators |
Dictionary of fitted estimators.
TYPE:
|
estimator_weights |
Learned optimal weights for each estimator.
TYPE:
|
Examples:
Adaptive ensemble based on cross-validation:
>>> from spectre.decomposition import DiffusionMap, PCA
>>> from spectre.ensemble import AdaptiveWeightedEnsemble
>>>
>>> dm = DiffusionMap(...)
>>> pca = PCA(...)
>>>
>>> adaptive = AdaptiveWeightedEnsemble(
... estimators={"dm": dm, "pca": pca},
... weight_method="cv_score",
... n_folds=5,
... )
>>> adaptive.fit(X)
>>> print(adaptive.estimator_weights) # {'dm': 0.7, 'pca': 0.3}
Stability-based weighting:
>>> adaptive_stable = AdaptiveWeightedEnsemble(
... estimators=[dm1, dm2, dm3],
... weight_method="stability",
... )
>>> adaptive_stable.fit(X)
>>> X_embedded = adaptive_stable.predict(X)
Embedding quality weighting:
>>> adaptive_quality = AdaptiveWeightedEnsemble(
... estimators={"method1": est1, "method2": est2},
... weight_method="embedding_quality",
... )
>>> adaptive_quality.fit(X)
| METHOD | DESCRIPTION |
|---|---|
fit |
Fit all estimators and learn optimal estimator_weights. |
predict |
Predict using weighted combination of estimators. |
score |
Compute weighted score across estimators. |
Source code in spectre/ensemble/adaptive.py
Attributes#
weights: dict[str, float]
property
#
Return learned estimator weights.
Functions#
fit(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> AdaptiveWeightedEnsemble
#
Fit all estimators and learn optimal estimator_weights.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Training data of shape (n_samples, in_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples, ).
TYPE:
|
target
|
Target values for supervised methods.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
AdaptiveWeightedEnsemble
|
Returns self for method chaining. |
Source code in spectre/ensemble/adaptive.py
predict(X: torch.Tensor) -> torch.Tensor
#
Predict using weighted combination of estimators.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Weighted prediction of shape (n_samples, out_features). |
Source code in spectre/ensemble/adaptive.py
score(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> float
#
Compute weighted score across estimators.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data.
TYPE:
|
weights
|
Sample weights.
TYPE:
|
target
|
Target values.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Weighted mean score across estimators. |