Skip to content

Predictors

AbstractPredictor

Bases: ABC

Abstract base class for all predictors.

Methods

fit(X, Y, kwargs) Fit the model to the data. predict(X, kwargs) Predict using the model. save(file_path) Save the model to a file. load(file_path) Load the model from a file. get_configuration_space(cs) Get the configuration space for the predictor. get_from_configuration(configuration) Get a predictor instance from a configuration.

Source code in asf/predictors/abstract_predictor.py
class AbstractPredictor(ABC):
    """
    Abstract base class for all predictors.

    Methods
    -------
    fit(X, Y, **kwargs)
        Fit the model to the data.
    predict(X, **kwargs)
        Predict using the model.
    save(file_path)
        Save the model to a file.
    load(file_path)
        Load the model from a file.
    get_configuration_space(cs)
        Get the configuration space for the predictor.
    get_from_configuration(configuration)
        Get a predictor instance from a configuration.
    """

    def __init__(self, **kwargs: Any) -> None:
        """
        Initialize the predictor.
        """
        pass

    @abstractmethod
    def fit(self, X: Any, Y: Any, **kwargs: Any) -> None:
        """
        Fit the model to the data.

        Parameters
        ----------
        X : Any
            Training data.
        Y : Any
            Target values.
        kwargs : Any
            Additional arguments for fitting the model.
        """
        pass

    @abstractmethod
    def predict(self, X: Any, **kwargs: Any) -> Any:
        """
        Predict using the model.

        Parameters
        ----------
        X : Any
            Data to predict on.
        kwargs : Any
            Additional arguments for prediction.

        Returns
        -------
        Any
            Predicted values.
        """
        pass

    @abstractmethod
    def save(self, file_path: str) -> None:
        """
        Save the model to a file.

        Parameters
        ----------
        file_path : str
            Path to the file where the model will be saved.
        """
        pass

    @classmethod
    @abstractmethod
    def load(cls, file_path: str) -> AbstractPredictor:
        """
        Load the model from a file.

        Parameters
        ----------
        file_path : str
            Path to the file from which the model will be loaded.

        Returns
        -------
        AbstractPredictor
            The loaded model.
        """
        pass

    @staticmethod
    def get_configuration_space(
        cs: ConfigurationSpace | None = None,
        pre_prefix: str = "",
        parent_param: Hyperparameter | None = None,
        parent_value: Any | None = None,
    ) -> ConfigurationSpace:
        """
        Get the configuration space for the predictor.

        Parameters
        ----------
        cs : ConfigurationSpace or None, default=None
            The configuration space to add the parameters to.
            If None, a new configuration space will be created.
        pre_prefix : str, default=""
            Prefix for all hyperparameters.
        parent_param : Hyperparameter or None, default=None
            Parent hyperparameter for conditions.
        parent_value : Any or None, default=None
            Value of the parent hyperparameter for conditions.

        Returns
        -------
        ConfigurationSpace
            The configuration space for the predictor.

        Raises
        ------
        RuntimeError
            If ConfigSpace is not installed.
        NotImplementedError
            If the method is not implemented for the predictor.
        """
        if not CONFIGSPACE_AVAILABLE:
            raise RuntimeError(
                "ConfigSpace is not installed. Install optional extra with: pip install 'asf[configspace]'"
            )
        raise NotImplementedError(
            "get_configuration_space() is not implemented for this predictor"
        )

    @staticmethod
    def get_from_configuration(
        configuration: dict[str, Any], pre_prefix: str = "", **kwargs: Any
    ) -> AbstractPredictor:
        """
        Get a predictor instance from a configuration.

        Parameters
        ----------
        configuration : dict[str, Any]
            The configuration to create the predictor from.
        pre_prefix : str, default=""
            Prefix used in the configuration.
        **kwargs : Any
            Additional arguments.

        Returns
        -------
        AbstractPredictor
            The predictor instance.

        Raises
        ------
        RuntimeError
            If ConfigSpace is not installed.
        NotImplementedError
            If the method is not implemented for the predictor.
        """
        if not CONFIGSPACE_AVAILABLE:
            raise RuntimeError(
                "ConfigSpace is not installed. Install optional extra with: pip install 'asf[configspace]'"
            )
        raise NotImplementedError(
            "get_from_configuration() is not implemented for this predictor"
        )

__init__(**kwargs)

Initialize the predictor.

Source code in asf/predictors/abstract_predictor.py
def __init__(self, **kwargs: Any) -> None:
    """
    Initialize the predictor.
    """
    pass

fit(X, Y, **kwargs) abstractmethod

Fit the model to the data.

Parameters

X : Any Training data. Y : Any Target values. kwargs : Any Additional arguments for fitting the model.

Source code in asf/predictors/abstract_predictor.py
@abstractmethod
def fit(self, X: Any, Y: Any, **kwargs: Any) -> None:
    """
    Fit the model to the data.

    Parameters
    ----------
    X : Any
        Training data.
    Y : Any
        Target values.
    kwargs : Any
        Additional arguments for fitting the model.
    """
    pass

get_configuration_space(cs=None, pre_prefix='', parent_param=None, parent_value=None) staticmethod

Get the configuration space for the predictor.

Parameters

cs : ConfigurationSpace or None, default=None The configuration space to add the parameters to. If None, a new configuration space will be created. pre_prefix : str, default="" Prefix for all hyperparameters. parent_param : Hyperparameter or None, default=None Parent hyperparameter for conditions. parent_value : Any or None, default=None Value of the parent hyperparameter for conditions.

Returns

ConfigurationSpace The configuration space for the predictor.

Raises

RuntimeError If ConfigSpace is not installed. NotImplementedError If the method is not implemented for the predictor.

Source code in asf/predictors/abstract_predictor.py
@staticmethod
def get_configuration_space(
    cs: ConfigurationSpace | None = None,
    pre_prefix: str = "",
    parent_param: Hyperparameter | None = None,
    parent_value: Any | None = None,
) -> ConfigurationSpace:
    """
    Get the configuration space for the predictor.

    Parameters
    ----------
    cs : ConfigurationSpace or None, default=None
        The configuration space to add the parameters to.
        If None, a new configuration space will be created.
    pre_prefix : str, default=""
        Prefix for all hyperparameters.
    parent_param : Hyperparameter or None, default=None
        Parent hyperparameter for conditions.
    parent_value : Any or None, default=None
        Value of the parent hyperparameter for conditions.

    Returns
    -------
    ConfigurationSpace
        The configuration space for the predictor.

    Raises
    ------
    RuntimeError
        If ConfigSpace is not installed.
    NotImplementedError
        If the method is not implemented for the predictor.
    """
    if not CONFIGSPACE_AVAILABLE:
        raise RuntimeError(
            "ConfigSpace is not installed. Install optional extra with: pip install 'asf[configspace]'"
        )
    raise NotImplementedError(
        "get_configuration_space() is not implemented for this predictor"
    )

get_from_configuration(configuration, pre_prefix='', **kwargs) staticmethod

Get a predictor instance from a configuration.

Parameters

configuration : dict[str, Any] The configuration to create the predictor from. pre_prefix : str, default="" Prefix used in the configuration. **kwargs : Any Additional arguments.

Returns

AbstractPredictor The predictor instance.

Raises

RuntimeError If ConfigSpace is not installed. NotImplementedError If the method is not implemented for the predictor.

Source code in asf/predictors/abstract_predictor.py
@staticmethod
def get_from_configuration(
    configuration: dict[str, Any], pre_prefix: str = "", **kwargs: Any
) -> AbstractPredictor:
    """
    Get a predictor instance from a configuration.

    Parameters
    ----------
    configuration : dict[str, Any]
        The configuration to create the predictor from.
    pre_prefix : str, default=""
        Prefix used in the configuration.
    **kwargs : Any
        Additional arguments.

    Returns
    -------
    AbstractPredictor
        The predictor instance.

    Raises
    ------
    RuntimeError
        If ConfigSpace is not installed.
    NotImplementedError
        If the method is not implemented for the predictor.
    """
    if not CONFIGSPACE_AVAILABLE:
        raise RuntimeError(
            "ConfigSpace is not installed. Install optional extra with: pip install 'asf[configspace]'"
        )
    raise NotImplementedError(
        "get_from_configuration() is not implemented for this predictor"
    )

load(file_path) abstractmethod classmethod

Load the model from a file.

Parameters

file_path : str Path to the file from which the model will be loaded.

Returns

AbstractPredictor The loaded model.

Source code in asf/predictors/abstract_predictor.py
@classmethod
@abstractmethod
def load(cls, file_path: str) -> AbstractPredictor:
    """
    Load the model from a file.

    Parameters
    ----------
    file_path : str
        Path to the file from which the model will be loaded.

    Returns
    -------
    AbstractPredictor
        The loaded model.
    """
    pass

predict(X, **kwargs) abstractmethod

Predict using the model.

Parameters

X : Any Data to predict on. kwargs : Any Additional arguments for prediction.

Returns

Any Predicted values.

Source code in asf/predictors/abstract_predictor.py
@abstractmethod
def predict(self, X: Any, **kwargs: Any) -> Any:
    """
    Predict using the model.

    Parameters
    ----------
    X : Any
        Data to predict on.
    kwargs : Any
        Additional arguments for prediction.

    Returns
    -------
    Any
        Predicted values.
    """
    pass

save(file_path) abstractmethod

Save the model to a file.

Parameters

file_path : str Path to the file where the model will be saved.

Source code in asf/predictors/abstract_predictor.py
@abstractmethod
def save(self, file_path: str) -> None:
    """
    Save the model to a file.

    Parameters
    ----------
    file_path : str
        Path to the file where the model will be saved.
    """
    pass

EPMRandomForest

Bases: ForestRegressor, AbstractPredictor, ConfigurableMixin

Implementation of Random Forest as an Empirical Performance Model (EPM).

This model follows the approach described in the paper: "Algorithm runtime prediction: Methods & evaluation" by Hutter, Xu, Hoos, and Leyton-Brown (2014).

Parameters

n_estimators : int, default=100 The number of trees in the forest. log : bool, default=False Whether to apply logarithmic transformation to the tree values. return_var : bool, default=False Whether to compute variance across trees. criterion : str, default="squared_error" The function to measure the quality of a split. splitter : str, default="random" The strategy used to choose the split at each node. max_depth : int or None, default=None The maximum depth of the tree. min_samples_split : int, default=2 The minimum number of samples required to split an internal node. min_samples_leaf : int, default=1 The minimum number of samples required to be at a leaf node. min_weight_fraction_leaf : float, default=0.0 The minimum weighted fraction of the sum total of weights required to be at a leaf node. max_features : float, default=1.0 The number of features to consider when looking for the best split. max_leaf_nodes : int or None, default=None Grow trees with max_leaf_nodes in best-first fashion. min_impurity_decrease : float, default=0.0 A node will be split if this split induces a decrease of the impurity. bootstrap : bool, default=False Whether bootstrap samples are used when building trees. oob_score : bool, default=False Whether to use out-of-bag samples to estimate the generalization score. n_jobs : int or None, default=None The number of jobs to run in parallel. random_state : int or None, default=None Controls the randomness of the estimator. verbose : int, default=0 Controls the verbosity when fitting and predicting. warm_start : bool, default=False When set to True, reuse the solution of the previous call to fit. ccp_alpha : float, default=0.0 Complexity parameter used for Minimal Cost-Complexity Pruning. max_samples : int, float or None, default=None The number of samples to draw from X to train each base estimator. monotonic_cst : np.ndarray or None, default=None Constraints for monotonicity of features.

