Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added parameter c to SupportVectorMachines #267

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,30 @@


class SupportVectorMachine(Classifier):
"""Support vector machine."""
"""
Support vector machine.

def __init__(self) -> None:
Parameters
----------
c: float
The strength of regularization
alex-senger marked this conversation as resolved.
Show resolved Hide resolved

Raises
------
ValueError
If the strength of regularization is less than or equal to 0.
alex-senger marked this conversation as resolved.
Show resolved Hide resolved
"""

def __init__(self, c: float = 1.0) -> None:
# Internal state
self._wrapped_classifier: sk_SVC | None = None
self._feature_names: list[str] | None = None
self._target_name: str | None = None

if c <= 0:
raise ValueError("The strength of regularization must be strictly positive.")
alex-senger marked this conversation as resolved.
Show resolved Hide resolved
self._c = c

def fit(self, training_set: TaggedTable) -> SupportVectorMachine:
"""
Create a copy of this classifier and fit it with the given training data.
Expand All @@ -42,10 +58,10 @@ def fit(self, training_set: TaggedTable) -> SupportVectorMachine:
LearningError
If the training data contains invalid values or if the training failed.
"""
wrapped_classifier = sk_SVC()
wrapped_classifier = sk_SVC(C=self._c)
fit(wrapped_classifier, training_set)

result = SupportVectorMachine()
result = SupportVectorMachine(self._c)
result._wrapped_classifier = wrapped_classifier
result._feature_names = training_set.features.column_names
result._target_name = training_set.target.name
Expand Down
24 changes: 20 additions & 4 deletions src/safeds/ml/classical/regression/_support_vector_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,30 @@


class SupportVectorMachine(Regressor):
alex-senger marked this conversation as resolved.
Show resolved Hide resolved
"""Support vector machine."""
"""
Support vector machine.

def __init__(self) -> None:
Parameters
----------
c: float
The strength of regularization
alex-senger marked this conversation as resolved.
Show resolved Hide resolved

Raises
------
ValueError
If the strength of regularization is less than or equal to 0.
alex-senger marked this conversation as resolved.
Show resolved Hide resolved
"""

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

if c <= 0:
raise ValueError("The strength of regularization must be strictly positive.")
alex-senger marked this conversation as resolved.
Show resolved Hide resolved
self._c = c

def fit(self, training_set: TaggedTable) -> SupportVectorMachine:
"""
Create a copy of this regressor and fit it with the given training data.
Expand All @@ -42,10 +58,10 @@ def fit(self, training_set: TaggedTable) -> SupportVectorMachine:
LearningError
If the training data contains invalid values or if the training failed.
"""
wrapped_regressor = sk_SVR()
wrapped_regressor = sk_SVR(C=self._c)
fit(wrapped_regressor, training_set)

result = SupportVectorMachine()
result = SupportVectorMachine(self._c)
result._wrapped_regressor = wrapped_regressor
result._feature_names = training_set.features.column_names
result._target_name = training_set.target.name
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest
from safeds.data.tabular.containers import Table, TaggedTable
from safeds.ml.classical.classification import SupportVectorMachine


@pytest.fixture()
def training_set() -> TaggedTable:
table = Table.from_dict({"col1": [1, 2, 3, 4], "col2": [1, 2, 3, 4]})
return table.tag_columns(target_name="col1", feature_names=["col2"])


class TestC:
def test_should_be_passed_to_fitted_model(self, training_set: TaggedTable) -> None:
fitted_model = SupportVectorMachine(c=2).fit(training_set=training_set)
assert fitted_model._c == 2

def test_should_be_passed_to_sklearn(self, training_set: TaggedTable) -> None:
fitted_model = SupportVectorMachine(c=2).fit(training_set)
assert fitted_model._wrapped_classifier is not None
assert fitted_model._wrapped_classifier.C == 2

def test_should_raise_if_less_than_or_equal_to_0(self) -> None:
with pytest.raises(ValueError, match="The strength of regularization must be strictly positive."):
alex-senger marked this conversation as resolved.
Show resolved Hide resolved
SupportVectorMachine(c=-1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest
from safeds.data.tabular.containers import Table, TaggedTable
from safeds.ml.classical.regression import SupportVectorMachine


@pytest.fixture()
def training_set() -> TaggedTable:
table = Table.from_dict({"col1": [1, 2, 3, 4], "col2": [1, 2, 3, 4]})
return table.tag_columns(target_name="col1", feature_names=["col2"])


class TestC:
def test_should_be_passed_to_fitted_model(self, training_set: TaggedTable) -> None:
fitted_model = SupportVectorMachine(c=2).fit(training_set=training_set)
assert fitted_model._c == 2

def test_should_be_passed_to_sklearn(self, training_set: TaggedTable) -> None:
fitted_model = SupportVectorMachine(c=2).fit(training_set)
assert fitted_model._wrapped_regressor is not None
assert fitted_model._wrapped_regressor.C == 2

def test_should_raise_if_less_than_or_equal_to_0(self) -> None:
with pytest.raises(ValueError, match="The strength of regularization must be strictly positive."):
alex-senger marked this conversation as resolved.
Show resolved Hide resolved
SupportVectorMachine(c=-1)