From 3f79f6eec4b89718792e731408aa83cdbc7c661f Mon Sep 17 00:00:00 2001 From: Simon Blanke Date: Sun, 26 May 2024 10:34:32 +0200 Subject: [PATCH] complete restructure of ml-test-functions --- src/surfaces/test_functions/__init__.py | 5 +- .../machine_learning/__init__.py | 10 +- ..._function.py => _base_machine_learning.py} | 0 .../machine_learning/datasets/__init__.py | 7 -- .../machine_learning/tabular/__init__.py | 13 ++ .../machine_learning/tabular/_base_tabular.py | 11 ++ .../tabular/classification/__init__.py | 12 ++ .../classification/_base_classification.py | 13 ++ .../classification/datasets.py} | 0 .../classification/test_functions/__init__.py | 11 ++ .../test_functions/k_neighbors_classifier.py} | 6 +- .../tabular/regression/__init__.py | 14 +++ .../tabular/regression/_base_regression.py | 13 ++ .../regression/datasets.py} | 0 .../regression/test_functions/__init__.py | 13 ++ .../gradient_boosting_regressor.py | 66 ++++++++++ .../test_functions/k_neighbors_regressor.py | 66 ++++++++++ .../machine_learning/tabular_regressors.py | 115 ------------------ 18 files changed, 243 insertions(+), 132 deletions(-) rename src/surfaces/test_functions/machine_learning/{base_machine_learning_function.py => _base_machine_learning.py} (100%) delete mode 100644 src/surfaces/test_functions/machine_learning/datasets/__init__.py create mode 100644 src/surfaces/test_functions/machine_learning/tabular/_base_tabular.py create mode 100644 src/surfaces/test_functions/machine_learning/tabular/classification/_base_classification.py rename src/surfaces/test_functions/machine_learning/{datasets/tabular_classification.py => tabular/classification/datasets.py} (100%) create mode 100644 src/surfaces/test_functions/machine_learning/tabular/classification/test_functions/__init__.py rename src/surfaces/test_functions/machine_learning/{tabular_classifiers.py => tabular/classification/test_functions/k_neighbors_classifier.py} (90%) create mode 100644 src/surfaces/test_functions/machine_learning/tabular/regression/_base_regression.py rename src/surfaces/test_functions/machine_learning/{datasets/tabular_regression.py => tabular/regression/datasets.py} (100%) create mode 100644 src/surfaces/test_functions/machine_learning/tabular/regression/test_functions/__init__.py create mode 100644 src/surfaces/test_functions/machine_learning/tabular/regression/test_functions/gradient_boosting_regressor.py create mode 100644 src/surfaces/test_functions/machine_learning/tabular/regression/test_functions/k_neighbors_regressor.py delete mode 100644 src/surfaces/test_functions/machine_learning/tabular_regressors.py diff --git a/src/surfaces/test_functions/__init__.py b/src/surfaces/test_functions/__init__.py index 7202c55..332b804 100644 --- a/src/surfaces/test_functions/__init__.py +++ b/src/surfaces/test_functions/__init__.py @@ -2,9 +2,8 @@ # Email: simon.blanke@yahoo.com # License: MIT License -from .mathematical import ( - mathematical_functions, -) +from .mathematical import mathematical_functions + from .machine_learning import machine_learning_functions diff --git a/src/surfaces/test_functions/machine_learning/__init__.py b/src/surfaces/test_functions/machine_learning/__init__.py index 8ffe851..0d41be4 100644 --- a/src/surfaces/test_functions/machine_learning/__init__.py +++ b/src/surfaces/test_functions/machine_learning/__init__.py @@ -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, diff --git a/src/surfaces/test_functions/machine_learning/base_machine_learning_function.py b/src/surfaces/test_functions/machine_learning/_base_machine_learning.py similarity index 100% rename from src/surfaces/test_functions/machine_learning/base_machine_learning_function.py rename to src/surfaces/test_functions/machine_learning/_base_machine_learning.py diff --git a/src/surfaces/test_functions/machine_learning/datasets/__init__.py b/src/surfaces/test_functions/machine_learning/datasets/__init__.py deleted file mode 100644 index 2b35f0e..0000000 --- a/src/surfaces/test_functions/machine_learning/datasets/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -# Author: Simon Blanke -# Email: simon.blanke@yahoo.com -# License: MIT License - - -from .tabular_classification import digits_data, wine_data, iris_data -from .tabular_regression import diabetes_data diff --git a/src/surfaces/test_functions/machine_learning/tabular/__init__.py b/src/surfaces/test_functions/machine_learning/tabular/__init__.py index e69de29..3ef393b 100644 --- a/src/surfaces/test_functions/machine_learning/tabular/__init__.py +++ b/src/surfaces/test_functions/machine_learning/tabular/__init__.py @@ -0,0 +1,13 @@ +# Author: Simon Blanke +# Email: simon.blanke@yahoo.com +# License: MIT License + +from .classification import KNeighborsClassifierFunction +from .regression import KNeighborsRegressorFunction, GradientBoostingRegressorFunction + + +__all__ = [ + "KNeighborsClassifierFunction", + "KNeighborsRegressorFunction", + "GradientBoostingRegressorFunction", +] diff --git a/src/surfaces/test_functions/machine_learning/tabular/_base_tabular.py b/src/surfaces/test_functions/machine_learning/tabular/_base_tabular.py new file mode 100644 index 0000000..daf5a5f --- /dev/null +++ b/src/surfaces/test_functions/machine_learning/tabular/_base_tabular.py @@ -0,0 +1,11 @@ +# Author: Simon Blanke +# Email: simon.blanke@yahoo.com +# 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) diff --git a/src/surfaces/test_functions/machine_learning/tabular/classification/__init__.py b/src/surfaces/test_functions/machine_learning/tabular/classification/__init__.py index e69de29..093b352 100644 --- a/src/surfaces/test_functions/machine_learning/tabular/classification/__init__.py +++ b/src/surfaces/test_functions/machine_learning/tabular/classification/__init__.py @@ -0,0 +1,12 @@ +# Author: Simon Blanke +# Email: simon.blanke@yahoo.com +# License: MIT License + +from .test_functions import ( + KNeighborsClassifierFunction, +) + + +__all__ = [ + "KNeighborsClassifierFunction", +] diff --git a/src/surfaces/test_functions/machine_learning/tabular/classification/_base_classification.py b/src/surfaces/test_functions/machine_learning/tabular/classification/_base_classification.py new file mode 100644 index 0000000..31624b1 --- /dev/null +++ b/src/surfaces/test_functions/machine_learning/tabular/classification/_base_classification.py @@ -0,0 +1,13 @@ +# Author: Simon Blanke +# Email: simon.blanke@yahoo.com +# 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) diff --git a/src/surfaces/test_functions/machine_learning/datasets/tabular_classification.py b/src/surfaces/test_functions/machine_learning/tabular/classification/datasets.py similarity index 100% rename from src/surfaces/test_functions/machine_learning/datasets/tabular_classification.py rename to src/surfaces/test_functions/machine_learning/tabular/classification/datasets.py diff --git a/src/surfaces/test_functions/machine_learning/tabular/classification/test_functions/__init__.py b/src/surfaces/test_functions/machine_learning/tabular/classification/test_functions/__init__.py new file mode 100644 index 0000000..e22b0f3 --- /dev/null +++ b/src/surfaces/test_functions/machine_learning/tabular/classification/test_functions/__init__.py @@ -0,0 +1,11 @@ +# Author: Simon Blanke +# Email: simon.blanke@yahoo.com +# License: MIT License + + +from .k_neighbors_classifier import KNeighborsClassifierFunction + + +__all__ = [ + "KNeighborsClassifierFunction", +] diff --git a/src/surfaces/test_functions/machine_learning/tabular_classifiers.py b/src/surfaces/test_functions/machine_learning/tabular/classification/test_functions/k_neighbors_classifier.py similarity index 90% rename from src/surfaces/test_functions/machine_learning/tabular_classifiers.py rename to src/surfaces/test_functions/machine_learning/tabular/classification/test_functions/k_neighbors_classifier.py index b555199..ac44799 100644 --- a/src/surfaces/test_functions/machine_learning/tabular_classifiers.py +++ b/src/surfaces/test_functions/machine_learning/tabular/classification/test_functions/k_neighbors_classifier.py @@ -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" diff --git a/src/surfaces/test_functions/machine_learning/tabular/regression/__init__.py b/src/surfaces/test_functions/machine_learning/tabular/regression/__init__.py index e69de29..41d69d2 100644 --- a/src/surfaces/test_functions/machine_learning/tabular/regression/__init__.py +++ b/src/surfaces/test_functions/machine_learning/tabular/regression/__init__.py @@ -0,0 +1,14 @@ +# Author: Simon Blanke +# Email: simon.blanke@yahoo.com +# License: MIT License + +from .test_functions import ( + KNeighborsRegressorFunction, + GradientBoostingRegressorFunction, +) + + +__all__ = [ + "KNeighborsRegressorFunction", + "GradientBoostingRegressorFunction", +] diff --git a/src/surfaces/test_functions/machine_learning/tabular/regression/_base_regression.py b/src/surfaces/test_functions/machine_learning/tabular/regression/_base_regression.py new file mode 100644 index 0000000..280bf8e --- /dev/null +++ b/src/surfaces/test_functions/machine_learning/tabular/regression/_base_regression.py @@ -0,0 +1,13 @@ +# Author: Simon Blanke +# Email: simon.blanke@yahoo.com +# 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) diff --git a/src/surfaces/test_functions/machine_learning/datasets/tabular_regression.py b/src/surfaces/test_functions/machine_learning/tabular/regression/datasets.py similarity index 100% rename from src/surfaces/test_functions/machine_learning/datasets/tabular_regression.py rename to src/surfaces/test_functions/machine_learning/tabular/regression/datasets.py diff --git a/src/surfaces/test_functions/machine_learning/tabular/regression/test_functions/__init__.py b/src/surfaces/test_functions/machine_learning/tabular/regression/test_functions/__init__.py new file mode 100644 index 0000000..c6e8b77 --- /dev/null +++ b/src/surfaces/test_functions/machine_learning/tabular/regression/test_functions/__init__.py @@ -0,0 +1,13 @@ +# Author: Simon Blanke +# Email: simon.blanke@yahoo.com +# License: MIT License + + +from .k_neighbors_regressor import KNeighborsRegressorFunction +from .gradient_boosting_regressor import GradientBoostingRegressorFunction + + +__all__ = [ + "KNeighborsRegressorFunction", + "GradientBoostingRegressorFunction", +] diff --git a/src/surfaces/test_functions/machine_learning/tabular/regression/test_functions/gradient_boosting_regressor.py b/src/surfaces/test_functions/machine_learning/tabular/regression/test_functions/gradient_boosting_regressor.py new file mode 100644 index 0000000..ba2bba2 --- /dev/null +++ b/src/surfaces/test_functions/machine_learning/tabular/regression/test_functions/gradient_boosting_regressor.py @@ -0,0 +1,66 @@ +# Author: Simon Blanke +# Email: simon.blanke@yahoo.com +# 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 diff --git a/src/surfaces/test_functions/machine_learning/tabular/regression/test_functions/k_neighbors_regressor.py b/src/surfaces/test_functions/machine_learning/tabular/regression/test_functions/k_neighbors_regressor.py new file mode 100644 index 0000000..1a824f2 --- /dev/null +++ b/src/surfaces/test_functions/machine_learning/tabular/regression/test_functions/k_neighbors_regressor.py @@ -0,0 +1,66 @@ +# Author: Simon Blanke +# Email: simon.blanke@yahoo.com +# 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 diff --git a/src/surfaces/test_functions/machine_learning/tabular_regressors.py b/src/surfaces/test_functions/machine_learning/tabular_regressors.py deleted file mode 100644 index b74b10b..0000000 --- a/src/surfaces/test_functions/machine_learning/tabular_regressors.py +++ /dev/null @@ -1,115 +0,0 @@ -import numpy as np - -from sklearn.neighbors import KNeighborsRegressor -from sklearn.ensemble import GradientBoostingRegressor - -from sklearn.model_selection import cross_val_score -from .datasets import diabetes_data - -from .base_machine_learning_function import MachineLearningFunction - - -class KNeighborsRegressorFunction(MachineLearningFunction): - 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 - - -class GradientBoostingRegressorFunction(MachineLearningFunction): - 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