Skip to content

Regressor

Bases: ABC

Abstract base class for all regressors.

Source code in src/safeds/ml/classical/regression/_regressor.py
class Regressor(ABC):
    """Abstract base class for all regressors."""

    @abstractmethod
    def fit(self, training_set: TaggedTable) -> Regressor:
        """
        Create a copy of this regressor and fit it with the given training data.

        This regressor is not modified.

        Parameters
        ----------
        training_set : TaggedTable
            The training data containing the feature and target vectors.

        Returns
        -------
        fitted_regressor : Regressor
            The fitted regressor.

        Raises
        ------
        LearningError
            If the training data contains invalid values or if the training failed.
        """

    @abstractmethod
    def predict(self, dataset: Table) -> TaggedTable:
        """
        Predict a target vector using a dataset containing feature vectors. The model has to be trained first.

        Parameters
        ----------
        dataset : Table
            The dataset containing the feature vectors.

        Returns
        -------
        table : TaggedTable
            A dataset containing the given feature vectors and the predicted target vector.

        Raises
        ------
        ModelNotFittedError
            If the model has not been fitted yet.
        DatasetContainsTargetError
            If the dataset contains the target column already.
        DatasetMissesFeaturesError
            If the dataset misses feature columns.
        PredictionError
            If predicting with the given dataset failed.
        """

    @abstractmethod
    def is_fitted(self) -> bool:
        """
        Check if the classifier is fitted.

        Returns
        -------
        is_fitted : bool
            Whether the regressor is fitted.
        """

    @abstractmethod
    def _get_sklearn_regressor(self) -> RegressorMixin:
        """
        Return a new wrapped Regressor from sklearn.

        Returns
        -------
        wrapped_regressor: RegressorMixin
            The sklearn Regressor.
        """

    # noinspection PyProtectedMember
    def mean_squared_error(self, validation_or_test_set: TaggedTable) -> float:
        """
        Compute the mean squared error (MSE) on the given data.

        Parameters
        ----------
        validation_or_test_set : TaggedTable
            The validation or test set.

        Returns
        -------
        mean_squared_error : float
            The calculated mean squared error (the average of the distance of each individual row squared).

        Raises
        ------
        UntaggedTableError
            If the table is untagged.
        """
        if not isinstance(validation_or_test_set, TaggedTable) and isinstance(validation_or_test_set, Table):
            raise UntaggedTableError
        expected = validation_or_test_set.target
        predicted = self.predict(validation_or_test_set.features).target

        _check_metrics_preconditions(predicted, expected)
        return sk_mean_squared_error(expected._data, predicted._data)

    # noinspection PyProtectedMember
    def mean_absolute_error(self, validation_or_test_set: TaggedTable) -> float:
        """
        Compute the mean absolute error (MAE) of the regressor on the given data.

        Parameters
        ----------
        validation_or_test_set : TaggedTable
            The validation or test set.

        Returns
        -------
        mean_absolute_error : float
            The calculated mean absolute error (the average of the distance of each individual row).

        Raises
        ------
        UntaggedTableError
            If the table is untagged.
        """
        if not isinstance(validation_or_test_set, TaggedTable) and isinstance(validation_or_test_set, Table):
            raise UntaggedTableError
        expected = validation_or_test_set.target
        predicted = self.predict(validation_or_test_set.features).target

        _check_metrics_preconditions(predicted, expected)
        return sk_mean_absolute_error(expected._data, predicted._data)

fit(training_set) abstractmethod

Create a copy of this regressor and fit it with the given training data.

This regressor is not modified.

Parameters:

Name Type Description Default
training_set TaggedTable

The training data containing the feature and target vectors.

required

Returns:

Name Type Description
fitted_regressor Regressor

The fitted regressor.

Raises:

Type Description
LearningError

If the training data contains invalid values or if the training failed.

Source code in src/safeds/ml/classical/regression/_regressor.py
@abstractmethod
def fit(self, training_set: TaggedTable) -> Regressor:
    """
    Create a copy of this regressor and fit it with the given training data.

    This regressor is not modified.

    Parameters
    ----------
    training_set : TaggedTable
        The training data containing the feature and target vectors.

    Returns
    -------
    fitted_regressor : Regressor
        The fitted regressor.

    Raises
    ------
    LearningError
        If the training data contains invalid values or if the training failed.
    """

