From 9073f04cd339fc2146ea04339ed14688f29b336f Mon Sep 17 00:00:00 2001 From: Junior Atemebang <129027012+jxnior01@users.noreply.github.com> Date: Fri, 23 Jun 2023 11:34:41 +0200 Subject: [PATCH] feat: Add docstrings to the getter methods for hyperparameters in Regression and Classification models (#371) Closes #313 ### Summary of Changes * feat: Added docstrings in all getter methods of Hyperparameters for classification and regression models. * feat: Added docstrings in all methods of class Kernel in SupportVectorMachine for classification and regression models. --------- Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com> --- .../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 | 61 +++++++++++++++++++ .../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, 266 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..57a528d9f 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): 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):