From 1c928d7729335ec8475e4dc2e70fa11b91e0ae1a Mon Sep 17 00:00:00 2001 From: jxnior01 Date: Sat, 17 Jun 2023 18:02:48 +0200 Subject: [PATCH 1/4] added docstrings to getter methods for hyperparameters and to methods of class Kernel --- .../ml/classical/classification/_ada_boost.py | 24 +++++++ .../classification/_gradient_boosting.py | 16 +++++ .../classification/_k_nearest_neighbors.py | 8 +++ .../classification/_random_forest.py | 8 +++ .../classification/_support_vector_machine.py | 62 +++++++++++++++++++ .../ml/classical/regression/_ada_boost.py | 24 +++++++ .../regression/_elastic_net_regression.py | 16 +++++ .../regression/_gradient_boosting.py | 16 +++++ .../regression/_k_nearest_neighbors.py | 8 +++ .../classical/regression/_lasso_regression.py | 8 +++ .../ml/classical/regression/_random_forest.py | 8 +++ .../classical/regression/_ridge_regression.py | 8 +++ .../regression/_support_vector_machine.py | 61 ++++++++++++++++++ 13 files changed, 267 insertions(+) diff --git a/src/safeds/ml/classical/classification/_ada_boost.py b/src/safeds/ml/classical/classification/_ada_boost.py index 86ffde895..6cde8e4f2 100644 --- a/src/safeds/ml/classical/classification/_ada_boost.py +++ b/src/safeds/ml/classical/classification/_ada_boost.py @@ -60,14 +60,38 @@ def __init__( @property def learner(self) -> Classifier | None: + """ + Get the base learner used for training the ensemble. + + Returns + ------- + result: Classifier | None + The base learner. + """ return self._learner @property def maximum_number_of_learners(self) -> int: + """ + Get the maximum number of learners in the ensemble. + + Returns + ------- + result: int + The maximum number of learners. + """ return self._maximum_number_of_learners @property def learning_rate(self) -> float: + """ + Get the learning rate. + + Returns + ------- + result: float + The learning rate. + """ return self._learning_rate def fit(self, training_set: TaggedTable) -> AdaBoost: diff --git a/src/safeds/ml/classical/classification/_gradient_boosting.py b/src/safeds/ml/classical/classification/_gradient_boosting.py index 17b3a4205..23804b3ca 100644 --- a/src/safeds/ml/classical/classification/_gradient_boosting.py +++ b/src/safeds/ml/classical/classification/_gradient_boosting.py @@ -51,10 +51,26 @@ def __init__(self, *, number_of_trees: int = 100, learning_rate: float = 0.1) -> @property def number_of_trees(self) -> int: + """ + Get the number of trees (estimators) in the ensemble. + + Returns + ------- + result: int + The number of trees. + """ return self._number_of_trees @property def learning_rate(self) -> float: + """ + Get the learning rate. + + Returns + ------- + result: float + The learning rate. + """ return self._learning_rate def fit(self, training_set: TaggedTable) -> GradientBoosting: diff --git a/src/safeds/ml/classical/classification/_k_nearest_neighbors.py b/src/safeds/ml/classical/classification/_k_nearest_neighbors.py index a7eaa4ff0..d7ff20362 100644 --- a/src/safeds/ml/classical/classification/_k_nearest_neighbors.py +++ b/src/safeds/ml/classical/classification/_k_nearest_neighbors.py @@ -46,6 +46,14 @@ def __init__(self, number_of_neighbors: int) -> None: @property def number_of_neighbors(self) -> int: + """ + Get the number of neighbors used for interpolation. + + Returns + ------- + result: int + The number of neighbors. + """ return self._number_of_neighbors def fit(self, training_set: TaggedTable) -> KNearestNeighbors: diff --git a/src/safeds/ml/classical/classification/_random_forest.py b/src/safeds/ml/classical/classification/_random_forest.py index c50b063e0..850c5296b 100644 --- a/src/safeds/ml/classical/classification/_random_forest.py +++ b/src/safeds/ml/classical/classification/_random_forest.py @@ -43,6 +43,14 @@ def __init__(self, *, number_of_trees: int = 100) -> None: @property def number_of_trees(self) -> int: + """ + Get the number of trees used in the random forest. + + Returns + ------- + result: int + The number of trees. + """ return self._number_of_trees def fit(self, training_set: TaggedTable) -> RandomForest: diff --git a/src/safeds/ml/classical/classification/_support_vector_machine.py b/src/safeds/ml/classical/classification/_support_vector_machine.py index 11c5765d8..641aab306 100644 --- a/src/safeds/ml/classical/classification/_support_vector_machine.py +++ b/src/safeds/ml/classical/classification/_support_vector_machine.py @@ -59,15 +59,39 @@ def __init__(self, *, c: float = 1.0, kernel: SupportVectorMachineKernel | None @property def c(self) -> float: + """ + Get the regularization strength. + + Returns + ------- + result: float + The regularization strength. + """ return self._c @property def kernel(self) -> SupportVectorMachineKernel | None: + """ + Get the type of kernel used. + + Returns + ------- + result: SupportVectorMachineKernel | None + The type of kernel used. + """ return self._kernel class Kernel: class Linear(SupportVectorMachineKernel): def get_sklearn_kernel(self) -> str: + """ + Get the name of the linear kernel. + + Returns + ------- + result: str + The name of the linear kernel. + """ return "linear" class Polynomial(SupportVectorMachineKernel): @@ -77,17 +101,54 @@ def __init__(self, degree: int): self._degree = degree def get_sklearn_kernel(self) -> str: + """ + Get the name of the polynomial kernel. + + Returns + ------- + result: str + The name of the polynomial kernel. + """ return "poly" class Sigmoid(SupportVectorMachineKernel): def get_sklearn_kernel(self) -> str: + """ + Get the name of the sigmoid kernel. + + Returns + ------- + result: str + The name of the sigmoid kernel. + """ return "sigmoid" class RadialBasisFunction(SupportVectorMachineKernel): def get_sklearn_kernel(self) -> str: + """ + Get the name of the radial basis function (RBF) kernel. + + Returns + ------- + result: str + The name of the RBF kernel. + """ return "rbf" def _get_kernel_name(self) -> str: + """ + Get the name of the kernel. + + Returns + ------- + result: str + The name of the kernel. + + Raises + ------ + TypeError + If the kernel type is invalid. + """ if isinstance(self.kernel, SupportVectorMachine.Kernel.Linear): return "linear" elif isinstance(self.kernel, SupportVectorMachine.Kernel.Polynomial): @@ -192,3 +253,4 @@ def _get_sklearn_classifier(self) -> ClassifierMixin: The sklearn Classifier. """ return sk_SVC(C=self._c) + diff --git a/src/safeds/ml/classical/regression/_ada_boost.py b/src/safeds/ml/classical/regression/_ada_boost.py index 9e4254292..d4d39bf85 100644 --- a/src/safeds/ml/classical/regression/_ada_boost.py +++ b/src/safeds/ml/classical/regression/_ada_boost.py @@ -60,14 +60,38 @@ def __init__( @property def learner(self) -> Regressor | None: + """ + Get the base learner used for training the ensemble. + + Returns + ------- + result: Classifier | None + The base learner. + """ return self._learner @property def maximum_number_of_learners(self) -> int: + """ + Get the maximum number of learners in the ensemble. + + Returns + ------- + result: int + The maximum number of learners. + """ return self._maximum_number_of_learners @property def learning_rate(self) -> float: + """ + Get the learning rate. + + Returns + ------- + result: float + The learning rate. + """ return self._learning_rate def fit(self, training_set: TaggedTable) -> AdaBoost: diff --git a/src/safeds/ml/classical/regression/_elastic_net_regression.py b/src/safeds/ml/classical/regression/_elastic_net_regression.py index bb8951814..054bcb3cf 100644 --- a/src/safeds/ml/classical/regression/_elastic_net_regression.py +++ b/src/safeds/ml/classical/regression/_elastic_net_regression.py @@ -76,10 +76,26 @@ def __init__(self, *, alpha: float = 1.0, lasso_ratio: float = 0.5) -> None: @property def alpha(self) -> float: + """ + Get the regularization of the model. + + Returns + ------- + result: float + The regularization of the model. + """ return self._alpha @property def lasso_ratio(self) -> float: + """ + Get the ratio between Lasso and Ridge regularization. + + Returns + ------- + result: float + The ratio between Lasso and Ridge regularization. + """ return self._lasso_ratio def fit(self, training_set: TaggedTable) -> ElasticNetRegression: diff --git a/src/safeds/ml/classical/regression/_gradient_boosting.py b/src/safeds/ml/classical/regression/_gradient_boosting.py index c851f948f..b783f4511 100644 --- a/src/safeds/ml/classical/regression/_gradient_boosting.py +++ b/src/safeds/ml/classical/regression/_gradient_boosting.py @@ -51,10 +51,26 @@ def __init__(self, *, number_of_trees: int = 100, learning_rate: float = 0.1) -> @property def number_of_trees(self) -> int: + """ + Get the number of trees (estimators) in the ensemble. + + Returns + ------- + result: int + The number of trees. + """ return self._number_of_trees @property def learning_rate(self) -> float: + """ + Get the learning rate. + + Returns + ------- + result: float + The learning rate. + """ return self._learning_rate def fit(self, training_set: TaggedTable) -> GradientBoosting: diff --git a/src/safeds/ml/classical/regression/_k_nearest_neighbors.py b/src/safeds/ml/classical/regression/_k_nearest_neighbors.py index f99b4d66e..4da871342 100644 --- a/src/safeds/ml/classical/regression/_k_nearest_neighbors.py +++ b/src/safeds/ml/classical/regression/_k_nearest_neighbors.py @@ -46,6 +46,14 @@ def __init__(self, number_of_neighbors: int) -> None: @property def number_of_neighbors(self) -> int: + """ + Get the number of neighbors used for interpolation. + + Returns + ------- + result: int + The number of neighbors. + """ return self._number_of_neighbors def fit(self, training_set: TaggedTable) -> KNearestNeighbors: diff --git a/src/safeds/ml/classical/regression/_lasso_regression.py b/src/safeds/ml/classical/regression/_lasso_regression.py index 857db67a5..ac238e330 100644 --- a/src/safeds/ml/classical/regression/_lasso_regression.py +++ b/src/safeds/ml/classical/regression/_lasso_regression.py @@ -53,6 +53,14 @@ def __init__(self, *, alpha: float = 1.0) -> None: @property def alpha(self) -> float: + """ + Get the regularization of the model. + + Returns + ------- + result: float + The regularization of the model. + """ return self._alpha def fit(self, training_set: TaggedTable) -> LassoRegression: diff --git a/src/safeds/ml/classical/regression/_random_forest.py b/src/safeds/ml/classical/regression/_random_forest.py index 08d8c9883..3908592ff 100644 --- a/src/safeds/ml/classical/regression/_random_forest.py +++ b/src/safeds/ml/classical/regression/_random_forest.py @@ -43,6 +43,14 @@ def __init__(self, *, number_of_trees: int = 100) -> None: @property def number_of_trees(self) -> int: + """ + Get the number of trees used in the random forest. + + Returns + ------- + result: int + The number of trees. + """ return self._number_of_trees def fit(self, training_set: TaggedTable) -> RandomForest: diff --git a/src/safeds/ml/classical/regression/_ridge_regression.py b/src/safeds/ml/classical/regression/_ridge_regression.py index c57b77b43..b775c4215 100644 --- a/src/safeds/ml/classical/regression/_ridge_regression.py +++ b/src/safeds/ml/classical/regression/_ridge_regression.py @@ -54,6 +54,14 @@ def __init__(self, *, alpha: float = 1.0) -> None: @property def alpha(self) -> float: + """ + Get the regularization of the model. + + Returns + ------- + result: float + The regularization of the model. + """ return self._alpha def fit(self, training_set: TaggedTable) -> RidgeRegression: diff --git a/src/safeds/ml/classical/regression/_support_vector_machine.py b/src/safeds/ml/classical/regression/_support_vector_machine.py index d43da828d..3fb6fe367 100644 --- a/src/safeds/ml/classical/regression/_support_vector_machine.py +++ b/src/safeds/ml/classical/regression/_support_vector_machine.py @@ -59,15 +59,39 @@ def __init__(self, *, c: float = 1.0, kernel: SupportVectorMachineKernel | None @property def c(self) -> float: + """ + Get the regularization strength. + + Returns + ------- + result: float + The regularization strength. + """ return self._c @property def kernel(self) -> SupportVectorMachineKernel | None: + """ + Get the type of kernel used. + + Returns + ------- + result: SupportVectorMachineKernel | None + The type of kernel used. + """ return self._kernel class Kernel: class Linear(SupportVectorMachineKernel): def get_sklearn_kernel(self) -> str: + """ + Get the name of the linear kernel. + + Returns + ------- + result: str + The name of the linear kernel. + """ return "linear" class Polynomial(SupportVectorMachineKernel): @@ -77,17 +101,54 @@ def __init__(self, degree: int): self._degree = degree def get_sklearn_kernel(self) -> str: + """ + Get the name of the polynomial kernel. + + Returns + ------- + result: str + The name of the polynomial kernel. + """ return "poly" class Sigmoid(SupportVectorMachineKernel): def get_sklearn_kernel(self) -> str: + """ + Get the name of the sigmoid kernel. + + Returns + ------- + result: str + The name of the sigmoid kernel. + """ return "sigmoid" class RadialBasisFunction(SupportVectorMachineKernel): def get_sklearn_kernel(self) -> str: + """ + Get the name of the radial basis function (RBF) kernel. + + Returns + ------- + result: str + The name of the RBF kernel. + """ return "rbf" def _get_kernel_name(self) -> str: + """ + Get the name of the kernel. + + Returns + ------- + result: str + The name of the kernel. + + Raises + ------ + TypeError + If the kernel type is invalid. + """ if isinstance(self.kernel, SupportVectorMachine.Kernel.Linear): return "linear" elif isinstance(self.kernel, SupportVectorMachine.Kernel.Polynomial): From 10e0d83145f2b6f20277db7a0f65de02174863dc Mon Sep 17 00:00:00 2001 From: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com> Date: Sat, 17 Jun 2023 17:06:39 +0000 Subject: [PATCH 2/4] style: apply automated linter fixes --- .../ml/classical/classification/_support_vector_machine.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/safeds/ml/classical/classification/_support_vector_machine.py b/src/safeds/ml/classical/classification/_support_vector_machine.py index 641aab306..57a528d9f 100644 --- a/src/safeds/ml/classical/classification/_support_vector_machine.py +++ b/src/safeds/ml/classical/classification/_support_vector_machine.py @@ -253,4 +253,3 @@ def _get_sklearn_classifier(self) -> ClassifierMixin: The sklearn Classifier. """ return sk_SVC(C=self._c) - From 5b6699578b97a96e8194819cb229c2011db3c3d7 Mon Sep 17 00:00:00 2001 From: jxnior01 Date: Fri, 23 Jun 2023 11:08:10 +0200 Subject: [PATCH 3/4] nothing added, just for linter to run --- .../ml/classical/classification/_support_vector_machine.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/safeds/ml/classical/classification/_support_vector_machine.py b/src/safeds/ml/classical/classification/_support_vector_machine.py index 641aab306..b38aa25f7 100644 --- a/src/safeds/ml/classical/classification/_support_vector_machine.py +++ b/src/safeds/ml/classical/classification/_support_vector_machine.py @@ -1,6 +1,7 @@ from __future__ import annotations from abc import ABC, abstractmethod + from typing import TYPE_CHECKING from sklearn.svm import SVC as sk_SVC # noqa: N811 From eb85f07e214765cc8cacf75bf6e0d5aeec0a2153 Mon Sep 17 00:00:00 2001 From: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com> Date: Fri, 23 Jun 2023 09:10:25 +0000 Subject: [PATCH 4/4] style: apply automated linter fixes --- .../ml/classical/classification/_support_vector_machine.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/safeds/ml/classical/classification/_support_vector_machine.py b/src/safeds/ml/classical/classification/_support_vector_machine.py index 8c81a5284..57a528d9f 100644 --- a/src/safeds/ml/classical/classification/_support_vector_machine.py +++ b/src/safeds/ml/classical/classification/_support_vector_machine.py @@ -1,7 +1,6 @@ from __future__ import annotations from abc import ABC, abstractmethod - from typing import TYPE_CHECKING from sklearn.svm import SVC as sk_SVC # noqa: N811