is_fitted() abstractmethod

Check if the classifier is fitted.

Returns:

Name Type Description
is_fitted bool

Whether the regressor is fitted.

Source code in src/safeds/ml/classical/regression/_regressor.py
@abstractmethod
def is_fitted(self) -> bool:
    """
    Check if the classifier is fitted.

    Returns
    -------
    is_fitted : bool
        Whether the regressor is fitted.
    """

mean_absolute_error(validation_or_test_set)

Compute the mean absolute error (MAE) of the regressor on the given data.

Parameters:

Name Type Description Default
validation_or_test_set TaggedTable

The validation or test set.

required

Returns:

Name Type Description
mean_absolute_error float

The calculated mean absolute error (the average of the distance of each individual row).

Raises:

Type Description
UntaggedTableError

If the table is untagged.

Source code in src/safeds/ml/classical/regression/_regressor.py
def mean_absolute_error(self, validation_or_test_set: TaggedTable) -> float:
    """
    Compute the mean absolute error (MAE) of the regressor on the given data.

    Parameters
    ----------
    validation_or_test_set : TaggedTable
        The validation or test set.

    Returns
    -------
    mean_absolute_error : float
        The calculated mean absolute error (the average of the distance of each individual row).

    Raises
    ------
    UntaggedTableError
        If the table is untagged.
    """
    if not isinstance(validation_or_test_set, TaggedTable) and isinstance(validation_or_test_set, Table):
        raise UntaggedTableError
    expected = validation_or_test_set.target
    predicted = self.predict(validation_or_test_set.features).target

    _check_metrics_preconditions(predicted, expected)
    return sk_mean_absolute_error(expected._data, predicted._data)

mean_squared_error(validation_or_test_set)

Compute the mean squared error (MSE) on the given data.

Parameters:

Name Type Description Default
validation_or_test_set TaggedTable

The validation or test set.

required

Returns:

Name Type Description
mean_squared_error float

The calculated mean squared error (the average of the distance of each individual row squared).

Raises:

Type Description
UntaggedTableError

If the table is untagged.

Source code in src/safeds/ml/classical/regression/_regressor.py
def mean_squared_error(self, validation_or_test_set: TaggedTable) -> float:
    """
    Compute the mean squared error (MSE) on the given data.

    Parameters
    ----------
    validation_or_test_set : TaggedTable
        The validation or test set.

    Returns
    -------
    mean_squared_error : float
        The calculated mean squared error (the average of the distance of each individual row squared).

    Raises
    ------
    UntaggedTableError
        If the table is untagged.
    """
    if not isinstance(validation_or_test_set, TaggedTable) and isinstance(validation_or_test_set, Table):
        raise UntaggedTableError
    expected = validation_or_test_set.target
    predicted = self.predict(validation_or_test_set.features).target

    _check_metrics_preconditions(predicted, expected)
    return sk_mean_squared_error(expected._data, predicted._data)

predict(dataset) abstractmethod

Predict a target vector using a dataset containing feature vectors. The model has to be trained first.

Parameters:

Name Type Description Default
dataset Table

The dataset containing the feature vectors.

required

Returns:

Name Type Description
table TaggedTable

A dataset containing the given feature vectors and the predicted target vector.

Raises:

Type Description
ModelNotFittedError

If the model has not been fitted yet.

DatasetContainsTargetError

If the dataset contains the target column already.

DatasetMissesFeaturesError

If the dataset misses feature columns.

PredictionError

If predicting with the given dataset failed.

Source code in src/safeds/ml/classical/regression/_regressor.py
@abstractmethod
def predict(self, dataset: Table) -> TaggedTable:
    """
    Predict a target vector using a dataset containing feature vectors. The model has to be trained first.

    Parameters
    ----------
    dataset : Table
        The dataset containing the feature vectors.

    Returns
    -------
    table : TaggedTable
        A dataset containing the given feature vectors and the predicted target vector.

    Raises
    ------
    ModelNotFittedError
        If the model has not been fitted yet.
    DatasetContainsTargetError
        If the dataset contains the target column already.
    DatasetMissesFeaturesError
        If the dataset misses feature columns.
    PredictionError
        If predicting with the given dataset failed.
    """