Skip to content

SupportVectorMachine

Bases: Classifier

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/classification/_support_vector_machine.py
class SupportVectorMachine(Classifier):
    """
    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 __init__(self, *, c: float = 1.0, kernel: SupportVectorMachineKernel | None = None) -> None:
        # Internal state
        self._wrapped_classifier: sk_SVC | 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"

        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"

        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"

        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 _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, SupportVectorMachine.Kernel.Linear):
            return "linear"
        elif isinstance(self.kernel, SupportVectorMachine.Kernel.Polynomial):
            return "poly"
        elif isinstance(self.kernel, SupportVectorMachine.Kernel.Sigmoid):
            return "sigmoid"
        elif isinstance(self.kernel, SupportVectorMachine.Kernel.RadialBasisFunction):
            return "rbf"
        else:
            raise TypeError("Invalid kernel type.")

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

        This classifier is not modified.

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

        Returns
        -------
        fitted_classifier : SupportVectorMachine
            The fitted classifier.

        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_classifier = self._get_sklearn_classifier()
        fit(wrapped_classifier, training_set)

        result = SupportVectorMachine(c=self._c, kernel=self._kernel)
        result._wrapped_classifier = wrapped_classifier
        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_classifier, dataset, self._feature_names, self._target_name)

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

        Returns
        -------
        is_fitted : bool
            Whether the classifier is fitted.
        """
        return self._wrapped_classifier is not None

    def _get_sklearn_classifier(self) -> ClassifierMixin:
        """
        Return a new wrapped Classifier from sklearn.

        Returns
        -------
        wrapped_classifier: ClassifierMixin
            The sklearn Classifier.
        """
        return sk_SVC(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/classification/_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"

    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"

    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"

    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"

Linear

Bases: SupportVectorMachineKernel

Source code in src/safeds/ml/classical/classification/_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"

get_sklearn_kernel()

Get the name of the linear kernel.

Returns:

Name Type Description
result str

The name of the linear kernel.

Source code in src/safeds/ml/classical/classification/_support_vector_machine.py
def get_sklearn_kernel(self) -> str:
    """
    Get the name of the linear kernel.

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

Polynomial

Bases: SupportVectorMachineKernel

Source code in src/safeds/ml/classical/classification/_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"

__init__(degree)

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

get_sklearn_kernel()

Get the name of the polynomial kernel.

Returns:

Name Type Description
result str

The name of the polynomial kernel.

Source code in src/safeds/ml/classical/classification/_support_vector_machine.py
def get_sklearn_kernel(self) -> str:
    """
    Get the name of the polynomial kernel.

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

RadialBasisFunction

Bases: SupportVectorMachineKernel

Source code in src/safeds/ml/classical/classification/_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"

get_sklearn_kernel()

Get the name of the radial basis function (RBF) kernel.

Returns:

Name Type Description
result str

The name of the RBF kernel.

Source code in src/safeds/ml/classical/classification/_support_vector_machine.py
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"

Sigmoid

Bases: SupportVectorMachineKernel

Source code in src/safeds/ml/classical/classification/_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"

get_sklearn_kernel()

Get the name of the sigmoid kernel.

Returns:

Name Type Description
result str

The name of the sigmoid kernel.

Source code in src/safeds/ml/classical/classification/_support_vector_machine.py
def get_sklearn_kernel(self) -> str:
    """
    Get the name of the sigmoid kernel.

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

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

Source code in src/safeds/ml/classical/classification/_support_vector_machine.py
def __init__(self, *, c: float = 1.0, kernel: SupportVectorMachineKernel | None = None) -> None:
    # Internal state
    self._wrapped_classifier: sk_SVC | 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 classifier and fit it with the given training data.

This classifier 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_classifier SupportVectorMachine

The fitted classifier.

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/classification/_support_vector_machine.py
def fit(self, training_set: TaggedTable) -> SupportVectorMachine:
    """
    Create a copy of this classifier and fit it with the given training data.

    This classifier is not modified.

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

    Returns
    -------
    fitted_classifier : SupportVectorMachine
        The fitted classifier.

    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_classifier = self._get_sklearn_classifier()
    fit(wrapped_classifier, training_set)

    result = SupportVectorMachine(c=self._c, kernel=self._kernel)
    result._wrapped_classifier = wrapped_classifier
    result._feature_names = training_set.features.column_names
    result._target_name = training_set.target.name

    return result

is_fitted()

Check if the classifier is fitted.

Returns:

Name Type Description
is_fitted bool

Whether the classifier is fitted.

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

    Returns
    -------
    is_fitted : bool
        Whether the classifier is fitted.
    """
    return self._wrapped_classifier 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/classification/_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_classifier, dataset, self._feature_names, self._target_name)