Source code in asf/predictors/epm_random_forest.py
class EPMRandomForest(ForestRegressor, AbstractPredictor, ConfigurableMixin):
    """
    Implementation of Random Forest as an Empirical Performance Model (EPM).

    This model follows the approach described in the paper:
    "Algorithm runtime prediction: Methods & evaluation" by Hutter, Xu, Hoos, and Leyton-Brown (2014).

    Parameters
    ----------
    n_estimators : int, default=100
        The number of trees in the forest.
    log : bool, default=False
        Whether to apply logarithmic transformation to the tree values.
    return_var : bool, default=False
        Whether to compute variance across trees.
    criterion : str, default="squared_error"
        The function to measure the quality of a split.
    splitter : str, default="random"
        The strategy used to choose the split at each node.
    max_depth : int or None, default=None
        The maximum depth of the tree.
    min_samples_split : int, default=2
        The minimum number of samples required to split an internal node.
    min_samples_leaf : int, default=1
        The minimum number of samples required to be at a leaf node.
    min_weight_fraction_leaf : float, default=0.0
        The minimum weighted fraction of the sum total of weights required to be at a leaf node.
    max_features : float, default=1.0
        The number of features to consider when looking for the best split.
    max_leaf_nodes : int or None, default=None
        Grow trees with max_leaf_nodes in best-first fashion.
    min_impurity_decrease : float, default=0.0
        A node will be split if this split induces a decrease of the impurity.
    bootstrap : bool, default=False
        Whether bootstrap samples are used when building trees.
    oob_score : bool, default=False
        Whether to use out-of-bag samples to estimate the generalization score.
    n_jobs : int or None, default=None
        The number of jobs to run in parallel.
    random_state : int or None, default=None
        Controls the randomness of the estimator.
    verbose : int, default=0
        Controls the verbosity when fitting and predicting.
    warm_start : bool, default=False
        When set to True, reuse the solution of the previous call to fit.
    ccp_alpha : float, default=0.0
        Complexity parameter used for Minimal Cost-Complexity Pruning.
    max_samples : int, float or None, default=None
        The number of samples to draw from X to train each base estimator.
    monotonic_cst : np.ndarray or None, default=None
        Constraints for monotonicity of features.
    """

    PREFIX: str = "epm_random_forest"

    def __init__(
        self,
        n_estimators: int = 100,
        *,
        log: bool = False,
        return_var: bool = False,
        criterion: str = "squared_error",
        splitter: str = "random",
        max_depth: int | None = None,
        min_samples_split: int = 2,
        min_samples_leaf: int = 1,
        min_weight_fraction_leaf: float = 0.0,
        max_features: float = 1.0,
        max_leaf_nodes: int | None = None,
        min_impurity_decrease: float = 0.0,
        bootstrap: bool = False,
        oob_score: bool = False,
        n_jobs: int | None = None,
        random_state: int | None = None,
        verbose: int = 0,
        warm_start: bool = False,
        ccp_alpha: float = 0.0,
        max_samples: int | float | None = None,
        monotonic_cst: np.ndarray | None = None,
    ) -> None:
        super().__init__(
            DecisionTreeRegressor(),
            n_estimators,
            estimator_params=(
                "criterion",
                "max_depth",
                "min_samples_split",
                "min_samples_leaf",
                "min_weight_fraction_leaf",
                "max_features",
                "max_leaf_nodes",
                "min_impurity_decrease",
                "random_state",
                "ccp_alpha",
                "monotonic_cst",
            ),
            bootstrap=bootstrap,
            oob_score=oob_score,
            n_jobs=n_jobs,
            random_state=random_state,
            verbose=verbose,
            warm_start=warm_start,
            max_samples=max_samples,
        )
        self.criterion = criterion
        self.max_depth = max_depth
        self.min_samples_split = min_samples_split
        self.min_samples_leaf = min_samples_leaf
        self.min_weight_fraction_leaf = min_weight_fraction_leaf
        self.max_features = max_features
        self.max_leaf_nodes = max_leaf_nodes
        self.min_impurity_decrease = min_impurity_decrease
        self.ccp_alpha = ccp_alpha
        self.monotonic_cst = monotonic_cst
        self.splitter = splitter
        self.log = log
        self.return_var = return_var

    @staticmethod
    def _define_hyperparameters(
        **kwargs: Any,
    ) -> tuple[list[Hyperparameter], list[Any], list[Any]]:
        """
        Define hyperparameters for EPMRandomForest.

        Parameters
        ----------
        **kwargs : Any
            Additional keyword arguments.

        Returns
        -------
        tuple
            (hyperparameters, conditions, forbiddens)
        """
        if not CONFIGSPACE_AVAILABLE:
            return [], [], []

        hyperparameters = [
            Integer("n_estimators", (16, 128), log=True, default=100),
            Integer("min_samples_split", (2, 20), log=False, default=2),
            Integer("min_samples_leaf", (1, 20), log=False, default=1),
            Float("max_features", (0.1, 1.0), log=False, default=1.0),
            Categorical("bootstrap", items=[True, False], default=False),
            Categorical("log", items=[True, False], default=False),
        ]
        return hyperparameters, [], []

    @classmethod
    def _get_from_clean_configuration(
        cls,
        clean_config: dict[str, Any],
        **kwargs: Any,
    ) -> partial:
        """
        Create a partial function from a clean (unprefixed) configuration.
        """
        config = clean_config.copy()
        config.update(kwargs)
        return partial(EPMRandomForest, **config)

    def fit(
        self,
        X: np.ndarray,
        Y: np.ndarray,
        sample_weight: np.ndarray | None = None,
        **kwargs: Any,
    ) -> None:
        """
        Fit the model to the data.

        Parameters
        ----------
        X : np.ndarray
            Training data of shape (n_samples, n_features).
        y : np.ndarray
            Target values of shape (n_samples,).
        sample_weight : np.ndarray or None, default=None
            Sample weights. Currently not supported.

        Raises
        ------
        AssertionError
            If sample weights are provided.
        """
        assert sample_weight is None, "Sample weights are not supported"
        super().fit(X=X, y=Y, sample_weight=sample_weight)

        self.trainX = X
        self.trainY = Y
        if self.log:
            for tree, samples_idx in zip(self.estimators_, self.estimators_samples_):
                curX = X[samples_idx]
                curY = Y[samples_idx]
                preds = tree.apply(curX)
                for k in np.unique(preds):
                    tree.tree_.value[k, 0, 0] = np.log(np.exp(curY[preds == k]).mean())

    def predict(
        self, X: np.ndarray, **kwargs: Any
    ) -> np.ndarray | tuple[np.ndarray, np.ndarray]:
        """
        Predict using the model.

        Parameters
        ----------
        X : np.ndarray
            Data to predict on of shape (n_samples, n_features).

        Returns
        -------
        np.ndarray or tuple[np.ndarray, np.ndarray]
            Predicted means, or a tuple of (means, variances) if return_var is True.
        """
        preds = []
        for tree in self.estimators_:
            preds.append(tree.predict(X))
        preds_arr = np.array(preds).T

        means = preds_arr.mean(axis=1)
        vars_arr = preds_arr.var(axis=1)

        if self.return_var:
            return means, vars_arr
        else:
            return means

    def save(self, file_path: str) -> None:
        """
        Save the model to a file.

        Parameters
        ----------
        file_path : str
            Path to the file where the model will be saved.
        """
        joblib.dump(self, file_path)

    @classmethod
    def load(cls, file_path: str) -> EPMRandomForest:
        """
        Load the model from a file.

        Parameters
        ----------
        file_path : str
            Path to the file from which the model will be loaded.

        Returns
        -------
        EPMRandomForest
            The loaded model.
        """
        return joblib.load(file_path)

fit(X, Y, sample_weight=None, **kwargs)

Fit the model to the data.

Parameters

X : np.ndarray Training data of shape (n_samples, n_features). y : np.ndarray Target values of shape (n_samples,). sample_weight : np.ndarray or None, default=None Sample weights. Currently not supported.

Raises

AssertionError If sample weights are provided.

Source code in asf/predictors/epm_random_forest.py
def fit(
    self,
    X: np.ndarray,
    Y: np.ndarray,
    sample_weight: np.ndarray | None = None,
    **kwargs: Any,
) -> None:
    """
    Fit the model to the data.

    Parameters
    ----------
    X : np.ndarray
        Training data of shape (n_samples, n_features).
    y : np.ndarray
        Target values of shape (n_samples,).
    sample_weight : np.ndarray or None, default=None
        Sample weights. Currently not supported.

    Raises
    ------
    AssertionError
        If sample weights are provided.
    """
    assert sample_weight is None, "Sample weights are not supported"
    super().fit(X=X, y=Y, sample_weight=sample_weight)

    self.trainX = X
    self.trainY = Y
    if self.log:
        for tree, samples_idx in zip(self.estimators_, self.estimators_samples_):
            curX = X[samples_idx]
            curY = Y[samples_idx]
            preds = tree.apply(curX)
            for k in np.unique(preds):
                tree.tree_.value[k, 0, 0] = np.log(np.exp(curY[preds == k]).mean())

load(file_path) classmethod

Load the model from a file.

Parameters

file_path : str Path to the file from which the model will be loaded.

Returns

EPMRandomForest The loaded model.

Source code in asf/predictors/epm_random_forest.py
@classmethod
def load(cls, file_path: str) -> EPMRandomForest:
    """
    Load the model from a file.

    Parameters
    ----------
    file_path : str
        Path to the file from which the model will be loaded.

    Returns
    -------
    EPMRandomForest
        The loaded model.
    """
    return joblib.load(file_path)

predict(X, **kwargs)

Predict using the model.

Parameters

X : np.ndarray Data to predict on of shape (n_samples, n_features).

Returns

np.ndarray or tuple[np.ndarray, np.ndarray] Predicted means, or a tuple of (means, variances) if return_var is True.

Source code in asf/predictors/epm_random_forest.py
def predict(
    self, X: np.ndarray, **kwargs: Any
) -> np.ndarray | tuple[np.ndarray, np.ndarray]:
    """
    Predict using the model.

    Parameters
    ----------
    X : np.ndarray
        Data to predict on of shape (n_samples, n_features).

    Returns
    -------
    np.ndarray or tuple[np.ndarray, np.ndarray]
        Predicted means, or a tuple of (means, variances) if return_var is True.
    """
    preds = []
    for tree in self.estimators_:
        preds.append(tree.predict(X))
    preds_arr = np.array(preds).T

    means = preds_arr.mean(axis=1)
    vars_arr = preds_arr.var(axis=1)

    if self.return_var:
        return means, vars_arr
    else:
        return means

save(file_path)

Save the model to a file.

Parameters

file_path : str Path to the file where the model will be saved.

Source code in asf/predictors/epm_random_forest.py
def save(self, file_path: str) -> None:
    """
    Save the model to a file.

    Parameters
    ----------
    file_path : str
        Path to the file where the model will be saved.
    """
    joblib.dump(self, file_path)

LinearClassifierWrapper

Bases: ConfigurableMixin, SklearnWrapper

A wrapper for the SGDClassifier from scikit-learn, providing additional functionality for configuration space generation and parameter extraction.

Source code in asf/predictors/linear_model.py
class LinearClassifierWrapper(ConfigurableMixin, SklearnWrapper):
    """
    A wrapper for the SGDClassifier from scikit-learn, providing additional functionality
    for configuration space generation and parameter extraction.
    """

    PREFIX = "linear_classifier"

    def __init__(self, init_params: dict[str, Any] | None = None):
        """
        Initialize the LinearClassifierWrapper.

        Parameters
        ----------
        init_params : dict, optional
            A dictionary of initialization parameters for the SGDClassifier.
        """
        super().__init__(SGDClassifier, init_params or {})

    @staticmethod
    def _define_hyperparameters(**kwargs):
        """
        Define hyperparameters for the Linear Classifier.
        """
        if not CONFIGSPACE_AVAILABLE:
            return [], [], []

        alpha = Float(
            "alpha",
            (1e-5, 1),
            log=True,
            default=1e-3,
        )
        eta0 = Float(
            "eta0",
            (1e-5, 1),
            log=True,
            default=1e-2,
        )

        params = [
            alpha,
            eta0,
        ]

        return params, [], []

    @classmethod
    def _get_from_clean_configuration(
        cls,
        clean_config: dict[str, Any],
        **kwargs,
    ) -> partial:
        """
        Create a partial function from a clean (unprefixed) configuration.
        """
        linear_classifier_params = {
            "alpha": clean_config["alpha"],
            "eta0": clean_config["eta0"],
            **kwargs,
        }

        return partial(LinearClassifierWrapper, init_params=linear_classifier_params)

