Ensemble Base#
base
#
| CLASS | DESCRIPTION |
|---|---|
Ensemble |
Abstract base class for ensemble methods. |
| ATTRIBUTE | DESCRIPTION |
|---|---|
ModelType |
Type alias for models that can be used in ensembles.
|
Attributes#
ModelType = Estimator | Parametric
module-attribute
#
Type alias for models that can be used in ensembles.
A union type representing models that can be included in ensemble methods.
Can be either an Estimator (non-parametric methods
like PCA, diffusion maps) or a Parametric
model (neural network-based methods like autoencoders, spectral maps).
Type
Union[Estimator, Parametric]
Classes#
Ensemble(estimators: list[ModelType] | dict[str, ModelType], fit_manager: FitManager | None = None)
#
Bases: Estimator, ABC
Abstract base class for ensemble methods.
Provides common functionality for combining multiple estimators or parametric models, including model validation, Procrustes alignment, score aggregation, and weighting.
This class provides a unified interface for ensemble learning across different model types:
- Estimator methods (i.e., non-parametric): diffusion maps, PCA, kernel PCA
- Parametric models: autoencoders, spectral maps
| PARAMETER | DESCRIPTION |
|---|---|
estimators
|
Models to create an ensemble from. Can be list for unnamed models or dictionary for named models.
Each model must be of type Parametric or
Estimator ( |
fit_manager
|
Manager for unified training of both Estimator and Parametric models.
If provided, enables ensemble methods to work with PyTorch Lightning-based
Parametric models by handling Trainer creation and configuration.
If None, uses direct
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
estimators |
Dictionary of fitted estimators with names as keys.
TYPE:
|
estimator_names |
List of estimator names in order.
TYPE:
|
is_fitted |
Whether the ensemble has been fitted.
TYPE:
|
Notes
Subclasses must implement:
- Fit ensemble to data:
fit( X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) - Generate ensemble predictions:
predict(X: torch.Tensor) - Compute ensemble score:
score( X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None)
The class automatically handles:
- Procrustes alignment between embeddings
- Model cloning for independent fitting
- Score aggregation across estimators
- Weight normalization
Examples:
Create ensemble with named estimators:
>>> from spectre.decomposition import DiffusionMap, PCA
>>> from spectre.ensemble import EmbeddingEnsemble
>>>
>>> dm = DiffusionMap(...)
>>> pca = PCA(...)
>>>
>>> ensemble = EmbeddingEnsemble(
... estimators={"diffusion": dm, "pca": pca},
... reduce="mean",
... )
>>> ensemble.fit(X)
>>> X_embedded = ensemble.predict(X)
Create ensemble with unnamed estimators:
>>> estimators = [dm1, dm2, dm3]
>>> ensemble = EmbeddingEnsemble(estimators, reduce="median")
>>> ensemble.fit(X)
Access individual estimators:
>>> for name, estimator in ensemble.estimators.items():
... print(f"{name}: {estimator.out_features} features")
Compute scores with different reduction methods:
>>> mean_score = ensemble.reduce_scores(X, reduce="mean")
>>> weighted_score = ensemble.reduce_scores(
... X, reduce="weighted_mean", estimator_weights={"diffusion": 0.7, "pca": 0.3}
... )
| METHOD | DESCRIPTION |
|---|---|
clone_estimator |
Clone an estimator for independent fitting. |
align |
Align embedding to target using Procrustes analysis. |
reduce_scores |
Compute scores from all estimators and reduce them to a scalar. |
predict_ensemble |
Get predictions from all estimators. |
reduce_predictions |
Reduce predictions from multiple estimators. |
normalize_estimator_weights |
Normalize estimator weights to sum to 1. |
get_equal_estimator_weights |
Get equal estimator weights for all estimators. |
fit |
Fit the ensemble to data. |
predict |
Generate ensemble predictions. |
score |
Compute ensemble score. |
Source code in spectre/ensemble/base.py
Attributes#
estimators: dict[str, ModelType]
property
#
Return fitted estimators.
estimator_names: list[str]
property
#
Return list of estimator names.
is_named: bool
property
#
Return whether estimators were provided with names.
Functions#
clone_estimator(estimator: ModelType) -> ModelType
#
align(embedding: torch.Tensor, target: torch.Tensor, scaling: bool = True) -> torch.Tensor
#
Align embedding to target using Procrustes analysis.
See [procrustes][spectre.utils.compute.procrustes] function for detailed documentation.
| PARAMETER | DESCRIPTION |
|---|---|
embedding
|
Source embedding to align, shape (n_samples, n_components).
TYPE:
|
target
|
Target embedding, shape (n_samples, n_components).
TYPE:
|
scaling
|
Whether to apply optimal scaling after rotation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Aligned embedding, shape (n_samples, n_components). |
Source code in spectre/ensemble/base.py
reduce_scores(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None, reduce: str = 'mean', estimator_weights: dict[str, float] | None = None) -> float
#
Compute scores from all estimators and reduce them to a scalar.
Uses each estimator's score or score_step method to compute individual
scores.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples, ).
TYPE:
|
target
|
Target values of shape (n_samples, ).
TYPE:
|
reduce
|
Reduction method: "mean", "median", "weighted_mean", "min", "max".
TYPE:
|
estimator_weights
|
Weights for
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Reduced score across all estimators. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If no estimators have a score method or reduction is unknown. |
Source code in spectre/ensemble/base.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | |
predict_ensemble(X: torch.Tensor) -> dict[str, torch.Tensor]
#
Get predictions from all estimators.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Tensor]
|
Dictionary mapping estimator names to their predictions. |
Source code in spectre/ensemble/base.py
reduce_predictions(predictions: list[torch.Tensor] | dict[str, torch.Tensor], reduce: str = 'mean', estimator_weights: dict[str, float] | None = None) -> torch.Tensor
#
Reduce predictions from multiple estimators.
| PARAMETER | DESCRIPTION |
|---|---|
predictions
|
Predictions to aggregate. If dict, keys are estimator names.
TYPE:
|
reduce
|
Reduction method.
TYPE:
|
estimator_weights
|
Weights for 'weighted_mean' reduce. Keys must match prediction dict keys.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Reduced predictions. |
Source code in spectre/ensemble/base.py
normalize_estimator_weights(estimator_weights: dict[str, float] | list[float]) -> dict[str, float]
#
Normalize estimator weights to sum to 1.
| PARAMETER | DESCRIPTION |
|---|---|
estimator_weights
|
Weights to normalize. If dict, keys must match estimator names. If list, must match number of estimators.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, float]
|
Normalized weights as dictionary. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If weights structure doesn't match estimators. |
Source code in spectre/ensemble/base.py
get_equal_estimator_weights() -> dict[str, float]
#
Get equal estimator weights for all estimators.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, float]
|
Dictionary mapping estimator names to equal weights (1/n_estimators). |