Skip to content
Merged
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
1 change: 0 additions & 1 deletion examples/example_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def create_plot(scores):
verbose=10,
scoring=["r2", "neg_mean_squared_error"],
return_estimator=True,
fit_params={"cleanup": True},
)

create_plot(scores)
Expand Down
1 change: 0 additions & 1 deletion examples/example_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
verbose=10,
scoring=["r2", "neg_mean_squared_error"],
return_estimator=True,
fit_params={"cleanup": True},
)

log_scores(scores)
1 change: 0 additions & 1 deletion examples/example_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
verbose=10,
scoring=["r2", "neg_mean_squared_error"],
return_estimator=True,
fit_params={"cleanup": True},
)

log_scores(scores)
5 changes: 2 additions & 3 deletions examples/example_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
elitist_ratio=0.2,
random_state=random_state,
n_jobs=1,
mutation=suprb.optimizer.solution.ga.mutation.BitFlips(mutation_rate=0.1),
crossover=suprb.optimizer.solution.ga.crossover.NPoint(crossover_rate=0.9, n=2),
mutation=suprb.optimizer.solution.ga.mutation.BitFlips(),
crossover=suprb.optimizer.solution.ga.crossover.NPoint(n=2),
selection=suprb.optimizer.solution.ga.selection.Tournament(k=6),
init=suprb.solution.initialization.RandomInit(
mixing=suprb.solution.mixing_model.ErrorExperienceHeuristic(),
Expand All @@ -71,7 +71,6 @@
verbose=10,
scoring=["r2", "neg_mean_squared_error"],
return_estimator=True,
fit_params={"cleanup": True},
)

log_scores(scores)
17 changes: 5 additions & 12 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
numpy~=1.22.4
scipy~=1.7.1
matplotlib~=3.4.3
scikit-learn~=1.0.1
scikit-optimize~=0.9
pandas~=1.3.4
mlflow~=1.21.0
hypothesis~=6.24.2
joblib~=1.1.0
tqdm~=4.62.3
pytest~=6.2.5
protobuf~=3.20.0
matplotlib==3.10.3
pandas==2.3.0
pytest==8.4.1
scikit-learn==1.7.0
tqdm==4.67.1
2 changes: 1 addition & 1 deletion suprb/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def _more_str_attributes(self) -> dict:
return {}


class BaseRegressor(BaseEstimator, RegressorMixin, metaclass=ABCMeta):
Comment thread
heidmic marked this conversation as resolved.
class BaseRegressor(RegressorMixin, BaseEstimator, metaclass=ABCMeta):
"""A base (composite) Regressor."""

is_fitted_: bool
Expand Down
26 changes: 18 additions & 8 deletions suprb/suprb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import numpy as np
from sklearn import clone
from sklearn.utils import check_X_y
from sklearn.utils.validation import check_is_fitted, check_array
from sklearn.utils.validation import check_is_fitted, check_array, validate_data


from .base import BaseRegressor
from .exceptions import PopulationEmptyWarning
Expand All @@ -22,6 +23,18 @@


class SupRB(BaseRegressor):
def __sklearn_tags__(self):
Comment thread
heidmic marked this conversation as resolved.
tags = super().__sklearn_tags__()
tags.target_tags.single_output = False
tags.non_deterministic = True
return tags

def _more_tags(self):
# additional or override tags
return {
# 'some_tag': True,
}