__init__(init_params=None)

Initialize the LinearClassifierWrapper.

Parameters

init_params : dict, optional A dictionary of initialization parameters for the SGDClassifier.

Source code in asf/predictors/linear_model.py
def __init__(self, init_params: dict[str, Any] | None = None):
    """
    Initialize the LinearClassifierWrapper.

    Parameters
    ----------
    init_params : dict, optional
        A dictionary of initialization parameters for the SGDClassifier.
    """
    super().__init__(SGDClassifier, init_params or {})

LinearRegressorWrapper

Bases: ConfigurableMixin, SklearnWrapper

A wrapper for the SGDRegressor from scikit-learn, providing additional functionality for configuration space generation and parameter extraction.

Source code in asf/predictors/linear_model.py
class LinearRegressorWrapper(ConfigurableMixin, SklearnWrapper):
    """
    A wrapper for the SGDRegressor from scikit-learn, providing additional functionality
    for configuration space generation and parameter extraction.
    """

    PREFIX = "linear_regressor"

    def __init__(self, init_params: dict[str, Any] | None = None):
        """
        Initialize the LinearRegressorWrapper.

        Parameters
        ----------
        init_params : dict, optional
            A dictionary of initialization parameters for the SGDRegressor.
        """
        super().__init__(SGDRegressor, init_params or {})

    @staticmethod
    def _define_hyperparameters(**kwargs):
        """
        Define hyperparameters for the Linear Regressor.
        """
        if not CONFIGSPACE_AVAILABLE:
            return [], [], []

        alpha = Float(
            "alpha",
            (1e-5, 1),
            log=True,
            default=1e-3,
        )
        eta0 = Float(
            "eta0",
            (1e-5, 1),
            log=True,
            default=1e-2,
        )

        params = [alpha, eta0]
        return params, [], []

    @classmethod
    def _get_from_clean_configuration(
        cls,
        clean_config: dict[str, Any],
        **kwargs,
    ) -> partial:
        """
        Create a partial function from a clean (unprefixed) configuration.
        """
        linear_regressor_params = {
            "alpha": clean_config["alpha"],
            "eta0": clean_config["eta0"],
            **kwargs,
        }

        return partial(LinearRegressorWrapper, init_params=linear_regressor_params)

__init__(init_params=None)

Initialize the LinearRegressorWrapper.

Parameters

init_params : dict, optional A dictionary of initialization parameters for the SGDRegressor.

Source code in asf/predictors/linear_model.py
def __init__(self, init_params: dict[str, Any] | None = None):
    """
    Initialize the LinearRegressorWrapper.

    Parameters
    ----------
    init_params : dict, optional
        A dictionary of initialization parameters for the SGDRegressor.
    """
    super().__init__(SGDRegressor, init_params or {})

MLPClassifierWrapper

Bases: ConfigurableMixin, SklearnWrapper

A wrapper for the MLPClassifier from scikit-learn.

Source code in asf/predictors/mlp.py
class MLPClassifierWrapper(ConfigurableMixin, SklearnWrapper):
    """
    A wrapper for the MLPClassifier from scikit-learn.
    """

    PREFIX: str = "mlp_classifier"

    def __init__(self, init_params: dict[str, Any] | None = None):
        """
        Initialize the MLPClassifierWrapper.

        Parameters
        ----------
        init_params : dict[str, Any] or None, default=None
            Initial parameters for the MLPClassifier.
        """
        super().__init__(MLPClassifier, init_params or {})

    def fit(
        self,
        X: Any,
        Y: Any,
        sample_weight: Any | None = None,
        **kwargs: Any,
    ) -> None:
        """
        Fit the model to the data.

        Parameters
        ----------
        X : array-like
            Training data.
        Y : array-like
            Target values.
        sample_weight : array-like or None, default=None
            Sample weights. Not supported for MLPClassifier.
        **kwargs : Any
            Additional arguments for the fit method.

        Raises
        ------
        AssertionError
            If sample_weight is provided.
        """
        assert sample_weight is None, (
            "Sample weights are not supported for MLPClassifier"
        )
        self.model_class.fit(X, Y, **kwargs)

    @staticmethod
    def _define_hyperparameters(
        **kwargs: Any,
    ) -> tuple[list[Hyperparameter], list[Any], list[Any]]:
        """
        Define hyperparameters for the MLP Classifier.

        Parameters
        ----------
        **kwargs : Any
            Additional keyword arguments.

        Returns
        -------
        tuple
            (hyperparameters, conditions, forbiddens)
        """
        if not CONFIGSPACE_AVAILABLE:
            return [], [], []

        depth = Integer("depth", (1, 3), default=3, log=False)
        width = Integer("width", (16, 1024), default=64, log=True)
        batch_size = Integer(
            "batch_size",
            (256, 1024),
            default=256,
            log=True,
        )
        alpha = Float(
            "alpha",
            (10**-8, 1),
            default=10**-3,
            log=True,
        )
        learning_rate_init = Float(
            "learning_rate_init",
            (10**-5, 1),
            default=10**-3,
            log=True,
        )

        params = [depth, width, batch_size, alpha, learning_rate_init]
        return params, [], []

    @classmethod
    def _get_from_clean_configuration(
        cls,
        clean_config: dict[str, Any],
        **kwargs: Any,
    ) -> partial[MLPClassifierWrapper]:
        """
        Create a partial function from a clean (unprefixed) configuration.
        """
        hidden_layers = [clean_config["width"]] * clean_config["depth"]

        if "activation" not in kwargs:
            kwargs["activation"] = "relu"
        if "solver" not in kwargs:
            kwargs["solver"] = "adam"

        mlp_params = {
            "hidden_layer_sizes": tuple(hidden_layers),
            "batch_size": clean_config["batch_size"],
            "alpha": clean_config["alpha"],
            "learning_rate_init": clean_config["learning_rate_init"],
            **kwargs,
        }

        return partial(MLPClassifierWrapper, init_params=mlp_params)

__init__(init_params=None)

Initialize the MLPClassifierWrapper.

Parameters

init_params : dict[str, Any] or None, default=None Initial parameters for the MLPClassifier.

Source code in asf/predictors/mlp.py
def __init__(self, init_params: dict[str, Any] | None = None):
    """
    Initialize the MLPClassifierWrapper.

    Parameters
    ----------
    init_params : dict[str, Any] or None, default=None
        Initial parameters for the MLPClassifier.
    """
    super().__init__(MLPClassifier, init_params or {})

fit(X, Y, sample_weight=None, **kwargs)

Fit the model to the data.

Parameters

X : array-like Training data. Y : array-like Target values. sample_weight : array-like or None, default=None Sample weights. Not supported for MLPClassifier. **kwargs : Any Additional arguments for the fit method.

Raises

AssertionError If sample_weight is provided.

Source code in asf/predictors/mlp.py
def fit(
    self,
    X: Any,
    Y: Any,
    sample_weight: Any | None = None,
    **kwargs: Any,
) -> None:
    """
    Fit the model to the data.

    Parameters
    ----------
    X : array-like
        Training data.
    Y : array-like
        Target values.
    sample_weight : array-like or None, default=None
        Sample weights. Not supported for MLPClassifier.
    **kwargs : Any
        Additional arguments for the fit method.

    Raises
    ------
    AssertionError
        If sample_weight is provided.
    """
    assert sample_weight is None, (
        "Sample weights are not supported for MLPClassifier"
    )
    self.model_class.fit(X, Y, **kwargs)

MLPRegressorWrapper

Bases: ConfigurableMixin, SklearnWrapper

A wrapper for the MLPRegressor from scikit-learn.

Source code in asf/predictors/mlp.py
class MLPRegressorWrapper(ConfigurableMixin, SklearnWrapper):
    """
    A wrapper for the MLPRegressor from scikit-learn.
    """

    PREFIX: str = "mlp_regressor"

    def __init__(self, init_params: dict[str, Any] | None = None):
        """
        Initialize the MLPRegressorWrapper.

        Parameters
        ----------
        init_params : dict[str, Any] or None, default=None
            Initial parameters for the MLPRegressor.
        """
        super().__init__(MLPRegressor, init_params or {})

    def fit(
        self,
        X: Any,
        Y: Any,
        sample_weight: Any | None = None,
        **kwargs: Any,
    ) -> None:
        """
        Fit the model to the data.

        Parameters
        ----------
        X : array-like
            Training data.
        Y : array-like
            Target values.
        sample_weight : array-like or None, default=None
            Sample weights. Not supported for MLPRegressor.
        **kwargs : Any
            Additional arguments for the fit method.

        Raises
        ------
        AssertionError
            If sample_weight is provided.
        """
        assert sample_weight is None, (
            "Sample weights are not supported for MLPRegressor"
        )
        self.model_class.fit(X, Y, **kwargs)

    @staticmethod
    def _define_hyperparameters(
        dataset_size: str = "large",
        **kwargs: Any,
    ) -> tuple[list[Hyperparameter], list[Any], list[Any]]:
        """
        Define hyperparameters for the MLP Regressor.

        Parameters
        ----------
        dataset_size : str, default="large"
            The size of the dataset ('small', 'medium', or 'large').
        **kwargs : Any
            Additional keyword arguments.

        Returns
        -------
        tuple
            (hyperparameters, conditions, forbiddens)
        """
        if not CONFIGSPACE_AVAILABLE:
            return [], [], []

        depth = Integer("depth", (1, 3), default=3, log=False)
        width = Integer("width", (16, 1024), default=64, log=True)

        if dataset_size == "small":
            batch_size = Integer(
                "batch_size",
                (16, 256),
                default=64,
                log=True,
            )
        elif dataset_size == "medium":
            batch_size = Integer(
                "batch_size",
                (128, 512),
                default=128,
                log=True,
            )
        elif dataset_size == "large":
            batch_size = Integer(
                "batch_size",
                (256, 1024),
                default=256,
                log=True,
            )
        else:
            raise ValueError(
                f"Invalid dataset_size: {dataset_size}. Choose from 'small', 'medium', 'large'."
            )

        alpha = Float(
            "alpha",
            (10**-8, 1),
            default=10**-3,
            log=True,
        )

        learning_rate_init = Float(
            "learning_rate_init",
            (10**-5, 1),
            default=10**-3,
            log=True,
        )

        params = [depth, width, batch_size, alpha, learning_rate_init]
        return params, [], []

    @classmethod
    def _get_from_clean_configuration(
        cls,
        clean_config: dict[str, Any],
        **kwargs: Any,
    ) -> partial[MLPRegressorWrapper]:
        """
        Create a MLPRegressorWrapper partial from a clean configuration.
        """
        hidden_layers = [clean_config["width"]] * clean_config["depth"]

        if "activation" not in kwargs:
            kwargs["activation"] = "relu"
        if "solver" not in kwargs:
            kwargs["solver"] = "adam"

        mlp_params = {
            "hidden_layer_sizes": tuple(hidden_layers),
            "batch_size": clean_config["batch_size"],
            "alpha": clean_config["alpha"],
            "learning_rate_init": clean_config["learning_rate_init"],
            **kwargs,
        }

        return partial(MLPRegressorWrapper, init_params=mlp_params)

__init__(init_params=None)

Initialize the MLPRegressorWrapper.

Parameters

