Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 3 additions & 5 deletions flaml/automl/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2308,11 +2308,9 @@ def config2params(self, config: dict) -> dict:

def __init__(self, task="binary", **config):
super().__init__(task, **config)
self.params.update(
{
"random_state": config.get("random_seed", 10242048),
}
)
random_seed = self.params.pop("random_seed", config.get("random_seed", 10242048))
if "random_state" not in self.params:
self.params["random_state"] = random_seed
assert self._task.is_classification(), "LinearSVC for classification task only"
self.estimator_class = LinearSVC

Expand Down
51 changes: 51 additions & 0 deletions test/automl/test_svc_clone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import numpy as np
import pytest
from sklearn.base import clone
from sklearn.datasets import make_classification
from sklearn.ensemble import StackingClassifier
from sklearn.linear_model import LogisticRegression

from flaml.automl.model import SVCEstimator


@pytest.mark.parametrize("random_state", [None, 7, np.int64(13), np.random.RandomState(17)])
def test_svc_clone_preserves_random_state(random_state):
if isinstance(random_state, np.random.RandomState):
random_state.normal()
expected_state = random_state.get_state()
estimator = SVCEstimator(random_state=random_state)
cloned = clone(estimator)
actual = cloned.get_params()["random_state"]
if isinstance(random_state, np.random.RandomState):
np.testing.assert_equal(actual.get_state(), expected_state)
np.testing.assert_equal(random_state.get_state(), expected_state)
else:
assert actual == random_state
assert "random_seed" not in cloned.get_params()


@pytest.mark.parametrize(
"config, expected",
[({}, 10242048), ({"random_seed": 23}, 23), ({"random_seed": 23, "random_state": 7}, 7)],
)
def test_svc_random_seed_alias(config, expected):
estimator = SVCEstimator(**config)
cloned = clone(estimator)
assert estimator.get_params()["random_state"] == expected
assert cloned.get_params()["random_state"] == expected
assert "random_seed" not in estimator.get_params()
X, y = make_classification(n_samples=40, n_features=4, random_state=0)
cloned.fit(X, y)
assert cloned.model.random_state == expected


def test_svc_stacking():
X, y = make_classification(n_samples=60, n_features=4, random_state=0)
stacking = StackingClassifier(
estimators=[("svc", SVCEstimator(random_state=np.int64(7)))],
final_estimator=LogisticRegression(),
cv=2,
)
stacking.fit(X, y)
assert stacking.named_estimators_["svc"].model.random_state == 7
assert stacking.predict(X).shape == y.shape
Loading