Skip to content

SupportVectorMachineRegressor

Bases: Regressor

Support vector machine.

Parameters:

Name Type Description Default
c float

The strength of regularization. Must be strictly positive.

1.0
kernel SupportVectorMachineKernel | None

The type of kernel to be used. Defaults to None.

None

Raises:

Type Description
OutOfBoundsError

If c is less than or equal to 0.

Source code in src/safeds/ml/classical/regression/_support_vector_machine.py
class SupportVectorMachineRegressor(Regressor):
    """
    Support vector machine.

    Parameters
    ----------
    c: float
        The strength of regularization. Must be strictly positive.
    kernel: SupportVectorMachineKernel | None
        The type of kernel to be used. Defaults to None.

    Raises
    ------
    OutOfBoundsError
        If `c` is less than or equal to 0.
    """

    def __hash__(self) -> int:
        return _structural_hash(Regressor.__hash__(self), self._target_name, self._feature_names, self._c, self.kernel)

    def __init__(self, *, c: float = 1.0, kernel: SupportVectorMachineKernel | None = None) -> None:
        # Internal state
        self._wrapped_regressor: sk_SVR | None = None
        self._feature_names: list[str] | None = None
        self._target_name: str | None = None

        # Hyperparameters
        if c <= 0:
            raise OutOfBoundsError(c, name="c", lower_bound=OpenBound(0))
        self._c = c
        self._kernel = kernel

    @property
    def c(self) -> float:
        """
        Get the regularization strength.

        Returns
        -------
        result: float
            The regularization strength.
        """
        return self._c

    @property
    def kernel(self) -> SupportVectorMachineKernel | None:
        """
        Get the type of kernel used.

        Returns
        -------
        result: SupportVectorMachineKernel | None
            The type of kernel used.
        """
        return self._kernel

    class Kernel:
        class Linear(SupportVectorMachineKernel):
            def _get_sklearn_kernel(self) -> str:
                """
                Get the name of the linear kernel.

                Returns
                -------
                result: str
                    The name of the linear kernel.
                """
                return "linear"

            def __eq__(self, other: object) -> bool:
                if not isinstance(other, SupportVectorMachineRegressor.Kernel.Linear):
                    return NotImplemented
                return True

            __hash__ = SupportVectorMachineKernel.__hash__

        class Polynomial(SupportVectorMachineKernel):
            def __init__(self, degree: int):
                if degree < 1:
                    raise OutOfBoundsError(degree, name="degree", lower_bound=ClosedBound(1))
                self._degree = degree

            def _get_sklearn_kernel(self) -> str:
                """
                Get the name of the polynomial kernel.

                Returns
                -------
                result: str
                    The name of the polynomial kernel.
                """
                return "poly"

            def __eq__(self, other: object) -> bool:
                if not isinstance(other, SupportVectorMachineRegressor.Kernel.Polynomial):
                    return NotImplemented
                return self._degree == other._degree

            def __hash__(self) -> int:
                return _structural_hash(SupportVectorMachineKernel.__hash__(self), self._degree)

            def __sizeof__(self) -> int:
                """
                Return the complete size of this object.

                Returns
                -------
                size:
                    Size of this object in bytes.
                """
                return sys.getsizeof(self._degree)

        class Sigmoid(SupportVectorMachineKernel):
            def _get_sklearn_kernel(self) -> str:
                """
                Get the name of the sigmoid kernel.

                Returns
                -------
                result: str
                    The name of the sigmoid kernel.
                """
                return "sigmoid"

            def __eq__(self, other: object) -> bool:
                if not isinstance(other, SupportVectorMachineRegressor.Kernel.Sigmoid):
                    return NotImplemented
                return True

            __hash__ = SupportVectorMachineKernel.__hash__

        class RadialBasisFunction(SupportVectorMachineKernel):
            def _get_sklearn_kernel(self) -> str:
                """
                Get the name of the radial basis function (RBF) kernel.

                Returns
                -------
                result: str
                    The name of the RBF kernel.
                """
                return "rbf"

            def __eq__(self, other: object) -> bool:
                if not isinstance(other, SupportVectorMachineRegressor.Kernel.RadialBasisFunction):
                    return NotImplemented
                return True

            __hash__ = SupportVectorMachineKernel.__hash__

    def _get_kernel_name(self) -> str:
        """
        Get the name of the kernel.

        Returns
        -------
        result: str
            The name of the kernel.

        Raises
        ------
        TypeError
            If the kernel type is invalid.
        """
        if isinstance(self.kernel, SupportVectorMachineRegressor.Kernel.Linear):
            return "linear"
        elif isinstance(self.kernel, SupportVectorMachineRegressor.Kernel.Polynomial):
            return "poly"
        elif isinstance(self.kernel, SupportVectorMachineRegressor.Kernel.Sigmoid):
            return "sigmoid"
        elif isinstance(self.kernel, SupportVectorMachineRegressor.Kernel.RadialBasisFunction):
            return "rbf"
        else:
            raise TypeError("Invalid kernel type.")

    def fit(self, training_set: TaggedTable) -> SupportVectorMachineRegressor:
        """
        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 : SupportVectorMachineRegressor
            The fitted regressor.

        Raises
        ------
        LearningError
            If the training data contains invalid values or if the training failed.
        UntaggedTableError
            If the table is untagged.
        NonNumericColumnError
            If the training data contains non-numerical values.
        MissingValuesColumnError
            If the training data contains missing values.
        DatasetMissesDataError
            If the training data contains no rows.
        """
        wrapped_regressor = self._get_sklearn_regressor()
        fit(wrapped_regressor, training_set)

        result = SupportVectorMachineRegressor(c=self._c, kernel=self._kernel)
        result._wrapped_regressor = wrapped_regressor
        result._feature_names = training_set.features.column_names
        result._target_name = training_set.target.name

        return result

    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.
        NonNumericColumnError
            If the dataset contains non-numerical values.
        MissingValuesColumnError
            If the dataset contains missing values.
        DatasetMissesDataError
            If the dataset contains no rows.
        """
        return predict(self._wrapped_regressor, dataset, self._feature_names, self._target_name)

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

        Returns
        -------
        is_fitted : bool
            Whether the regressor is fitted.
        """
        return self._wrapped_regressor is not None

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

        Returns
        -------
        wrapped_regressor: RegressorMixin
            The sklearn Regressor.
        """
        from sklearn.svm import SVR as sk_SVR  # noqa: N811

        return sk_SVR(C=self._c)

c: float property

Get the regularization strength.

Returns:

Name Type Description
result float

The regularization strength.

kernel: SupportVectorMachineKernel | None property

Get the type of kernel used.

Returns:

Name Type Description
result SupportVectorMachineKernel | None

The type of kernel used.

Kernel

Source code in src/safeds/ml/classical/regression/_support_vector_machine.py
class Kernel:
    class Linear(SupportVectorMachineKernel):
        def _get_sklearn_kernel(self) -> str:
            """
            Get the name of the linear kernel.

            Returns
            -------
            result: str
                The name of the linear kernel.
            """
            return "linear"

        def __eq__(self, other: object) -> bool:
            if not isinstance(other, SupportVectorMachineRegressor.Kernel.Linear):
                return NotImplemented
            return True

        __hash__ = SupportVectorMachineKernel.__hash__

    class Polynomial(SupportVectorMachineKernel):
        def __init__(self, degree: int):
            if degree < 1:
                raise OutOfBoundsError(degree, name="degree", lower_bound=ClosedBound(1))
            self._degree = degree

        def _get_sklearn_kernel(self) -> str:
            """
            Get the name of the polynomial kernel.

            Returns
            -------
            result: str
                The name of the polynomial kernel.
            """
            return "poly"

        def __eq__(self, other: object) -> bool:
            if not isinstance(other, SupportVectorMachineRegressor.Kernel.Polynomial):
                return NotImplemented
            return self._degree == other._degree

        def __hash__(self) -> int:
            return _structural_hash(SupportVectorMachineKernel.__hash__(self), self._degree)

        def __sizeof__(self) -> int:
            """
            Return the complete size of this object.

            Returns
            -------
            size:
                Size of this object in bytes.
            """
            return sys.getsizeof(self._degree)

    class Sigmoid(SupportVectorMachineKernel):
        def _get_sklearn_kernel(self) -> str:
            """
            Get the name of the sigmoid kernel.

            Returns
            -------
            result: str
                The name of the sigmoid kernel.
            """
            return "sigmoid"

        def __eq__(self, other: object) -> bool:
            if not isinstance(other, SupportVectorMachineRegressor.Kernel.Sigmoid):
                return NotImplemented
            return True

        __hash__ = SupportVectorMachineKernel.__hash__

    class RadialBasisFunction(SupportVectorMachineKernel):
        def _get_sklearn_kernel(self) -> str:
            """
            Get the name of the radial basis function (RBF) kernel.

            Returns
            -------
            result: str
                The name of the RBF kernel.
            """
            return "rbf"

        def __eq__(self, other: object) -> bool:
            if not isinstance(other, SupportVectorMachineRegressor.Kernel.RadialBasisFunction):
                return NotImplemented
            return True

        __hash__ = SupportVectorMachineKernel.__hash__

Linear

Bases: SupportVectorMachineKernel

Source code in src/safeds/ml/classical/regression/_support_vector_machine.py
class Linear(SupportVectorMachineKernel):
    def _get_sklearn_kernel(self) -> str:
        """
        Get the name of the linear kernel.

        Returns
        -------
        result: str
            The name of the linear kernel.
        """
        return "linear"

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, SupportVectorMachineRegressor.Kernel.Linear):
            return NotImplemented
        return True

    __hash__ = SupportVectorMachineKernel.__hash__

__hash__ = SupportVectorMachineKernel.__hash__ class-attribute instance-attribute

__eq__(other)

Source code in src/safeds/ml/classical/regression/_support_vector_machine.py
def __eq__(self, other: object) -> bool:
    if not isinstance(other, SupportVectorMachineRegressor.Kernel.Linear):
        return NotImplemented
    return True

Polynomial

Bases: SupportVectorMachineKernel

Source code in src/safeds/ml/classical/regression/_support_vector_machine.py
class Polynomial(SupportVectorMachineKernel):
    def __init__(self, degree: int):
        if degree < 1:
            raise OutOfBoundsError(degree, name="degree", lower_bound=ClosedBound(1))
        self._degree = degree

    def _get_sklearn_kernel(self) -> str:
        """
        Get the name of the polynomial kernel.

        Returns
        -------
        result: str
            The name of the polynomial kernel.
        """
        return "poly"

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, SupportVectorMachineRegressor.Kernel.Polynomial):
            return NotImplemented
        return self._degree == other._degree

    def __hash__(self) -> int:
        return _structural_hash(SupportVectorMachineKernel.__hash__(self), self._degree)

    def __sizeof__(self) -> int:
        """
        Return the complete size of this object.

        Returns
        -------
        size:
            Size of this object in bytes.
        """
        return sys.getsizeof(self._degree)

__eq__(other)

Source code in src/safeds/ml/classical/regression/_support_vector_machine.py
def __eq__(self, other: object) -> bool:
    if not isinstance(other, SupportVectorMachineRegressor.Kernel.Polynomial):
        return NotImplemented
    return self._degree == other._degree

__hash__()

Source code in src/safeds/ml/classical/regression/_support_vector_machine.py
def __hash__(self) -> int:
    return _structural_hash(SupportVectorMachineKernel.__hash__(self), self._degree)

__init__(degree)

Source code in src/safeds/ml/classical/regression/_support_vector_machine.py
def __init__(self, degree: int):
    if degree < 1:
        raise OutOfBoundsError(degree, name="degree", lower_bound=ClosedBound(1))
    self._degree = degree

__sizeof__()

Return the complete size of this object.

Returns:

Name Type Description
size int

Size of this object in bytes.

Source code in src/safeds/ml/classical/regression/_support_vector_machine.py
def __sizeof__(self) -> int:
    """
    Return the complete size of this object.

    Returns
    -------
    size:
        Size of this object in bytes.
    """
    return sys.getsizeof(self._degree)

RadialBasisFunction

Bases: SupportVectorMachineKernel

Source code in src/safeds/ml/classical/regression/_support_vector_machine.py
class RadialBasisFunction(SupportVectorMachineKernel):
    def _get_sklearn_kernel(self) -> str:
        """
        Get the name of the radial basis function (RBF) kernel.

        Returns
        -------
        result: str
            The name of the RBF kernel.
        """
        return "rbf"

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, SupportVectorMachineRegressor.Kernel.RadialBasisFunction):
            return NotImplemented
        return True

    __hash__ = SupportVectorMachineKernel.__hash__

__hash__ = SupportVectorMachineKernel.__hash__ class-attribute instance-attribute

__eq__(other)

Source code in src/safeds/ml/classical/regression/_support_vector_machine.py
def __eq__(self, other: object) -> bool:
    if not isinstance(other, SupportVectorMachineRegressor.Kernel.RadialBasisFunction):
        return NotImplemented
    return True

Sigmoid

Bases: SupportVectorMachineKernel

Source code in src/safeds/ml/classical/regression/_support_vector_machine.py
class Sigmoid(SupportVectorMachineKernel):
    def _get_sklearn_kernel(self) -> str:
        """
        Get the name of the sigmoid kernel.

        Returns
        -------
        result: str
            The name of the sigmoid kernel.
        """
        return "sigmoid"

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, SupportVectorMachineRegressor.Kernel.Sigmoid):
            return NotImplemented
        return True

    __hash__ = SupportVectorMachineKernel.__hash__

__hash__ = SupportVectorMachineKernel.__hash__ class-attribute instance-attribute

__eq__(other)

Source code in src/safeds/ml/classical/regression/_support_vector_machine.py
def __eq__(self, other: object) -> bool:
    if not isinstance(other, SupportVectorMachineRegressor.Kernel.Sigmoid):
        return NotImplemented
    return True

__hash__()

Source code in src/safeds/ml/classical/regression/_support_vector_machine.py
def __hash__(self) -> int:
    return _structural_hash(Regressor.__hash__(self), self._target_name, self._feature_names, self._c, self.kernel)

__init__(*, c=1.0, kernel=None)

Source code in src/safeds/ml/classical/regression/_support_vector_machine.py
def __init__(self, *, c: float = 1.0, kernel: SupportVectorMachineKernel | None = None) -> None:
    # Internal state
    self._wrapped_regressor: sk_SVR | None = None
    self._feature_names: list[str] | None = None
    self._target_name: str | None = None

    # Hyperparameters
    if c <= 0:
        raise OutOfBoundsError(c, name="c", lower_bound=OpenBound(0))
    self._c = c
    self._kernel = kernel

fit(training_set)

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 SupportVectorMachineRegressor

The fitted regressor.

Raises:

Type Description
LearningError

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

UntaggedTableError

If the table is untagged.

NonNumericColumnError

If the training data contains non-numerical values.

MissingValuesColumnError

If the training data contains missing values.

DatasetMissesDataError

If the training data contains no rows.

Source code in src/safeds/ml/classical/regression/_support_vector_machine.py
def fit(self, training_set: TaggedTable) -> SupportVectorMachineRegressor:
    """
    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 : SupportVectorMachineRegressor
        The fitted regressor.

    Raises
    ------
    LearningError
        If the training data contains invalid values or if the training failed.
    UntaggedTableError
        If the table is untagged.
    NonNumericColumnError
        If the training data contains non-numerical values.
    MissingValuesColumnError
        If the training data contains missing values.
    DatasetMissesDataError
        If the training data contains no rows.
    """
    wrapped_regressor = self._get_sklearn_regressor()
    fit(wrapped_regressor, training_set)

    result = SupportVectorMachineRegressor(c=self._c, kernel=self._kernel)
    result._wrapped_regressor = wrapped_regressor
    result._feature_names = training_set.features.column_names
    result._target_name = training_set.target.name

    return result

is_fitted()

Check if the regressor is fitted.

Returns:

Name Type Description
is_fitted bool

Whether the regressor is fitted.

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

    Returns
    -------
    is_fitted : bool
        Whether the regressor is fitted.
    """
    return self._wrapped_regressor is not None

predict(dataset)

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.

NonNumericColumnError

If the dataset contains non-numerical values.

MissingValuesColumnError

If the dataset contains missing values.

DatasetMissesDataError

If the dataset contains no rows.

Source code in src/safeds/ml/classical/regression/_support_vector_machine.py
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.
    NonNumericColumnError
        If the dataset contains non-numerical values.
    MissingValuesColumnError
        If the dataset contains missing values.
    DatasetMissesDataError
        If the dataset contains no rows.
    """
    return predict(self._wrapped_regressor, dataset, self._feature_names, self._target_name)