init_params : dict[str, Any] or None, default=None Initial parameters for the MLPRegressor.

Source code in asf/predictors/mlp.py
def __init__(self, init_params: dict[str, Any] | None = None):
    """
    Initialize the MLPRegressorWrapper.

    Parameters
    ----------
    init_params : dict[str, Any] or None, default=None
        Initial parameters for the MLPRegressor.
    """
    super().__init__(MLPRegressor, init_params or {})

fit(X, Y, sample_weight=None, **kwargs)

Fit the model to the data.

Parameters

X : array-like Training data. Y : array-like Target values. sample_weight : array-like or None, default=None Sample weights. Not supported for MLPRegressor. **kwargs : Any Additional arguments for the fit method.

Raises

AssertionError If sample_weight is provided.

Source code in asf/predictors/mlp.py
def fit(
    self,
    X: Any,
    Y: Any,
    sample_weight: Any | None = None,
    **kwargs: Any,
) -> None:
    """
    Fit the model to the data.

    Parameters
    ----------
    X : array-like
        Training data.
    Y : array-like
        Target values.
    sample_weight : array-like or None, default=None
        Sample weights. Not supported for MLPRegressor.
    **kwargs : Any
        Additional arguments for the fit method.

    Raises
    ------
    AssertionError
        If sample_weight is provided.
    """
    assert sample_weight is None, (
        "Sample weights are not supported for MLPRegressor"
    )
    self.model_class.fit(X, Y, **kwargs)

RandomForestClassifierWrapper

Bases: ConfigurableMixin, SklearnWrapper

A wrapper for the RandomForestClassifier from scikit-learn.

Source code in asf/predictors/random_forest.py
class RandomForestClassifierWrapper(ConfigurableMixin, SklearnWrapper):
    """
    A wrapper for the RandomForestClassifier from scikit-learn.
    """

    PREFIX: str = "rf_classifier"

    def __init__(self, init_params: dict[str, Any] | None = None):
        """
        Initialize the RandomForestClassifierWrapper.

        Parameters
        ----------
        init_params : dict[str, Any] or None, default=None
            A dictionary of initialization parameters for the RandomForestClassifier.
        """
        super().__init__(RandomForestClassifier, init_params or {})

    @staticmethod
    def _define_hyperparameters(
        **kwargs: Any,
    ) -> tuple[list[Hyperparameter], list[Any], list[Any]]:
        """
        Define hyperparameters for RandomForestClassifier.

        Parameters
        ----------
        **kwargs : Any
            Additional keyword arguments.

        Returns
        -------
        tuple
            (hyperparameters, conditions, forbiddens)
        """
        if not CONFIGSPACE_AVAILABLE:
            return [], [], []

        hyperparameters = [
            Integer("n_estimators", (16, 128), log=True, default=116),
            Integer("min_samples_split", (2, 20), log=False, default=2),
            Integer("min_samples_leaf", (1, 20), log=False, default=2),
            Float("max_features", (0.1, 1.0), log=False, default=0.17055852159745608),
            Categorical("bootstrap", items=[True, False], default=False),
        ]
        return hyperparameters, [], []

    @classmethod
    def _get_from_clean_configuration(
        cls,
        clean_config: dict[str, Any],
        **kwargs: Any,
    ) -> partial[RandomForestClassifierWrapper]:
        """
        Create a partial function from a clean (unprefixed) configuration.

        Parameters
        ----------
        clean_config : dict[str, Any]
            The clean configuration dictionary.
        **kwargs : Any
            Additional arguments.

        Returns
        -------
        partial
            A partial function for instantiating the wrapper.
        """
        rf_params = clean_config.copy()
        rf_params.update(kwargs)
        return partial(RandomForestClassifierWrapper, init_params=rf_params)

__init__(init_params=None)

Initialize the RandomForestClassifierWrapper.

Parameters

init_params : dict[str, Any] or None, default=None A dictionary of initialization parameters for the RandomForestClassifier.

Source code in asf/predictors/random_forest.py
def __init__(self, init_params: dict[str, Any] | None = None):
    """
    Initialize the RandomForestClassifierWrapper.

    Parameters
    ----------
    init_params : dict[str, Any] or None, default=None
        A dictionary of initialization parameters for the RandomForestClassifier.
    """
    super().__init__(RandomForestClassifier, init_params or {})

RandomForestRegressorWrapper

Bases: ConfigurableMixin, SklearnWrapper

A wrapper for the RandomForestRegressor from scikit-learn.

Source code in asf/predictors/random_forest.py
class RandomForestRegressorWrapper(ConfigurableMixin, SklearnWrapper):
    """
    A wrapper for the RandomForestRegressor from scikit-learn.
    """

    PREFIX: str = "rf_regressor"

    def __init__(self, init_params: dict[str, Any] | None = None):
        """
        Initialize the RandomForestRegressorWrapper.

        Parameters
        ----------
        init_params : dict[str, Any] or None, default=None
            A dictionary of initialization parameters for the RandomForestRegressor.
        """
        super().__init__(RandomForestRegressor, init_params or {})

    @staticmethod
    def _define_hyperparameters(
        **kwargs: Any,
    ) -> tuple[list[Hyperparameter], list[Any], list[Any]]:
        """
        Define hyperparameters for RandomForestRegressor.

        Parameters
        ----------
        **kwargs : Any
            Additional keyword arguments.

        Returns
        -------
        tuple
            (hyperparameters, conditions, forbiddens)
        """
        if not CONFIGSPACE_AVAILABLE:
            return [], [], []

        hyperparameters = [
            Integer("n_estimators", (16, 128), log=True, default=116),
            Integer("min_samples_split", (2, 20), log=False, default=2),
            Integer("min_samples_leaf", (1, 20), log=False, default=2),
            Float("max_features", (0.1, 1.0), log=False, default=0.17055852159745608),
            Categorical("bootstrap", items=[True, False], default=False),
        ]
        return hyperparameters, [], []

    @classmethod
    def _get_from_clean_configuration(
        cls,
        clean_config: dict[str, Any],
        **kwargs: Any,
    ) -> partial[RandomForestRegressorWrapper]:
        """
        Create a partial function from a clean (unprefixed) configuration.

        Parameters
        ----------
        clean_config : dict[str, Any]
            The clean configuration dictionary.
        **kwargs : Any
            Additional arguments.

        Returns
        -------
        partial
            A partial function for instantiating the wrapper.
        """
        rf_params = clean_config.copy()
        rf_params.update(kwargs)
        return partial(RandomForestRegressorWrapper, init_params=rf_params)

__init__(init_params=None)

Initialize the RandomForestRegressorWrapper.

Parameters

init_params : dict[str, Any] or None, default=None A dictionary of initialization parameters for the RandomForestRegressor.

Source code in asf/predictors/random_forest.py
def __init__(self, init_params: dict[str, Any] | None = None):
    """
    Initialize the RandomForestRegressorWrapper.

    Parameters
    ----------
    init_params : dict[str, Any] or None, default=None
        A dictionary of initialization parameters for the RandomForestRegressor.
    """
    super().__init__(RandomForestRegressor, init_params or {})

RandomSurvivalForestWrapper

Bases: ConfigurableMixin, AbstractPredictor

Lightweight wrapper around sksurv's RandomSurvivalForest model.

Source code in asf/predictors/survival.py
class RandomSurvivalForestWrapper(ConfigurableMixin, AbstractPredictor):
    """
    Lightweight wrapper around ``sksurv``'s ``RandomSurvivalForest`` model.
    """

    PREFIX: str = "random_survival_forest"

    def __init__(self, init_params: dict[str, Any] | None = None) -> None:
        """
        Initialize the RandomSurvivalForestWrapper.

        Parameters
        ----------
        init_params : dict[str, Any] or None, default=None
            Initial parameters for the RandomSurvivalForest model.

        Raises
        ------
        ImportError
            If sksurv is not installed.
        """
        if not SKSURV_AVAILABLE:
            raise ImportError(
                "sksurv is not installed. Install scikit-survival to use RandomSurvivalForestWrapper."
            )
        params = init_params or {}
        self.model = RandomSurvivalForest(**params)

    @staticmethod
    def _define_hyperparameters(
        **kwargs: Any,
    ) -> tuple[list[Hyperparameter], list[Any], list[Any]]:
        """
        Define hyperparameters for RandomSurvivalForestWrapper.

        Parameters
        ----------
        **kwargs : Any
            Additional keyword arguments.

        Returns
        -------
        tuple
            (hyperparameters, conditions, forbiddens)
        """
        if not CONFIGSPACE_AVAILABLE:
            return [], [], []

        hyperparameters = [
            Integer("n_estimators", (10, 1000), log=True, default=100),
            Integer("min_samples_split", (2, 20), default=6),
            Integer("min_samples_leaf", (1, 20), default=3),
            Float("max_features", (0.1, 1.0), default=1.0),
            Categorical("bootstrap", items=[True, False], default=True),
        ]
        return hyperparameters, [], []

    @classmethod
    def _get_from_clean_configuration(
        cls,
        clean_config: dict[str, Any],
        **kwargs: Any,
    ) -> partial:
        """
        Create a partial function from a clean (unprefixed) configuration.
        """
        config = clean_config.copy()
        config.update(kwargs)
        return partial(cls, init_params=config)

    def fit(self, X: Any, Y: Any, **kwargs: Any) -> None:
        """
        Fit the model to the data.

        Parameters
        ----------
        X : Any
            Training data.
        y : Any
            Target values.
        **kwargs : Any
            Additional arguments for the fit method.
        """
        self.model.fit(X, Y, **kwargs)

    def predict(self, X: Any, **kwargs: Any) -> Any:
        """
        Predict using the model.

        Parameters
        ----------
        X : Any
            Data to predict on.
        **kwargs : Any
            Additional arguments for the predict method.

        Returns
        -------
        Any
            Predicted values.
        """
        return self.model.predict(X, **kwargs)

    def predict_survival_function(self, X: Any, **kwargs: Any) -> Any:
        """
        Predict survival function.
        """
        return self.model.predict_survival_function(X, **kwargs)

    def save(self, file_path: str) -> None:
        """
        Save the model to a file.
        """
        joblib.dump(self, file_path)

    @classmethod
    def load(cls, file_path: str) -> AbstractPredictor:
        """
        Load the model from a file.
        """
        return joblib.load(file_path)

__init__(init_params=None)

Initialize the RandomSurvivalForestWrapper.

Parameters

init_params : dict[str, Any] or None, default=None Initial parameters for the RandomSurvivalForest model.

Raises

ImportError If sksurv is not installed.

Source code in asf/predictors/survival.py
def __init__(self, init_params: dict[str, Any] | None = None) -> None:
    """
    Initialize the RandomSurvivalForestWrapper.

    Parameters
    ----------
    init_params : dict[str, Any] or None, default=None
        Initial parameters for the RandomSurvivalForest model.

    Raises
    ------
    ImportError
        If sksurv is not installed.
    """
    if not SKSURV_AVAILABLE:
        raise ImportError(
            "sksurv is not installed. Install scikit-survival to use RandomSurvivalForestWrapper."
        )
    params = init_params or {}
    self.model = RandomSurvivalForest(**params)

fit(X, Y, **kwargs)

Fit the model to the data.

Parameters

X : Any Training data. y : Any Target values. **kwargs : Any Additional arguments for the fit method.

Source code in asf/predictors/survival.py
def fit(self, X: Any, Y: Any, **kwargs: Any) -> None:
    """
    Fit the model to the data.

    Parameters
    ----------
    X : Any
        Training data.
    y : Any
        Target values.
    **kwargs : Any
        Additional arguments for the fit method.
    """
    self.model.fit(X, Y, **kwargs)

load(file_path) classmethod

Load the model from a file.

Source code in asf/predictors/survival.py
@classmethod
def load(cls, file_path: str) -> AbstractPredictor:
    """
    Load the model from a file.
    """
    return joblib.load(file_path)