"""The multi-solution batch learning LCS developed by the Organic Computing group at Universität Augsburg.

Parameters
Expand Down Expand Up @@ -150,8 +163,7 @@ def fit(self, X: np.ndarray, y: np.ndarray, cleanup=False):
self.elitist_.complexity_ = 99999

# Check that x and y have correct shape
X, y = check_X_y(X, y, dtype="float64", y_numeric=True)
y = check_array(y, ensure_2d=False, dtype="float64")
X, y = validate_data(self, X, y, ensure_2d=True)

# Init sklearn interface
self.n_features_in_ = X.shape[1]
Expand Down Expand Up @@ -273,11 +285,9 @@ def _compose_solution(self, X: np.ndarray, y: np.ndarray):
# Optimize
self.solution_composition_.optimize(X, y)

def predict(self, X: np.ndarray):
# Check is fit had been called
check_is_fitted(self, ["is_fitted_"])
# Input validation
X = check_array(X)
def predict(self, X):
check_is_fitted(self)
X = validate_data(self, X, ensure_2d=True, reset=False)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a change in sklearn guidelines?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also part of https://scikit-learn.org/stable/developers/develop.html and shall be used in the newer version


if hasattr(self, "is_error_") and self.is_error_:
return [0] * len(X)
Expand Down
26 changes: 20 additions & 6 deletions tests/test_solution.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from sklearn.utils.estimator_checks import check_estimator
from sklearn.utils.estimator_checks import check_estimator, _regression_dataset

import suprb
import suprb.logging.stdout
Expand All @@ -18,55 +18,69 @@ class TestSolution(unittest.TestCase):

def test_check_ga(self):
estimator = suprb.SupRB(
n_iter=1,
n_iter=4,
rule_discovery=ES1xLambda(n_iter=4, lmbda=1, delay=2),
solution_composition=GeneticAlgorithm(n_iter=2, population_size=2),
logger=suprb.logging.stdout.StdoutLogger(),
verbose=10,
)

X, y = _regression_dataset()
estimator.fit(X, y)

check_estimator(estimator)

def test_check_saga1(self):
estimator = suprb.SupRB(
n_iter=1,
n_iter=4,
rule_discovery=ES1xLambda(n_iter=4, lmbda=1, delay=2),
solution_composition=SelfAdaptingGeneticAlgorithm1(n_iter=2, population_size=2),
logger=suprb.logging.stdout.StdoutLogger(),
verbose=10,
)
X, y = _regression_dataset()
estimator.fit(X, y)

check_estimator(estimator)

def test_check_saga2(self):
estimator = suprb.SupRB(
n_iter=1,
n_iter=4,
rule_discovery=ES1xLambda(n_iter=4, lmbda=1, delay=2),
solution_composition=SelfAdaptingGeneticAlgorithm2(n_iter=2, population_size=2),
logger=suprb.logging.stdout.StdoutLogger(),
verbose=10,
)

X, y = _regression_dataset()
estimator.fit(X, y)

check_estimator(estimator)

def test_check_saga3(self):
estimator = suprb.SupRB(
n_iter=1,
n_iter=4,
rule_discovery=ES1xLambda(n_iter=4, lmbda=1, delay=2),
solution_composition=SelfAdaptingGeneticAlgorithm3(n_iter=2, population_size=2),
logger=suprb.logging.stdout.StdoutLogger(),
verbose=10,
)

X, y = _regression_dataset()
estimator.fit(X, y)

check_estimator(estimator)

def test_check_sas(self):
estimator = suprb.SupRB(
n_iter=1,
n_iter=4,
rule_discovery=ES1xLambda(n_iter=4, lmbda=1, delay=2),
solution_composition=SasGeneticAlgorithm(n_iter=2, initial_population_size=2),
logger=suprb.logging.stdout.StdoutLogger(),
verbose=10,
)

X, y = _regression_dataset()
estimator.fit(X, y)

check_estimator(estimator)
7 changes: 5 additions & 2 deletions tests/test_suprb.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
import numpy as np

from sklearn.utils.estimator_checks import check_estimator
from sklearn.utils.estimator_checks import check_estimator, _regression_dataset

import suprb
import suprb.logging.stdout
Expand Down Expand Up @@ -35,13 +35,16 @@ def test_check_estimator(self):

# Low n_iter for speed. Still takes forever though.
estimator = suprb.SupRB(
n_iter=1,
n_iter=4,
rule_discovery=ES1xLambda(n_iter=4, lmbda=1, delay=2),
solution_composition=suprb.optimizer.solution.ga.GeneticAlgorithm(n_iter=2, population_size=2),
logger=suprb.logging.stdout.StdoutLogger(),
verbose=10,
)

X, y = _regression_dataset()
estimator.fit(X, y)

check_estimator(estimator)

def test_early_stopping(self):
Expand Down