Core Estimator#
estimator
#
| CLASS | DESCRIPTION |
|---|---|
Estimator |
Base class for non-parametric methods. |
Classes#
Estimator()
#
Bases: Module, ABC
Base class for non-parametric methods.
This abstract base class provides a common interface for non-parametric methods
that follow the scikit-learn pattern of
fit, predict, and score.
All non-parametric methods (those not using neural networks) should inherit from this class.
The class tracks fitting state and provides consistent interfaces for training and
prediction. Subclasses must implement the abstract methods fit, predict,
and score.
| ATTRIBUTE | DESCRIPTION |
|---|---|
in_features |
Number of input features, set during fitting.
TYPE:
|
out_features |
Number of output features, set during fitting.
TYPE:
|
is_fitted |
Whether the estimator has been fitted to data.
TYPE:
|
Examples:
Basic usage example with a custom estimator:
>>> import torch
>>> from spectre.core import Estimator
>>> class MyEstimator(Estimator):
... def fit(self, X, weights=None, target=None):
... self.in_features = X.shape[1]
... self.out_features = X.shape[1] // 2
... self.is_fitted = True
... return self
...
... def predict(self, X):
... self.check_fitted()
... return X[:, : self.out_features]
...
... def score(self, X, weights=None, target=None):
... self.check_fitted()
... return 1.0
>>> estimator = MyEstimator()
>>> X = torch.randn(100, 10)
>>> estimator.fit(X)
MyEstimator(in_features=10, out_features=5, fitted)
>>> preds = estimator.predict(X)
>>> score = estimator.score(X)
>>> print(preds.shape, score)
torch.Size([100, 5]) 1.0
| METHOD | DESCRIPTION |
|---|---|
check_fitted |
Check if the estimator is fitted and raise an error if not. |
fit |
Fit the estimator to the provided data. |
predict |
Make predictions on new data using the fitted estimator. |
transform |
Transform data using the fitted estimator. |
inverse_transform |
Transform data back to original space. |
score |
Evaluate the performance of the fitted estimator. |
fit_predict |
Fit the estimator and immediately make predictions on the same data. |
fit_transform |
Fit the estimator and immediately transform the same data. |
predict_proba |
Predict class probabilities for input data. |
predict_log_proba |
Predict log probabilities for input data. |
get_params |
Get parameters for this estimator. |
set_params |
Set the parameters of this estimator. |
Source code in spectre/core/estimator.py
Attributes#
in_features: int
property
writable
#
Number of input features.
out_features: int
property
writable
#
Number of output features.
is_fitted: bool
property
writable
#
Whether the estimator has been fitted to data.
Functions#
check_fitted() -> None
#
Check if the estimator is fitted and raise an error if not.
This method should be called by :meth:predict and :meth:score methods
to ensure the estimator has been fitted before use.
Source code in spectre/core/estimator.py
fit(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> Estimator
abstractmethod
#
Fit the estimator to the provided data.
This method trains the estimator on the input data, with optional weights
and target values. After fitting, the estimator should be ready for prediction
and the is_fitted property should be set to True.
| 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 learning of shape (n_samples, ).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Estimator
|
Returns self for method chaining. |
Source code in spectre/core/estimator.py
predict(X: torch.Tensor) -> torch.Tensor
abstractmethod
#
Make predictions on new data using the fitted estimator.
This method applies the fitted estimator to new input data to generate predictions. The estimator must be fitted before calling this method.
For dimensionality reduction methods, this is semantically equivalent to
transform. For other methods, this generates actual predictions.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Predicted outputs of shape (n_samples, out_features). |
Source code in spectre/core/estimator.py
transform(X: torch.Tensor) -> torch.Tensor
#
Transform data using the fitted estimator.
This method provides sklearn-compatible interface for dimensionality
reduction and feature extraction. It is an alias for predict
to maintain consistency with scikit-learn's API conventions.
The estimator must be fitted before calling this method.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Transformed outputs of shape (n_samples, out_features). |
Source code in spectre/core/estimator.py
inverse_transform(X: torch.Tensor) -> torch.Tensor
#
Transform data back to original space.
For reversible dimensionality reduction methods (PCA, Linear methods), reconstruct the original data from the transformed representation.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Transformed data of shape (n_samples, out_features).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Reconstructed data of shape (n_samples, in_features). |
| RAISES | DESCRIPTION |
|---|---|
NotImplementedError
|
If the estimator does not support inverse transformation. |
Source code in spectre/core/estimator.py
score(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> torch.Tensor | float
abstractmethod
#
Evaluate the performance of the fitted estimator.
This method computes a performance score for the estimator on the provided data. The exact metric depends on the specific estimator implementation (e.g., likelihood, reconstruction error, etc.).
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples, ).
TYPE:
|
target
|
Target values for evaluation of shape (n_samples, ).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor | float
|
Performance score (higher is typically better). |
Source code in spectre/core/estimator.py
fit_predict(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> torch.Tensor
#
Fit the estimator and immediately make predictions on the same data.
This is a convenience method that combines fit and predict in
a single call. It is equivalent to calling fit followed by predict.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Training and prediction data of shape (n_samples, in_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples, ).
TYPE:
|
target
|
Target values for supervised learning of shape (n_samples, ).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Predicted outputs of shape (n_samples, out_features). |
Source code in spectre/core/estimator.py
fit_transform(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> torch.Tensor
#
Fit the estimator and immediately transform the same data.
This is a convenience method that combines fit and transform
in a single call. It is equivalent to calling fit followed by
transform. This method provides sklearn-compatible interface.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Training and transformation data of shape (n_samples, in_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples, ).
TYPE:
|
target
|
Target values for supervised learning of shape (n_samples, ).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Transformed outputs of shape (n_samples, out_features). |
Source code in spectre/core/estimator.py
predict_proba(X: torch.Tensor) -> torch.Tensor
#
Predict class probabilities for input data.
For classification methods, returns probability estimates for each class. For clustering methods, returns soft assignments.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Class probabilities of shape (n_samples, n_classes) or (n_samples, n_clusters). |
| RAISES | DESCRIPTION |
|---|---|
NotImplementedError
|
If the estimator does not support probabilistic predictions. |
Source code in spectre/core/estimator.py
predict_log_proba(X: torch.Tensor) -> torch.Tensor
#
Predict log probabilities for input data.
Numerically more stable than log(predict_proba(X)) by clamping
probabilities to avoid log(0) which would produce -inf.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Log probabilities of shape (n_samples, n_classes). |
| RAISES | DESCRIPTION |
|---|---|
NotImplementedError
|
If the estimator does not support probabilistic predictions. |
Source code in spectre/core/estimator.py
get_params(deep: bool = True) -> dict[str, Any]
#
Get parameters for this estimator.
| PARAMETER | DESCRIPTION |
|---|---|
deep
|
If True, return parameters for this estimator and contained subobjects that are estimators.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
Parameter names mapped to their values. |
Examples:
>>> from sklearn.model_selection import GridSearchCV
>>> pca = PrincipalComponentAnalysis(out_features=10, standardize=True)
>>> params = pca.get_params() # {'out_features': 10, 'standardize': True}
>>> pca.set_params(out_features=5) # PCA(out_features=5, standardize=True)
Works with sklearn GridSearchCV:
>>> param_grid = {"out_features": [3, 5, 10], "standardize": [True, False]}
>>> grid_search = GridSearchCV(pca, param_grid)
Source code in spectre/core/estimator.py
set_params(**params) -> Estimator
#
Set the parameters of this estimator.
| PARAMETER | DESCRIPTION |
|---|---|
**params
|
Estimator parameters.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Estimator
|
Returns self for method chaining. |