predict(X, **kwargs)

Predict using the model.

Parameters

X : Any Data to predict on. **kwargs : Any Additional arguments for the predict method.

Returns

Any Predicted values.

Source code in asf/predictors/survival.py
def predict(self, X: Any, **kwargs: Any) -> Any:
    """
    Predict using the model.

    Parameters
    ----------
    X : Any
        Data to predict on.
    **kwargs : Any
        Additional arguments for the predict method.

    Returns
    -------
    Any
        Predicted values.
    """
    return self.model.predict(X, **kwargs)

predict_survival_function(X, **kwargs)

Predict survival function.

Source code in asf/predictors/survival.py
def predict_survival_function(self, X: Any, **kwargs: Any) -> Any:
    """
    Predict survival function.
    """
    return self.model.predict_survival_function(X, **kwargs)

save(file_path)

Save the model to a file.

Source code in asf/predictors/survival.py
def save(self, file_path: str) -> None:
    """
    Save the model to a file.
    """
    joblib.dump(self, file_path)

RankingMLP

Bases: ConfigurableMixin, AbstractPredictor

A ranking-based predictor using a Multi-Layer Percetron (MLP).

This class implements a ranking model that uses an MLP to predict the performance of algorithms based on input features.

Source code in asf/predictors/ranking_mlp.py
class RankingMLP(ConfigurableMixin, AbstractPredictor):
    """
    A ranking-based predictor using a Multi-Layer Percetron (MLP).

    This class implements a ranking model that uses an MLP to predict
    the performance of algorithms based on input features.
    """

    def __init__(
        self,
        model: Any | None = None,
        input_size: int | None = None,
        loss: Callable | None = None,
        optimizer: Callable[..., Any] | None = None,
        batch_size: int = 128,
        epochs: int = 500,
        seed: int = 42,
        device: str = "cpu",
        compile: bool = True,
        learning_rate: float = 1e-3,
        weight_decay: float = 0.0,
        **kwargs,
    ):
        super().__init__(**kwargs)
        if not TORCH_AVAILABLE:
            raise RuntimeError(
                "PyTorch is not installed. Install it with: pip install torch"
            )

        assert model is not None or input_size is not None, (
            "Either model or input_size must be provided."
        )

        torch.manual_seed(seed)

        if model is None:
            assert input_size is not None
            self.model = get_mlp(input_size=input_size, output_size=1)
        else:
            self.model = model

        self.model.to(device)
        self.device = device

        self.loss = loss or bpr_loss
        self.batch_size = batch_size
        self.optimizer = optimizer or torch.optim.Adam
        self.epochs = epochs
        self.learning_rate = learning_rate
        self.weight_decay = weight_decay

        if compile:
            self.model = torch.compile(self.model)

    def _get_dataloader(
        self,
        features: pd.DataFrame,
        performance: pd.DataFrame,
        algorithm_features: pd.DataFrame,
    ) -> Any:
        dataset = RankingDataset(features, performance, algorithm_features)
        return torch.utils.data.DataLoader(
            dataset, batch_size=self.batch_size, shuffle=True, num_workers=4
        )

    def fit(
        self,
        X: Any,
        Y: Any,
        **kwargs: Any,
    ) -> None:
        # Extract algorithm_features from kwargs
        algorithm_features = kwargs.get("algorithm_features")
        if algorithm_features is None:
            raise ValueError(
                "algorithm_features must be provided in kwargs for RankingMLP.fit"
            )

        dataloader = self._get_dataloader(X, Y, algorithm_features)

        optimizer = self.optimizer(
            self.model.parameters(),
            lr=self.learning_rate,
            weight_decay=self.weight_decay,
        )
        self.model.train()
        for epoch in range(self.epochs):
            total_loss = 0
            for i, ((Xc, Xs, Xl), (yc, ys, yl)) in enumerate(dataloader):
                Xc, Xs, Xl = (
                    Xc.to(self.device),
                    Xs.to(self.device),
                    Xl.to(self.device),
                )
                yc, ys, yl = (
                    yc.to(self.device),
                    ys.to(self.device),
                    yl.to(self.device),
                )

                yc = yc.float().unsqueeze(1)
                ys = ys.float().unsqueeze(1)
                yl = yl.float().unsqueeze(1)

                optimizer.zero_grad()

                y_pred = self.model(Xc)
                y_pred_s = self.model(Xs)
                y_pred_l = self.model(Xl)

                loss = self.loss(y_pred, y_pred_s, y_pred_l, yc, ys, yl)
                total_loss += loss.item()

                loss.backward()
                optimizer.step()

            logging.debug(f"Epoch {epoch}, Loss: {total_loss / len(dataloader)}")

        return None

    def predict(self, X: pd.DataFrame, **kwargs: Any) -> pd.DataFrame:
        self.model.eval()

        features_tensor = torch.from_numpy(X.values).to(self.device).float()
        predictions = self.model(features_tensor).detach().numpy()

        return predictions

    def save(self, file_path: str) -> None:
        torch.save(self, file_path)

    @classmethod
    def load(cls, file_path: str) -> AbstractPredictor:
        return torch.load(file_path)

    PREFIX = "ranking_mlp"

    @staticmethod
    def _define_hyperparameters(**kwargs):
        """Define hyperparameters for RankingMLP."""
        if not CONFIGSPACE_AVAILABLE:
            return [], [], []

        hyperparameters = [
            Integer("batch_size", (32, 256), log=True, default=128),
            Integer("epochs", (50, 1000), log=True, default=500),
            Float("learning_rate", (1e-4, 1e-1), log=True, default=1e-3),
            Float("weight_decay", (1e-6, 1e-2), log=True, default=1e-5),
        ]
        return hyperparameters, [], []

    @classmethod
    def _get_from_clean_configuration(
        cls,
        clean_config: dict[str, Any],
        **kwargs,
    ) -> partial:
        """
        Create a partial function from a clean (unprefixed) configuration.
        """
        config = clean_config.copy()
        config.update(kwargs)
        return partial(RankingMLP, **config)

RegressionMLP

Bases: AbstractPredictor, ConfigurableMixin

A regression-based predictor using a Multi-Layer Perceptron (MLP).

Source code in asf/predictors/regression_mlp.py
class RegressionMLP(AbstractPredictor, ConfigurableMixin):
    """
    A regression-based predictor using a Multi-Layer Perceptron (MLP).
    """

    PREFIX: str = "regression_mlp"

    def __init__(
        self,
        model: torch.nn.Module | None = None,
        loss: torch.nn.modules.loss._Loss | None = None,
        optimizer: type[torch.optim.Optimizer] | None = None,
        batch_size: int = 128,
        epochs: int = 2000,
        seed: int = 42,
        device: str = "cpu",
        compile_model: bool = True,
        learning_rate: float = 1e-3,
        weight_decay: float = 0.0,
        **kwargs: Any,
    ):
        super().__init__(**kwargs)
        if not TORCH_AVAILABLE:
            raise RuntimeError(
                "PyTorch is not installed. Install it with: pip install torch"
            )

        torch.manual_seed(seed)

        self.model = model
        self.device = device
        self.loss = loss or torch.nn.MSELoss()
        self.batch_size = batch_size
        self.optimizer = optimizer or torch.optim.Adam
        self.epochs = epochs
        self.compile_model = compile_model
        self.learning_rate = learning_rate
        self.weight_decay = weight_decay

    def _get_dataloader(
        self,
        features: pd.DataFrame,
        performance: pd.DataFrame,
    ) -> torch.utils.data.DataLoader:
        dataset = RegressionDataset(features, performance)
        return torch.utils.data.DataLoader(
            dataset, batch_size=self.batch_size, shuffle=True
        )

    def fit(
        self,
        X: Any,
        Y: Any,
        **kwargs: Any,
    ) -> None:
        """
        Fit the model to the data.

        Parameters
        ----------
        X : pd.DataFrame
            The features for each instance.
        Y : pd.DataFrame
            The performance of each algorithm on each instance.
        sample_weight : np.ndarray or None, default=None
            Sample weights. Currently not supported.
        **kwargs : Any
            Additional arguments.

        Returns
        -------
        RegressionMLP
            The fitted model.

        Raises
        ------
        AssertionError
            If sample_weight is provided.
        """
        sample_weight = kwargs.get("sample_weight")
        assert sample_weight is None, "Sample weights are not supported."

        if self.model is None:
            input_size = X.shape[1] if hasattr(X, "shape") else len(X.columns)
            self.model = get_mlp(input_size=input_size, output_size=1)

        self.model.to(self.device)  # type: ignore[attr-defined]

        if self.compile_model:
            self.model = torch.compile(self.model)

        features_imputed = pd.DataFrame(
            SimpleImputer().fit_transform(X.values),
            index=X.index,
            columns=X.columns,
        )
        dataloader = self._get_dataloader(features_imputed, Y)

        optimizer = self.optimizer(
            self.model.parameters(),  # type: ignore[attr-defined]
            lr=self.learning_rate,
            weight_decay=self.weight_decay,
        )
        self.model.train()  # type: ignore[attr-defined]
        for epoch in range(self.epochs):
            total_loss = 0.0
            for i, (X_batch, y_batch) in enumerate(dataloader):
                X_batch, y_batch = X_batch.to(self.device), y_batch.to(self.device)
                X_batch = X_batch.float()
                y_batch = y_batch.unsqueeze(-1).float()
                optimizer.zero_grad()
                y_pred = self.model(X_batch)
                loss = self.loss(y_pred, y_batch)
                total_loss += loss.item()
                loss.backward()
                optimizer.step()

        return None

    def predict(self, X: pd.DataFrame, **kwargs: Any) -> np.ndarray:
        """
        Predict using the model.

        Parameters
        ----------
        X : pd.DataFrame
            The features to predict on.
        **kwargs : Any
            Additional arguments.

        Returns
        -------
        np.ndarray
            The predicted values.
        """
        if self.model is None:
            raise RuntimeError("Model not fitted")
        self.model.eval()  # type: ignore[attr-defined]

        features_tensor = torch.from_numpy(X.values).to(self.device).float()
        predictions = self.model(features_tensor).detach().cpu().numpy().squeeze(1)

        return predictions

    def save(self, file_path: str) -> None:
        """
        Save the model to a file.
        """
        torch.save(self, file_path)

    @classmethod
    def load(cls, file_path: str) -> AbstractPredictor:
        """
        Load the model from a file.
        """
        return torch.load(file_path)

    @staticmethod
    def _define_hyperparameters(
        **kwargs: Any,
    ) -> tuple[list[Hyperparameter], list[Any], list[Any]]:
        """
        Define hyperparameters for RegressionMLP.
        """
        if not CONFIGSPACE_AVAILABLE:
            return [], [], []

        hyperparameters = [
            Integer("batch_size", (32, 256), log=True, default=128),
            Integer("epochs", (200, 2000), log=True, default=500),
            Float("learning_rate", (1e-4, 1e-1), log=True, default=1e-3),
            Float("weight_decay", (1e-6, 1e-2), log=True, default=1e-5),
        ]
        return hyperparameters, [], []

    @classmethod
    def _get_from_clean_configuration(
        cls,
        clean_config: dict[str, Any],
        **kwargs: Any,
    ) -> partial[RegressionMLP]:
        """
        Create a partial function from a clean (unprefixed) configuration.
        """
        config = clean_config.copy()
        config.update(kwargs)
        return partial(RegressionMLP, **config)

fit(X, Y, **kwargs)

Fit the model to the data.

Parameters

X : pd.DataFrame The features for each instance. Y : pd.DataFrame The performance of each algorithm on each instance. sample_weight : np.ndarray or None, default=None Sample weights. Currently not supported. **kwargs : Any Additional arguments.

Returns

RegressionMLP The fitted model.

Raises

