Skip to content

Estimators

Custom estimators for use with scikit-learn.

class StoredPredictionRegressor(predictions) #

Bases: BaseEstimator, RegressorMixin

A class that just uses precomputed values for regression.

PARAMETER DESCRIPTION
predictions

The precomputed predictions.

TYPE: ndarray

Source code in src/amltk/sklearn/estimators.py
def __init__(self, predictions: np.ndarray):
    """Initialize the estimator.

    Args:
        predictions: The precomputed predictions.
    """
    super().__init__()
    self.predictions = predictions

def fit(*_, **__) #

Fit the estimator. Doesn't do anything.

Source code in src/amltk/sklearn/estimators.py
def fit(self, *_: Any, **__: Any) -> Self:
    """Fit the estimator. Doesn't do anything."""
    return self

def predict(X, *_, **__) #

Predict the target values, returning the precomputed values.

Source code in src/amltk/sklearn/estimators.py
def predict(self, X: Any, *_: Any, **__: Any) -> np.ndarray:  # noqa: N803, ARG002
    """Predict the target values, returning the precomputed values."""
    return self.predictions

class StoredPredictionClassifier(predictions=None, probabilities=None, classes=None) #

Bases: BaseEstimator, ClassifierMixin

A class that just uses precomputed values for classification.

PARAMETER DESCRIPTION
predictions

The precomputed predictions, if any.

TYPE: ndarray | None DEFAULT: None

probabilities

The precomputed probabilities, if any.

TYPE: ndarray | None DEFAULT: None

classes

The classes, if any.

TYPE: list[ndarray] | ndarray | None DEFAULT: None

Source code in src/amltk/sklearn/estimators.py
def __init__(
    self,
    predictions: np.ndarray | None = None,
    probabilities: np.ndarray | None = None,
    classes: list[np.ndarray] | np.ndarray | None = None,
):
    """Initialize the estimator.

    Args:
        predictions: The precomputed predictions, if any.
        probabilities: The precomputed probabilities, if any.
        classes: The classes, if any.
    """
    super().__init__()
    self.predictions = predictions
    self.probabilities = probabilities
    self.classes = classes

    # HACK: This is to enable sklearn-compatibility
    # `clone` and other methods rely on this trailing underscore
    # to indicate fitted attributes. We essentially declare it fitted
    # at init for simplicity
    self.classes_ = classes

def fit(*_, **__) #

Fit the estimator. Doesn't do anything.

Source code in src/amltk/sklearn/estimators.py
def fit(self, *_: Any, **__: Any) -> Self:
    """Fit the estimator. Doesn't do anything."""
    return self

def predict(X, *_, **__) #

Predict the target values, returning the precomputed values.

Source code in src/amltk/sklearn/estimators.py
def predict(self, X: Any, *_: Any, **__: Any) -> np.ndarray:  # noqa: N803, ARG002
    """Predict the target values, returning the precomputed values."""
    if self.predictions is None:
        if self.probabilities is None:
            raise RuntimeError(
                "No predictions or probabilities were provided during",
                " construction, so this estimator cannot be used for",
                " `predict()`.",
            )
        if self.classes_ is None:
            raise RuntimeError(
                "No classes were provided during construction, so it can't"
                " be used for `predict()` from probabilities.",
            )

        predictions = probabilities_to_classes(
            self.probabilities,
            classes=self.classes_,
        )
    else:
        predictions = self.predictions

    return predictions

def predict_proba(X, *_, **__) #

Predict the probabilities, returning the precomputed values.

Source code in src/amltk/sklearn/estimators.py
def predict_proba(
    self,
    X: Any,  # noqa: N803, ARG002
    *_: Any,
    **__: Any,
) -> np.ndarray:
    """Predict the probabilities, returning the precomputed values."""
    if self.probabilities is None:
        raise RuntimeError(
            "No probabilities were provided during construction, so this"
            " estimator cannot be used for `predict_proba()`.",
        )

    return self.probabilities