Skip to content

Commit

Permalink
complete restructure of ml-test-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBlanke committed May 26, 2024
1 parent f10d867 commit 3f79f6e
Show file tree
Hide file tree
Showing 18 changed files with 243 additions and 132 deletions.
5 changes: 2 additions & 3 deletions src/surfaces/test_functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
# Email: [email protected]
# License: MIT License

from .mathematical import (
mathematical_functions,
)
from .mathematical import mathematical_functions

from .machine_learning import machine_learning_functions


Expand Down
10 changes: 6 additions & 4 deletions src/surfaces/test_functions/machine_learning/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
# License: MIT License


from .tabular_classifiers import KNeighborsClassifierFunction
from .tabular_regressors import (
GradientBoostingRegressorFunction,
from .tabular import (
KNeighborsClassifierFunction,
KNeighborsRegressorFunction,
GradientBoostingRegressorFunction,
)


__all__ = [
"KNeighborsClassifierFunction",
"GradientBoostingRegressorFunction",
"KNeighborsRegressorFunction",
"GradientBoostingRegressorFunction",
]


machine_learning_functions = [
KNeighborsClassifierFunction,
GradientBoostingRegressorFunction,
Expand Down

This file was deleted.

13 changes: 13 additions & 0 deletions src/surfaces/test_functions/machine_learning/tabular/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Author: Simon Blanke
# Email: [email protected]
# License: MIT License

from .classification import KNeighborsClassifierFunction
from .regression import KNeighborsRegressorFunction, GradientBoostingRegressorFunction


__all__ = [
"KNeighborsClassifierFunction",
"KNeighborsRegressorFunction",
"GradientBoostingRegressorFunction",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Author: Simon Blanke
# Email: [email protected]
# License: MIT License


from .._base_machine_learning import MachineLearningFunction


class BaseTabular(MachineLearningFunction):
def __init__(self, metric, sleep, evaluate_from_data):
super().__init__(metric, sleep, evaluate_from_data)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Author: Simon Blanke
# Email: [email protected]
# License: MIT License

from .test_functions import (
KNeighborsClassifierFunction,
)


__all__ = [
"KNeighborsClassifierFunction",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Author: Simon Blanke
# Email: [email protected]
# License: MIT License


from .._base_tabular import BaseTabular


class BaseClassification(BaseTabular):
metric = "accuracy" # called 'scoring' in sklearn

def __init__(self, metric, sleep, evaluate_from_data):
super().__init__(metric, sleep, evaluate_from_data)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Author: Simon Blanke
# Email: [email protected]
# License: MIT License


from .k_neighbors_classifier import KNeighborsClassifierFunction


__all__ = [
"KNeighborsClassifierFunction",
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from sklearn.neighbors import KNeighborsClassifier

from sklearn.model_selection import cross_val_score
from .datasets import digits_data, wine_data, iris_data
from ..datasets import digits_data, wine_data, iris_data

from .base_machine_learning_function import MachineLearningFunction
from .._base_classification import BaseClassification


class KNeighborsClassifierFunction(MachineLearningFunction):
class KNeighborsClassifierFunction(BaseClassification):
name = "KNeighbors Classifier Function"
_name_ = "k_neighbors_classifier"
__name__ = "KNeighborsClassifierFunction"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Author: Simon Blanke
# Email: [email protected]
# License: MIT License

from .test_functions import (
KNeighborsRegressorFunction,
GradientBoostingRegressorFunction,
)


__all__ = [
"KNeighborsRegressorFunction",
"GradientBoostingRegressorFunction",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Author: Simon Blanke
# Email: [email protected]
# License: MIT License


from .._base_tabular import BaseTabular


class BaseRegression(BaseTabular):
metric = "r2" # called 'scoring' in sklearn

def __init__(self, metric, sleep, evaluate_from_data):
super().__init__(metric, sleep, evaluate_from_data)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Author: Simon Blanke
# Email: [email protected]
# License: MIT License


from .k_neighbors_regressor import KNeighborsRegressorFunction
from .gradient_boosting_regressor import GradientBoostingRegressorFunction


__all__ = [
"KNeighborsRegressorFunction",
"GradientBoostingRegressorFunction",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Author: Simon Blanke
# Email: [email protected]
# License: MIT License


import numpy as np

from sklearn.ensemble import GradientBoostingRegressor

from sklearn.model_selection import cross_val_score
from ..datasets import diabetes_data

from .._base_regression import BaseRegression


class GradientBoostingRegressorFunction(BaseRegression):
name = "Gradient Boosting Regressor Function"
_name_ = "gradient_boosting_regressor"
__name__ = "GradientBoostingRegressorFunction"

para_names = ["n_estimators", "max_depth", "cv", "dataset"]

n_estimators_default = list(np.arange(3, 150, 5))
max_depth_default = list(np.arange(2, 25))
cv_default = [2, 3, 4, 5, 8, 10]
dataset_default = [diabetes_data]

def __init__(
self,
metric=None,
sleep=0,
evaluate_from_data=False,
):
super().__init__(metric, sleep, evaluate_from_data)

def search_space(
self,
n_estimators: list = None,
max_depth: list = None,
cv: list = None,
dataset: list = None,
):
search_space: dict = {}

search_space["n_estimators"] = (
self.n_estimators_default if n_estimators is None else n_estimators
)
search_space["max_depth"] = (
self.max_depth_default if max_depth is None else max_depth
)
search_space["cv"] = self.cv_default if cv is None else cv
search_space["dataset"] = self.dataset_default if dataset is None else dataset

return search_space

def create_objective_function(self):
def gradient_boosting_regressor(params):
knc = GradientBoostingRegressor(
n_estimators=params["n_estimators"],
max_depth=params["max_depth"],
)
X, y = params["dataset"]()
scores = cross_val_score(knc, X, y, cv=params["cv"], scoring=self.metric)
return scores.mean()

self.pure_objective_function = gradient_boosting_regressor
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Author: Simon Blanke
# Email: [email protected]
# License: MIT License


import numpy as np

from sklearn.neighbors import KNeighborsRegressor

from sklearn.model_selection import cross_val_score
from ..datasets import diabetes_data

from .._base_regression import BaseRegression


class KNeighborsRegressorFunction(BaseRegression):
name = "KNeighbors Regressor Function"
_name_ = "k_neighbors_regressor"
__name__ = "KNeighborsRegressorFunction"

para_names = ["n_neighbors", "algorithm", "cv", "dataset"]

n_neighbors_default = list(np.arange(3, 150, 5))
algorithm_default = ["auto", "ball_tree", "kd_tree", "brute"]
cv_default = [2, 3, 4, 5, 8, 10]
dataset_default = [diabetes_data]

def __init__(
self,
metric=None,
sleep=0,
evaluate_from_data=False,
):
super().__init__(metric, sleep, evaluate_from_data)

def search_space(
self,
n_neighbors: list = None,
algorithm: list = None,
cv: list = None,
dataset: list = None,
):
search_space: dict = {}

search_space["n_neighbors"] = (
self.n_neighbors_default if n_neighbors is None else n_neighbors
)
search_space["algorithm"] = (
self.algorithm_default if algorithm is None else algorithm
)
search_space["cv"] = self.cv_default if cv is None else cv
search_space["dataset"] = self.dataset_default if dataset is None else dataset

return search_space

def create_objective_function(self):
def k_neighbors_regressor(params):
knc = KNeighborsRegressor(
n_neighbors=params["n_neighbors"],
algorithm=params["algorithm"],
)
X, y = params["dataset"]()
scores = cross_val_score(knc, X, y, cv=params["cv"], scoring=self.metric)
return scores.mean()

self.pure_objective_function = k_neighbors_regressor
Loading

0 comments on commit 3f79f6e

Please sign in to comment.