AssertionError If sample_weight is provided.

Source code in asf/predictors/regression_mlp.py
def fit(
    self,
    X: Any,
    Y: Any,
    **kwargs: Any,
) -> None:
    """
    Fit the model to the data.

    Parameters
    ----------
    X : pd.DataFrame
        The features for each instance.
    Y : pd.DataFrame
        The performance of each algorithm on each instance.
    sample_weight : np.ndarray or None, default=None
        Sample weights. Currently not supported.
    **kwargs : Any
        Additional arguments.

    Returns
    -------
    RegressionMLP
        The fitted model.

    Raises
    ------
    AssertionError
        If sample_weight is provided.
    """
    sample_weight = kwargs.get("sample_weight")
    assert sample_weight is None, "Sample weights are not supported."

    if self.model is None:
        input_size = X.shape[1] if hasattr(X, "shape") else len(X.columns)
        self.model = get_mlp(input_size=input_size, output_size=1)

    self.model.to(self.device)  # type: ignore[attr-defined]

    if self.compile_model:
        self.model = torch.compile(self.model)

    features_imputed = pd.DataFrame(
        SimpleImputer().fit_transform(X.values),
        index=X.index,
        columns=X.columns,
    )
    dataloader = self._get_dataloader(features_imputed, Y)

    optimizer = self.optimizer(
        self.model.parameters(),  # type: ignore[attr-defined]
        lr=self.learning_rate,
        weight_decay=self.weight_decay,
    )
    self.model.train()  # type: ignore[attr-defined]
    for epoch in range(self.epochs):
        total_loss = 0.0
        for i, (X_batch, y_batch) in enumerate(dataloader):
            X_batch, y_batch = X_batch.to(self.device), y_batch.to(self.device)
            X_batch = X_batch.float()
            y_batch = y_batch.unsqueeze(-1).float()
            optimizer.zero_grad()
            y_pred = self.model(X_batch)
            loss = self.loss(y_pred, y_batch)
            total_loss += loss.item()
            loss.backward()
            optimizer.step()

    return None

load(file_path) classmethod

Load the model from a file.

Source code in asf/predictors/regression_mlp.py
@classmethod
def load(cls, file_path: str) -> AbstractPredictor:
    """
    Load the model from a file.
    """
    return torch.load(file_path)

predict(X, **kwargs)

Predict using the model.

Parameters

X : pd.DataFrame The features to predict on. **kwargs : Any Additional arguments.

Returns

np.ndarray The predicted values.

Source code in asf/predictors/regression_mlp.py
def predict(self, X: pd.DataFrame, **kwargs: Any) -> np.ndarray:
    """
    Predict using the model.

    Parameters
    ----------
    X : pd.DataFrame
        The features to predict on.
    **kwargs : Any
        Additional arguments.

    Returns
    -------
    np.ndarray
        The predicted values.
    """
    if self.model is None:
        raise RuntimeError("Model not fitted")
    self.model.eval()  # type: ignore[attr-defined]

    features_tensor = torch.from_numpy(X.values).to(self.device).float()
    predictions = self.model(features_tensor).detach().cpu().numpy().squeeze(1)

    return predictions

save(file_path)

Save the model to a file.

Source code in asf/predictors/regression_mlp.py
def save(self, file_path: str) -> None:
    """
    Save the model to a file.
    """
    torch.save(self, file_path)

RidgeRegressorWrapper

Bases: ConfigurableMixin, SklearnWrapper

Wrapper around scikit-learn's Ridge regressor for ASF predictors.

Source code in asf/predictors/linear_model.py
class RidgeRegressorWrapper(ConfigurableMixin, SklearnWrapper):
    """Wrapper around scikit-learn's Ridge regressor for ASF predictors."""

    PREFIX = "ridge_regressor"

    def __init__(self, init_params: dict[str, Any] = {}):
        super().__init__(Ridge, init_params)

    @staticmethod
    def _define_hyperparameters(**kwargs):
        """
        Define hyperparameters for the Ridge Regressor.
        """
        if not CONFIGSPACE_AVAILABLE:
            return [], [], []

        alpha = Float(
            "alpha",
            (1e-6, 100.0),
            log=True,
            default=1.0,
        )
        fit_intercept = Categorical(
            "fit_intercept",
            [True, False],
            default=True,
        )
        solver = Categorical(
            "solver",
            ["auto", "svd", "cholesky", "lsqr", "sparse_cg", "sag", "saga"],
            default="auto",
        )

        params = [alpha, fit_intercept, solver]
        return params, [], []

    @classmethod
    def _get_from_clean_configuration(
        cls,
        clean_config: dict[str, Any],
        **kwargs,
    ) -> partial:
        """
        Create a partial function from a clean (unprefixed) configuration.
        """
        params = {
            "alpha": clean_config["alpha"],
            "fit_intercept": clean_config["fit_intercept"],
            "solver": clean_config["solver"],
            **kwargs,
        }
        return partial(RidgeRegressorWrapper, init_params=params)

SVMClassifierWrapper

Bases: ConfigurableMixin, SklearnWrapper

A wrapper for the Scikit-learn SVC (Support Vector Classifier) model. Provides methods to define a configuration space and create an instance of the classifier from a configuration.

Attributes

PREFIX : str Prefix used for parameter names in the configuration space.

Source code in asf/predictors/svm.py
class SVMClassifierWrapper(ConfigurableMixin, SklearnWrapper):
    """
    A wrapper for the Scikit-learn SVC (Support Vector Classifier) model.
    Provides methods to define a configuration space and create an instance
    of the classifier from a configuration.

    Attributes
    ----------
    PREFIX : str
        Prefix used for parameter names in the configuration space.
    """

    PREFIX = "svm_classifier"

    def __init__(self, init_params: dict[str, Any] = {}):
        """
        Initialize the SVMClassifierWrapper.

        Parameters
        ----------
        init_params : dict, optional
            Dictionary of parameters to initialize the SVC model.
        """
        super().__init__(SVC, init_params)

    @classmethod
    def get_configuration_space(
        cls,
        cs: ConfigurationSpace | None = None,
        pre_prefix: str = "",
        parent_param: Hyperparameter | None = None,
        parent_value: str | None = None,
        **kwargs,
    ) -> ConfigurationSpace:
        """
        Define the configuration space for the SVM classifier.

        Returns
        -------
        ConfigurationSpace
            The configuration space containing hyperparameters for the SVM classifier.
        """
        if not CONFIGSPACE_AVAILABLE:
            raise RuntimeError(
                "ConfigSpace is not installed. Install optional extra with: pip install 'asf[configspace]'"
            )

        if cs is None:
            cs = ConfigurationSpace(name="SVM")

        prefix = cls.PREFIX
        max_iter = Constant(
            f"{prefix}:max_iter",
            20000,
        )
        kernel = Categorical(
            f"{prefix}:kernel",
            items=["linear", "rbf", "poly", "sigmoid"],
            default="rbf",
        )
        degree = Integer(f"{prefix}:degree", (1, 128), log=True, default=1)
        coef0 = Float(
            f"{prefix}:coef0",
            (-0.5, 0.5),
            log=False,
            default=0.49070634552851977,
        )
        tol = Float(
            f"{prefix}:tol",
            (1e-4, 1e-2),
            log=True,
            default=0.0002154969698207585,
        )
        gamma = Categorical(
            f"{prefix}:gamma",
            items=["scale", "auto"],
            default="scale",
        )
        C = Float(
            f"{prefix}:C",
            (1.0, 20),
            log=True,
            default=1.0,
        )
        shrinking = Categorical(
            f"{prefix}:shrinking",
            items=[True, False],
            default=True,
        )

        params = [kernel, degree, coef0, tol, gamma, C, shrinking, max_iter]

        gamma_cond = InCondition(
            child=gamma,
            parent=kernel,
            values=["rbf", "poly", "sigmoid"],
        )
        degree_cond = InCondition(
            child=degree,
            parent=kernel,
            values=["poly"],
        )
        cur_conds = [gamma_cond, degree_cond]

        if parent_param is not None:
            simple_params = [p for p in params if p not in (gamma, degree)]
            simple_equals = [
                EqualsCondition(child=param, parent=parent_param, value=parent_value)
                for param in simple_params
            ]

            gamma_eq = EqualsCondition(
                child=gamma, parent=parent_param, value=parent_value
            )
            degree_eq = EqualsCondition(
                child=degree, parent=parent_param, value=parent_value
            )

            # AndConjunction expects variadic condition arguments, not a list
            gamma_and = AndConjunction(gamma_eq, gamma_cond)
            degree_and = AndConjunction(degree_eq, degree_cond)

            conditions = simple_equals + [gamma_and, degree_and]

            cs.add(params + conditions)
        else:
            conditions = []
            cs.add(params + conditions + cur_conds)

        return cs

    @classmethod
    def _get_from_clean_configuration(
        cls,
        clean_config: dict[str, Any],
        **kwargs,
    ) -> partial:
        """
        Create a partial function from a clean (unprefixed) configuration.
        """
        # We need to manually handle 'kernel' logic because ConfigSpace doesn't
        # automatically filter inactive conditionals from the dictionary
        # if the input dictionary has them (which depends on how SMAC behaves).
        # Assuming clean_config has valid active parameters.

        # SklearnWrapper expects init_params dict, not kwargs
        svm_params = clean_config.copy()
        svm_params.update(kwargs)

        return partial(SVMClassifierWrapper, init_params=svm_params)

__init__(init_params={})

Initialize the SVMClassifierWrapper.

Parameters

init_params : dict, optional Dictionary of parameters to initialize the SVC model.

Source code in asf/predictors/svm.py
def __init__(self, init_params: dict[str, Any] = {}):
    """
    Initialize the SVMClassifierWrapper.

    Parameters
    ----------
    init_params : dict, optional
        Dictionary of parameters to initialize the SVC model.
    """
    super().__init__(SVC, init_params)

get_configuration_space(cs=None, pre_prefix='', parent_param=None, parent_value=None, **kwargs) classmethod

Define the configuration space for the SVM classifier.

Returns

ConfigurationSpace The configuration space containing hyperparameters for the SVM classifier.

Source code in asf/predictors/svm.py
@classmethod
def get_configuration_space(
    cls,
    cs: ConfigurationSpace | None = None,
    pre_prefix: str = "",
    parent_param: Hyperparameter | None = None,
    parent_value: str | None = None,
    **kwargs,
) -> ConfigurationSpace:
    """
    Define the configuration space for the SVM classifier.

    Returns
    -------
    ConfigurationSpace
        The configuration space containing hyperparameters for the SVM classifier.
    """
    if not CONFIGSPACE_AVAILABLE:
        raise RuntimeError(
            "ConfigSpace is not installed. Install optional extra with: pip install 'asf[configspace]'"
        )

    if cs is None:
        cs = ConfigurationSpace(name="SVM")

    prefix = cls.PREFIX
    max_iter = Constant(
        f"{prefix}:max_iter",
        20000,
    )
    kernel = Categorical(
        f"{prefix}:kernel",
        items=["linear", "rbf", "poly", "sigmoid"],
        default="rbf",
    )
    degree = Integer(f"{prefix}:degree", (1, 128), log=True, default=1)
    coef0 = Float(
        f"{prefix}:coef0",
        (-0.5, 0.5),
        log=False,
        default=0.49070634552851977,
    )
    tol = Float(
        f"{prefix}:tol",
        (1e-4, 1e-2),
        log=True,
        default=0.0002154969698207585,
    )
    gamma = Categorical(
        f"{prefix}:gamma",
        items=["scale", "auto"],
        default="scale",
    )
    C = Float(
        f"{prefix}:C",
        (1.0, 20),
        log=True,
        default=1.0,
    )
    shrinking = Categorical(
        f"{prefix}:shrinking",
        items=[True, False],
        default=True,
    )

    params = [kernel, degree, coef0, tol, gamma, C, shrinking, max_iter]

    gamma_cond = InCondition(
        child=gamma,
        parent=kernel,
        values=["rbf", "poly", "sigmoid"],
    )
    degree_cond = InCondition(
        child=degree,
        parent=kernel,
        values=["poly"],
    )
    cur_conds = [gamma_cond, degree_cond]

    if parent_param is not None:
        simple_params = [p for p in params if p not in (gamma, degree)]
        simple_equals = [
            EqualsCondition(child=param, parent=parent_param, value=parent_value)
            for param in simple_params
        ]

        gamma_eq = EqualsCondition(
            child=gamma, parent=parent_param, value=parent_value
        )
        degree_eq = EqualsCondition(
            child=degree, parent=parent_param, value=parent_value
        )

        # AndConjunction expects variadic condition arguments, not a list
        gamma_and = AndConjunction(gamma_eq, gamma_cond)
        degree_and = AndConjunction(degree_eq, degree_cond)

        conditions = simple_equals + [gamma_and, degree_and]

        cs.add(params + conditions)
    else:
        conditions = []
        cs.add(params + conditions + cur_conds)

    return cs

SVMRegressorWrapper

Bases: ConfigurableMixin, SklearnWrapper

A wrapper for the Scikit-learn SVR (Support Vector Regressor) model. Provides methods to define a configuration space and create an instance of the regressor from a configuration.

Attributes

PREFIX : str Prefix used for parameter names in the configuration space.

Source code in asf/predictors/svm.py
class SVMRegressorWrapper(ConfigurableMixin, SklearnWrapper):
    """
    A wrapper for the Scikit-learn SVR (Support Vector Regressor) model.
    Provides methods to define a configuration space and create an instance
    of the regressor from a configuration.

    Attributes
    ----------
    PREFIX : str
        Prefix used for parameter names in the configuration space.
    """

    PREFIX = "svm_regressor"

    def __init__(self, init_params: dict[str, Any] = {}):
        """
        Initialize the SVMRegressorWrapper.

        Parameters
        ----------
        init_params : dict, optional
            Dictionary of parameters to initialize the SVR model.
        """
        super().__init__(SVR, init_params)

    @classmethod
    def get_configuration_space(
        cls,
        cs: ConfigurationSpace | None = None,
        pre_prefix: str = "",
        parent_param: Hyperparameter | None = None,
        parent_value: str | None = None,
        **kwargs,
    ) -> ConfigurationSpace:
        """
        Define the configuration space for the SVM regressor.

        Parameters
        ----------
        cs : ConfigurationSpace, optional
            The configuration space to add the parameters to. If None, a new
            ConfigurationSpace will be created.

        Returns
        -------
        ConfigurationSpace
            The configuration space containing hyperparameters for the SVM regressor.
        """

        if not CONFIGSPACE_AVAILABLE:
            raise RuntimeError(
                "ConfigSpace is not installed. Install optional extra with: pip install 'asf[configspace]'"
            )

        prefix = cls.PREFIX

        if cs is None:
            cs = ConfigurationSpace(name="SVM Regressor")

        max_iter = Constant(
            f"{prefix}:max_iter",
            20000,
        )
        kernel = Categorical(
            f"{prefix}:kernel",
            items=["linear", "rbf", "poly", "sigmoid"],
            default="rbf",
        )
        degree = Integer(f"{prefix}:degree", (1, 128), log=True, default=1)
        coef0 = Float(
            f"{prefix}:coef0",
            (-0.5, 0.5),
            log=False,
            default=0.0,
        )
        tol = Float(
            f"{prefix}:tol",
            (1e-4, 1e-2),
            log=True,
            default=0.001,
        )
        gamma = Categorical(
            f"{prefix}:gamma",
            items=["scale", "auto"],
            default="scale",
        )
        C = Float(f"{prefix}:C", (1.0, 20), log=True, default=1.0)
        shrinking = Categorical(
            f"{prefix}:shrinking",
            items=[True, False],
            default=True,
        )
        epsilon = Float(
            f"{prefix}:epsilon",
            (0.01, 0.99),
            log=True,
            default=0.0251,
        )
        params = [kernel, degree, coef0, tol, gamma, C, shrinking, epsilon, max_iter]

        gamma_cond = InCondition(
            child=gamma,
            parent=kernel,
            values=["rbf", "poly", "sigmoid"],
        )
        degree_cond = InCondition(
            child=degree,
            parent=kernel,
            values=["poly"],
        )
        cur_conds = [gamma_cond, degree_cond]

        if parent_param is not None:
            simple_params = [p for p in params if p not in (gamma, degree)]
            simple_equals = [
                EqualsCondition(child=param, parent=parent_param, value=parent_value)
                for param in simple_params
            ]

            gamma_eq = EqualsCondition(
                child=gamma, parent=parent_param, value=parent_value
            )
            degree_eq = EqualsCondition(
                child=degree, parent=parent_param, value=parent_value
            )

            # AndConjunction expects variadic condition arguments, not a list
            gamma_and = AndConjunction(gamma_eq, gamma_cond)
            degree_and = AndConjunction(degree_eq, degree_cond)

            conditions = simple_equals + [gamma_and, degree_and]

            cs.add(params + conditions)
        else:
            conditions = []
            cs.add(params + conditions + cur_conds)

        return cs

    @classmethod
    def _get_from_clean_configuration(
        cls,
        clean_config: dict[str, Any],
        **kwargs,
    ) -> partial:
        """
        Create a partial function from a clean (unprefixed) configuration.
        """
        svm_params = clean_config.copy()
        svm_params.update(kwargs)

        return partial(SVMRegressorWrapper, init_params=svm_params)

__init__(init_params={})

Initialize the SVMRegressorWrapper.

Parameters

init_params : dict, optional Dictionary of parameters to initialize the SVR model.

Source code in asf/predictors/svm.py
def __init__(self, init_params: dict[str, Any] = {}):
    """
    Initialize the SVMRegressorWrapper.

    Parameters
    ----------
    init_params : dict, optional
        Dictionary of parameters to initialize the SVR model.
    """
    super().__init__(SVR, init_params)

get_configuration_space(cs=None, pre_prefix='', parent_param=None, parent_value=None, **kwargs) classmethod

Define the configuration space for the SVM regressor.

Parameters

cs : ConfigurationSpace, optional The configuration space to add the parameters to. If None, a new ConfigurationSpace will be created.

Returns

ConfigurationSpace The configuration space containing hyperparameters for the SVM regressor.

Source code in asf/predictors/svm.py
@classmethod
def get_configuration_space(
    cls,
    cs: ConfigurationSpace | None = None,
    pre_prefix: str = "",
    parent_param: Hyperparameter | None = None,
    parent_value: str | None = None,
    **kwargs,
) -> ConfigurationSpace:
    """
    Define the configuration space for the SVM regressor.

    Parameters
    ----------
    cs : ConfigurationSpace, optional
        The configuration space to add the parameters to. If None, a new
        ConfigurationSpace will be created.

    Returns
    -------
    ConfigurationSpace
        The configuration space containing hyperparameters for the SVM regressor.
    """

    if not CONFIGSPACE_AVAILABLE:
        raise RuntimeError(
            "ConfigSpace is not installed. Install optional extra with: pip install 'asf[configspace]'"
        )

    prefix = cls.PREFIX

    if cs is None:
        cs = ConfigurationSpace(name="SVM Regressor")

    max_iter = Constant(
        f"{prefix}:max_iter",
        20000,
    )
    kernel = Categorical(
        f"{prefix}:kernel",
        items=["linear", "rbf", "poly", "sigmoid"],
        default="rbf",
    )
    degree = Integer(f"{prefix}:degree", (1, 128), log=True, default=1)
    coef0 = Float(
        f"{prefix}:coef0",
        (-0.5, 0.5),
        log=False,
        default=0.0,
    )
    tol = Float(
        f"{prefix}:tol",
        (1e-4, 1e-2),
        log=True,
        default=0.001,
    )
    gamma = Categorical(
        f"{prefix}:gamma",
        items=["scale", "auto"],
        default="scale",
    )
    C = Float(f"{prefix}:C", (1.0, 20), log=True, default=1.0)
    shrinking = Categorical(
        f"{prefix}:shrinking",
        items=[True, False],
        default=True,
    )
    epsilon = Float(
        f"{prefix}:epsilon",
        (0.01, 0.99),
        log=True,
        default=0.0251,
    )
    params = [kernel, degree, coef0, tol, gamma, C, shrinking, epsilon, max_iter]

    gamma_cond = InCondition(
        child=gamma,
        parent=kernel,
        values=["rbf", "poly", "sigmoid"],
    )
    degree_cond = InCondition(
        child=degree,
        parent=kernel,
        values=["poly"],
    )
    cur_conds = [gamma_cond, degree_cond]

    if parent_param is not None:
        simple_params = [p for p in params if p not in (gamma, degree)]
        simple_equals = [
            EqualsCondition(child=param, parent=parent_param, value=parent_value)
            for param in simple_params
        ]

        gamma_eq = EqualsCondition(
            child=gamma, parent=parent_param, value=parent_value
        )
        degree_eq = EqualsCondition(
            child=degree, parent=parent_param, value=parent_value
        )

        # AndConjunction expects variadic condition arguments, not a list
        gamma_and = AndConjunction(gamma_eq, gamma_cond)
        degree_and = AndConjunction(degree_eq, degree_cond)

        conditions = simple_equals + [gamma_and, degree_and]

        cs.add(params + conditions)
    else:
        conditions = []
        cs.add(params + conditions + cur_conds)

    return cs

SklearnWrapper

Bases: AbstractPredictor

A generic wrapper for scikit-learn models.

This class allows scikit-learn models to be used with the ASF framework.

Parameters

model_class : type[BaseEstimator] A scikit-learn model class. init_params : dict[str, Any], optional Initialization parameters for the scikit-learn model (default is {}).

Source code in asf/predictors/sklearn_wrapper.py
class SklearnWrapper(AbstractPredictor):
    """
    A generic wrapper for scikit-learn models.

    This class allows scikit-learn models to be used with the ASF framework.

    Parameters
    ----------
    model_class : type[BaseEstimator]
        A scikit-learn model class.
    init_params : dict[str, Any], optional
        Initialization parameters for the scikit-learn model (default is {}).
    """

    def __init__(
        self,
        model_class: Any,
        init_params: dict[str, Any] | None = None,
    ):
        super().__init__()
        self.model_class: Any = model_class(**(init_params or {}))

    def fit(
        self,
        X: np.ndarray,
        Y: np.ndarray,
        sample_weight: np.ndarray | None = None,
        **kwargs: Any,
    ) -> None:
        """
        Fit the model to the data.

        Parameters
        ----------
        X : np.ndarray
            Training data of shape (n_samples, n_features).
        Y : np.ndarray
            Target values of shape (n_samples,).
        sample_weight : np.ndarray or None, default=None
            Sample weights of shape (n_samples,).
        **kwargs : Any
            Additional keyword arguments for the scikit-learn model's `fit` method.
        """
        self.model_class.fit(X, Y, sample_weight=sample_weight, **kwargs)

    def predict(self, X: np.ndarray, **kwargs: Any) -> np.ndarray:
        """
        Predict using the model.

        Parameters
        ----------
        X : np.ndarray
            Data to predict on of shape (n_samples, n_features).
        **kwargs : Any
            Additional keyword arguments for the scikit-learn model's `predict` method.

        Returns
        -------
        np.ndarray
            Predicted values of shape (n_samples,).
        """
        return self.model_class.predict(X, **kwargs)

    def save(self, file_path: str) -> None:
        """
        Save the model to a file.

        Parameters
        ----------
        file_path : str
            Path to the file where the model will be saved.
        """
        joblib.dump(self, file_path)

    @classmethod
    def load(cls, file_path: str) -> SklearnWrapper:
        """
        Load the model from a file.

        Parameters
        ----------
        file_path : str
            Path to the file from which the model will be loaded.

        Returns
        -------
        SklearnWrapper
            The loaded model.
        """
        return joblib.load(file_path)

fit(X, Y, sample_weight=None, **kwargs)

Fit the model to the data.

Parameters

X : np.ndarray Training data of shape (n_samples, n_features). Y : np.ndarray Target values of shape (n_samples,). sample_weight : np.ndarray or None, default=None Sample weights of shape (n_samples,). **kwargs : Any Additional keyword arguments for the scikit-learn model's fit method.

Source code in asf/predictors/sklearn_wrapper.py
def fit(
    self,
    X: np.ndarray,
    Y: np.ndarray,
    sample_weight: np.ndarray | None = None,
    **kwargs: Any,
) -> None:
    """
    Fit the model to the data.

    Parameters
    ----------
    X : np.ndarray
        Training data of shape (n_samples, n_features).
    Y : np.ndarray
        Target values of shape (n_samples,).
    sample_weight : np.ndarray or None, default=None
        Sample weights of shape (n_samples,).
    **kwargs : Any
        Additional keyword arguments for the scikit-learn model's `fit` method.
    """
    self.model_class.fit(X, Y, sample_weight=sample_weight, **kwargs)

load(file_path) classmethod

Load the model from a file.

Parameters

file_path : str Path to the file from which the model will be loaded.

Returns

SklearnWrapper The loaded model.

Source code in asf/predictors/sklearn_wrapper.py
@classmethod
def load(cls, file_path: str) -> SklearnWrapper:
    """
    Load the model from a file.

    Parameters
    ----------
    file_path : str
        Path to the file from which the model will be loaded.

    Returns
    -------
    SklearnWrapper
        The loaded model.
    """
    return joblib.load(file_path)

predict(X, **kwargs)

Predict using the model.

Parameters

X : np.ndarray Data to predict on of shape (n_samples, n_features). **kwargs : Any Additional keyword arguments for the scikit-learn model's predict method.

Returns

np.ndarray Predicted values of shape (n_samples,).

Source code in asf/predictors/sklearn_wrapper.py
def predict(self, X: np.ndarray, **kwargs: Any) -> np.ndarray:
    """
    Predict using the model.

    Parameters
    ----------
    X : np.ndarray
        Data to predict on of shape (n_samples, n_features).
    **kwargs : Any
        Additional keyword arguments for the scikit-learn model's `predict` method.

    Returns
    -------
    np.ndarray
        Predicted values of shape (n_samples,).
    """
    return self.model_class.predict(X, **kwargs)

save(file_path)

Save the model to a file.

Parameters

file_path : str Path to the file where the model will be saved.

Source code in asf/predictors/sklearn_wrapper.py
def save(self, file_path: str) -> None:
    """
    Save the model to a file.

    Parameters
    ----------
    file_path : str
        Path to the file where the model will be saved.
    """
    joblib.dump(self, file_path)

XGBoostClassifierWrapper

Bases: ConfigurableMixin, SklearnWrapper

Wrapper for the XGBoost classifier to integrate with the ASF framework.

Source code in asf/predictors/xgboost.py
class XGBoostClassifierWrapper(ConfigurableMixin, SklearnWrapper):
    """
    Wrapper for the XGBoost classifier to integrate with the ASF framework.
    """

    PREFIX: str = "xgb_classifier"

    def __init__(self, init_params: dict[str, Any] | None = None):
        """
        Initialize the XGBoostClassifierWrapper.

        Parameters
        ----------
        init_params : dict, optional
            Initialization parameters for the XGBoost classifier.
        """
        if not XGB_AVAILABLE:
            raise ImportError(
                "XGBoost is not installed. Please install it using pip install asf-lib[xgb]."
            )
        super().__init__(XGBClassifier, init_params or {})

    def fit(
        self,
        X: np.ndarray,
        Y: np.ndarray,
        sample_weight: np.ndarray | None = None,
        **kwargs: Any,
    ) -> None:
        """
        Fit the model to the data.

        Parameters
        ----------
        X : np.ndarray
            Training data of shape (n_samples, n_features).
        Y : np.ndarray
            Target values of shape (n_samples,).
        sample_weight : np.ndarray, optional
            Sample weights of shape (n_samples,) (default is None).
        **kwargs : Any
            Additional keyword arguments for the scikit-learn model's `fit` method.
        """
        if Y.dtype == bool:
            self.bool_labels = True
        else:
            self.bool_labels = False

        self.model_class.fit(X, Y, sample_weight=sample_weight, **kwargs)  # type: ignore[attr-defined]

    def predict(self, X: np.ndarray, **kwargs: Any) -> np.ndarray:
        """
        Predict using the model.

        Parameters
        ----------
        X : np.ndarray
            Data to predict on of shape (n_samples, n_features).
        **kwargs : Any
            Additional keyword arguments for the scikit-learn model's `predict` method.

        Returns
        -------
        np.ndarray
            Predicted values of shape (n_samples,).
        """
        if self.bool_labels:
            return self.model_class.predict(X, **kwargs).astype(bool)  # type: ignore[attr-defined]
        return self.model_class.predict(X, **kwargs)  # type: ignore[attr-defined]

    @staticmethod
    def _define_hyperparameters(**kwargs):
        """Define hyperparameters for XGBoost classifier."""
        if not CONFIGSPACE_AVAILABLE:
            return [], [], []

        hyperparameters = [
            Constant("booster", "gbtree"),
            Constant("n_estimators", 2000),
            Integer("max_depth", (1, 11), log=False, default=8),
            Integer("min_child_weight", (1, 100), log=True, default=39),
            Float(
                "colsample_bytree", (0.0, 1.0), log=False, default=0.2545374925231651
            ),
            Float(
                "colsample_bylevel", (0.0, 1.0), log=False, default=0.6909224923784677
            ),
            Float("lambda", (0.001, 1000), log=True, default=31.393252465064943),
            Float("alpha", (0.001, 1000), log=True, default=0.24167936088332426),
            Float(
                "learning_rate", (0.001, 0.1), log=True, default=0.008237525103357958
            ),
        ]
        return hyperparameters, [], []

    @classmethod
    def _get_from_clean_configuration(
        cls,
        clean_config: dict[str, Any],
        **kwargs,
    ) -> partial:
        """
        Create a partial function from a clean (unprefixed) configuration.
        """
        xgb_params = clean_config.copy()
        xgb_params.update(kwargs)

        return partial(XGBoostClassifierWrapper, init_params=xgb_params)

__init__(init_params=None)

Initialize the XGBoostClassifierWrapper.

Parameters

init_params : dict, optional Initialization parameters for the XGBoost classifier.

Source code in asf/predictors/xgboost.py
def __init__(self, init_params: dict[str, Any] | None = None):
    """
    Initialize the XGBoostClassifierWrapper.

    Parameters
    ----------
    init_params : dict, optional
        Initialization parameters for the XGBoost classifier.
    """
    if not XGB_AVAILABLE:
        raise ImportError(
            "XGBoost is not installed. Please install it using pip install asf-lib[xgb]."
        )
    super().__init__(XGBClassifier, init_params or {})

fit(X, Y, sample_weight=None, **kwargs)

Fit the model to the data.

Parameters

X : np.ndarray Training data of shape (n_samples, n_features). Y : np.ndarray Target values of shape (n_samples,). sample_weight : np.ndarray, optional Sample weights of shape (n_samples,) (default is None). **kwargs : Any Additional keyword arguments for the scikit-learn model's fit method.

Source code in asf/predictors/xgboost.py
def fit(
    self,
    X: np.ndarray,
    Y: np.ndarray,
    sample_weight: np.ndarray | None = None,
    **kwargs: Any,
) -> None:
    """
    Fit the model to the data.

    Parameters
    ----------
    X : np.ndarray
        Training data of shape (n_samples, n_features).
    Y : np.ndarray
        Target values of shape (n_samples,).
    sample_weight : np.ndarray, optional
        Sample weights of shape (n_samples,) (default is None).
    **kwargs : Any
        Additional keyword arguments for the scikit-learn model's `fit` method.
    """
    if Y.dtype == bool:
        self.bool_labels = True
    else:
        self.bool_labels = False

    self.model_class.fit(X, Y, sample_weight=sample_weight, **kwargs)  # type: ignore[attr-defined]

predict(X, **kwargs)

Predict using the model.

Parameters

X : np.ndarray Data to predict on of shape (n_samples, n_features). **kwargs : Any Additional keyword arguments for the scikit-learn model's predict method.

Returns

np.ndarray Predicted values of shape (n_samples,).

Source code in asf/predictors/xgboost.py
def predict(self, X: np.ndarray, **kwargs: Any) -> np.ndarray:
    """
    Predict using the model.

    Parameters
    ----------
    X : np.ndarray
        Data to predict on of shape (n_samples, n_features).
    **kwargs : Any
        Additional keyword arguments for the scikit-learn model's `predict` method.

    Returns
    -------
    np.ndarray
        Predicted values of shape (n_samples,).
    """
    if self.bool_labels:
        return self.model_class.predict(X, **kwargs).astype(bool)  # type: ignore[attr-defined]
    return self.model_class.predict(X, **kwargs)  # type: ignore[attr-defined]

XGBoostRegressorWrapper

Bases: ConfigurableMixin, SklearnWrapper

Wrapper for the XGBoost regressor to integrate with the ASF framework.

Source code in asf/predictors/xgboost.py
class XGBoostRegressorWrapper(ConfigurableMixin, SklearnWrapper):
    """
    Wrapper for the XGBoost regressor to integrate with the ASF framework.
    """

    PREFIX: str = "xgb_regressor"

    def __init__(self, init_params: dict[str, Any] | None = None):
        """
        Initialize the XGBoostRegressorWrapper.

        Parameters
        ----------
        init_params : dict, optional
            Initialization parameters for the XGBoost regressor.
        """
        super().__init__(XGBRegressor, init_params or {})

    @staticmethod
    def _define_hyperparameters(**kwargs):
        """Define hyperparameters for XGBoost regressor."""
        if not CONFIGSPACE_AVAILABLE:
            return [], [], []

        hyperparameters = [
            Constant("booster", "gbtree"),
            Constant("n_estimators", 2000),
            Integer("max_depth", (1, 11), log=False, default=8),
            Integer("min_child_weight", (1, 100), log=True, default=39),
            Float(
                "colsample_bytree", (0.0, 1.0), log=False, default=0.2545374925231651
            ),
            Float(
                "colsample_bylevel", (0.0, 1.0), log=False, default=0.6909224923784677
            ),
            Float("lambda", (0.001, 1000), log=True, default=31.393252465064943),
            Float("alpha", (0.001, 1000), log=True, default=0.24167936088332426),
            Float(
                "learning_rate", (0.001, 0.1), log=True, default=0.008237525103357958
            ),
        ]
        return hyperparameters, [], []

    @classmethod
    def _get_from_clean_configuration(
        cls,
        clean_config: dict[str, Any],
        **kwargs,
    ) -> partial:
        """
        Create a partial function from a clean (unprefixed) configuration.
        """
        xgb_params = clean_config.copy()
        xgb_params.update(kwargs)

        return partial(XGBoostRegressorWrapper, init_params=xgb_params)

__init__(init_params=None)

Initialize the XGBoostRegressorWrapper.

Parameters

init_params : dict, optional Initialization parameters for the XGBoost regressor.

Source code in asf/predictors/xgboost.py
def __init__(self, init_params: dict[str, Any] | None = None):
    """
    Initialize the XGBoostRegressorWrapper.

    Parameters
    ----------
    init_params : dict, optional
        Initialization parameters for the XGBoost regressor.
    """
    super().__init__(XGBRegressor, init_params or {})