From 5d5f5a69d7e4def34ab09494511ae6ad6a62d60b Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Fri, 24 Mar 2023 15:16:16 +0100 Subject: [PATCH] feat: return `TaggedTable` from `predict` (#73) ### Summary of Changes The `predict` method returns the original feature vector combined with the predicted target values. This is exactly what a `TaggedTable` can express, so we return this instead of a `Table`. --------- Co-authored-by: lars-reimann --- src/safeds/ml/_util_sklearn.py | 12 +++------ src/safeds/ml/classification/_ada_boost.py | 21 ++++++---------- src/safeds/ml/classification/_classifier.py | 8 ++++-- .../ml/classification/_decision_tree.py | 21 ++++++---------- .../_gradient_boosting_classification.py | 23 +++++++---------- .../ml/classification/_k_nearest_neighbors.py | 25 ++++++++++--------- .../ml/classification/_logistic_regression.py | 21 ++++++---------- .../ml/classification/_random_forest.py | 23 +++++++---------- src/safeds/ml/regression/_ada_boost.py | 19 ++++++-------- src/safeds/ml/regression/_decision_tree.py | 19 ++++++-------- .../ml/regression/_elastic_net_regression.py | 19 ++++++-------- .../_gradient_boosting_regression.py | 24 ++++++++---------- .../ml/regression/_k_nearest_neighbors.py | 21 +++++++--------- src/safeds/ml/regression/_lasso_regression.py | 19 ++++++-------- .../ml/regression/_linear_regression.py | 19 ++++++-------- src/safeds/ml/regression/_random_forest.py | 21 +++++++--------- src/safeds/ml/regression/_regressor.py | 8 ++++-- src/safeds/ml/regression/_ridge_regression.py | 19 ++++++-------- 18 files changed, 147 insertions(+), 195 deletions(-) diff --git a/src/safeds/ml/_util_sklearn.py b/src/safeds/ml/_util_sklearn.py index 77ac9c693..c07379284 100644 --- a/src/safeds/ml/_util_sklearn.py +++ b/src/safeds/ml/_util_sklearn.py @@ -6,7 +6,7 @@ # noinspection PyProtectedMember -def fit(model: Any, tagged_table: TaggedTable) -> str: +def fit(model: Any, tagged_table: TaggedTable) -> None: """ Fit a model for a given tagged table. @@ -17,11 +17,6 @@ def fit(model: Any, tagged_table: TaggedTable) -> str: tagged_table : TaggedTable The tagged table containing the feature and target vectors. - Returns - ------- - target_name : str - The target column name, inferred from the tagged table. - Raises ------ LearningError @@ -32,7 +27,6 @@ def fit(model: Any, tagged_table: TaggedTable) -> str: tagged_table.feature_vectors._data, tagged_table.target_values._data, ) - return tagged_table.target_values.name except ValueError as exception: raise LearningError(str(exception)) from exception except Exception as exception: @@ -40,7 +34,7 @@ def fit(model: Any, tagged_table: TaggedTable) -> str: # noinspection PyProtectedMember -def predict(model: Any, dataset: Table, target_name: str) -> Table: +def predict(model: Any, dataset: Table, target_name: str) -> TaggedTable: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -73,7 +67,7 @@ def predict(model: Any, dataset: Table, target_name: str) -> Table: f"Dataset already contains '{target_name}' column. Please rename this column" ) result_set[target_name] = predicted_target_vector - return Table(result_set) + return TaggedTable(Table(result_set), target_column=target_name) except NotFittedError as exception: raise PredictionError("The model was not trained") from exception except ValueError as exception: diff --git a/src/safeds/ml/classification/_ada_boost.py b/src/safeds/ml/classification/_ada_boost.py index 7592aae7d..4c583b92b 100644 --- a/src/safeds/ml/classification/_ada_boost.py +++ b/src/safeds/ml/classification/_ada_boost.py @@ -1,6 +1,6 @@ # noinspection PyProtectedMember -import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable +from safeds.ml._util_sklearn import fit, predict from sklearn.ensemble import AdaBoostClassifier as sk_AdaBoostClassifier from ._classifier import Classifier @@ -14,8 +14,8 @@ class AdaBoost(Classifier): """ def __init__(self) -> None: - self._classification = sk_AdaBoostClassifier() - self.target_name = "" + self._wrapped_classifier = sk_AdaBoostClassifier() + self._target_name = "" def fit(self, training_set: TaggedTable) -> None: """ @@ -31,11 +31,10 @@ def fit(self, training_set: TaggedTable) -> None: LearningError If the tagged table contains invalid values or if the training failed. """ - self.target_name = safeds.ml._util_sklearn.fit( - self._classification, training_set - ) + fit(self._wrapped_classifier, training_set) + self._target_name = training_set.target_values.name - def predict(self, dataset: Table) -> Table: + def predict(self, dataset: Table) -> TaggedTable: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -46,7 +45,7 @@ def predict(self, dataset: Table) -> Table: Returns ------- - table : Table + table : TaggedTable A dataset containing the given feature vectors and the predicted target vector. Raises @@ -54,8 +53,4 @@ def predict(self, dataset: Table) -> Table: PredictionError If prediction with the given dataset failed. """ - return safeds.ml._util_sklearn.predict( - self._classification, - dataset, - self.target_name, - ) + return predict(self._wrapped_classifier, dataset, self._target_name) diff --git a/src/safeds/ml/classification/_classifier.py b/src/safeds/ml/classification/_classifier.py index a89d3a5ce..a0423ab09 100644 --- a/src/safeds/ml/classification/_classifier.py +++ b/src/safeds/ml/classification/_classifier.py @@ -4,6 +4,10 @@ class Classifier(ABC): + """ + Abstract base class for all classifiers. + """ + @abstractmethod def fit(self, training_set: TaggedTable) -> None: """ @@ -21,7 +25,7 @@ def fit(self, training_set: TaggedTable) -> None: """ @abstractmethod - def predict(self, dataset: Table) -> Table: + def predict(self, dataset: Table) -> TaggedTable: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -32,7 +36,7 @@ def predict(self, dataset: Table) -> Table: Returns ------- - table : Table + table : TaggedTable A dataset containing the given feature vectors and the predicted target vector. Raises diff --git a/src/safeds/ml/classification/_decision_tree.py b/src/safeds/ml/classification/_decision_tree.py index 0969bd650..35e42c7b4 100644 --- a/src/safeds/ml/classification/_decision_tree.py +++ b/src/safeds/ml/classification/_decision_tree.py @@ -1,6 +1,6 @@ # noinspection PyProtectedMember -import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable +from safeds.ml._util_sklearn import fit, predict from sklearn.tree import DecisionTreeClassifier as sk_DecisionTreeClassifier from ._classifier import Classifier @@ -14,8 +14,8 @@ class DecisionTree(Classifier): """ def __init__(self) -> None: - self._classification = sk_DecisionTreeClassifier() - self.target_name = "" + self._wrapped_classifier = sk_DecisionTreeClassifier() + self._target_name = "" def fit(self, training_set: TaggedTable) -> None: """ @@ -31,11 +31,10 @@ def fit(self, training_set: TaggedTable) -> None: LearningError If the tagged table contains invalid values or if the training failed. """ - self.target_name = safeds.ml._util_sklearn.fit( - self._classification, training_set - ) + fit(self._wrapped_classifier, training_set) + self._target_name = training_set.target_values.name - def predict(self, dataset: Table) -> Table: + def predict(self, dataset: Table) -> TaggedTable: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -46,7 +45,7 @@ def predict(self, dataset: Table) -> Table: Returns ------- - table : Table + table : TaggedTable A dataset containing the given feature vectors and the predicted target vector. Raises @@ -54,8 +53,4 @@ def predict(self, dataset: Table) -> Table: PredictionError If prediction with the given dataset failed. """ - return safeds.ml._util_sklearn.predict( - self._classification, - dataset, - self.target_name, - ) + return predict(self._wrapped_classifier, dataset, self._target_name) diff --git a/src/safeds/ml/classification/_gradient_boosting_classification.py b/src/safeds/ml/classification/_gradient_boosting_classification.py index bd5551cf9..c2e2416bc 100644 --- a/src/safeds/ml/classification/_gradient_boosting_classification.py +++ b/src/safeds/ml/classification/_gradient_boosting_classification.py @@ -1,7 +1,7 @@ # noinspection PyProtectedMember -import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable -from sklearn.ensemble import GradientBoostingClassifier +from safeds.ml._util_sklearn import fit, predict +from sklearn.ensemble import GradientBoostingClassifier as sk_GradientBoostingClassifier from ._classifier import Classifier @@ -14,8 +14,8 @@ class GradientBoosting(Classifier): """ def __init__(self) -> None: - self._classification = GradientBoostingClassifier() - self.target_name = "" + self._wrapped_classifier = sk_GradientBoostingClassifier() + self._target_name = "" def fit(self, training_set: TaggedTable) -> None: """ @@ -31,12 +31,11 @@ def fit(self, training_set: TaggedTable) -> None: LearningError If the tagged table contains invalid values or if the training failed. """ - self.target_name = safeds.ml._util_sklearn.fit( - self._classification, training_set - ) + fit(self._wrapped_classifier, training_set) + self._target_name = training_set.target_values.name # noinspection PyProtectedMember - def predict(self, dataset: Table) -> Table: + def predict(self, dataset: Table) -> TaggedTable: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -47,7 +46,7 @@ def predict(self, dataset: Table) -> Table: Returns ------- - table : Table + table : TaggedTable A dataset containing the given feature vectors and the predicted target vector. Raises @@ -55,8 +54,4 @@ def predict(self, dataset: Table) -> Table: PredictionError If prediction with the given dataset failed. """ - return safeds.ml._util_sklearn.predict( - self._classification, - dataset, - self.target_name, - ) + return predict(self._wrapped_classifier, dataset, self._target_name) diff --git a/src/safeds/ml/classification/_k_nearest_neighbors.py b/src/safeds/ml/classification/_k_nearest_neighbors.py index 4278dbd85..89a19e477 100644 --- a/src/safeds/ml/classification/_k_nearest_neighbors.py +++ b/src/safeds/ml/classification/_k_nearest_neighbors.py @@ -1,7 +1,7 @@ # noinspection PyProtectedMember -import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable -from sklearn.neighbors import KNeighborsClassifier +from safeds.ml._util_sklearn import fit, predict +from sklearn.neighbors import KNeighborsClassifier as sk_KNeighborsClassifier from ._classifier import Classifier @@ -18,8 +18,10 @@ class KNearestNeighbors(Classifier): """ def __init__(self, n_neighbors: int) -> None: - self._classification = KNeighborsClassifier(n_jobs=-1, n_neighbors=n_neighbors) - self.target_name = "" + self._wrapped_classifier = sk_KNeighborsClassifier( + n_jobs=-1, n_neighbors=n_neighbors + ) + self._target_name = "" def fit(self, training_set: TaggedTable) -> None: """ @@ -35,11 +37,10 @@ def fit(self, training_set: TaggedTable) -> None: LearningError If the tagged table contains invalid values or if the training failed. """ - self.target_name = safeds.ml._util_sklearn.fit( - self._classification, training_set - ) + fit(self._wrapped_classifier, training_set) + self._target_name = training_set.target_values.name - def predict(self, dataset: Table) -> Table: + def predict(self, dataset: Table) -> TaggedTable: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first @@ -50,7 +51,7 @@ def predict(self, dataset: Table) -> Table: Returns ------- - table : Table + table : TaggedTable A dataset containing the given feature vectors and the predicted target vector. Raises @@ -58,8 +59,8 @@ def predict(self, dataset: Table) -> Table: PredictionError If prediction with the given dataset failed. """ - return safeds.ml._util_sklearn.predict( - self._classification, + return predict( + self._wrapped_classifier, dataset, - self.target_name, + self._target_name, ) diff --git a/src/safeds/ml/classification/_logistic_regression.py b/src/safeds/ml/classification/_logistic_regression.py index 263c6ae3e..48917295b 100644 --- a/src/safeds/ml/classification/_logistic_regression.py +++ b/src/safeds/ml/classification/_logistic_regression.py @@ -1,6 +1,6 @@ # noinspection PyProtectedMember -import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable +from safeds.ml._util_sklearn import fit, predict from sklearn.linear_model import LogisticRegression as sk_LogisticRegression from ._classifier import Classifier @@ -14,8 +14,8 @@ class LogisticRegression(Classifier): """ def __init__(self) -> None: - self._classification = sk_LogisticRegression(n_jobs=-1) - self.target_name = "" + self._wrapped_classifier = sk_LogisticRegression(n_jobs=-1) + self._target_name = "" def fit(self, training_set: TaggedTable) -> None: """ @@ -31,11 +31,10 @@ def fit(self, training_set: TaggedTable) -> None: LearningError If the tagged table contains invalid values or if the training failed. """ - self.target_name = safeds.ml._util_sklearn.fit( - self._classification, training_set - ) + fit(self._wrapped_classifier, training_set) + self._target_name = training_set.target_values.name - def predict(self, dataset: Table) -> Table: + def predict(self, dataset: Table) -> TaggedTable: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -46,7 +45,7 @@ def predict(self, dataset: Table) -> Table: Returns ------- - table : Table + table : TaggedTable A dataset containing the given feature vectors and the predicted target vector. Raises @@ -54,8 +53,4 @@ def predict(self, dataset: Table) -> Table: PredictionError If prediction with the given dataset failed. """ - return safeds.ml._util_sklearn.predict( - self._classification, - dataset, - self.target_name, - ) + return predict(self._wrapped_classifier, dataset, self._target_name) diff --git a/src/safeds/ml/classification/_random_forest.py b/src/safeds/ml/classification/_random_forest.py index 40d6d7681..d78a2c5e1 100644 --- a/src/safeds/ml/classification/_random_forest.py +++ b/src/safeds/ml/classification/_random_forest.py @@ -1,7 +1,7 @@ # noinspection PyProtectedMember -import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable -from sklearn.ensemble import RandomForestClassifier +from safeds.ml._util_sklearn import fit, predict +from sklearn.ensemble import RandomForestClassifier as sk_RandomForestClassifier from ._classifier import Classifier @@ -13,8 +13,8 @@ class RandomForest(Classifier): """ def __init__(self) -> None: - self._classification = RandomForestClassifier(n_jobs=-1) - self.target_name = "" + self._wrapped_classifier = sk_RandomForestClassifier(n_jobs=-1) + self._target_name = "" def fit(self, training_set: TaggedTable) -> None: """ @@ -30,11 +30,10 @@ def fit(self, training_set: TaggedTable) -> None: LearningError If the tagged table contains invalid values or if the training failed. """ - self.target_name = safeds.ml._util_sklearn.fit( - self._classification, training_set - ) + fit(self._wrapped_classifier, training_set) + self._target_name = training_set.target_values.name - def predict(self, dataset: Table) -> Table: + def predict(self, dataset: Table) -> TaggedTable: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -45,7 +44,7 @@ def predict(self, dataset: Table) -> Table: Returns ------- - table : Table + table : TaggedTable A dataset containing the given feature vectors and the predicted target vector. Raises @@ -53,8 +52,4 @@ def predict(self, dataset: Table) -> Table: PredictionError If prediction with the given dataset failed. """ - return safeds.ml._util_sklearn.predict( - self._classification, - dataset, - self.target_name, - ) + return predict(self._wrapped_classifier, dataset, self._target_name) diff --git a/src/safeds/ml/regression/_ada_boost.py b/src/safeds/ml/regression/_ada_boost.py index ed8349b5c..16c4ed1c2 100644 --- a/src/safeds/ml/regression/_ada_boost.py +++ b/src/safeds/ml/regression/_ada_boost.py @@ -1,6 +1,6 @@ # noinspection PyProtectedMember -import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable +from safeds.ml._util_sklearn import fit, predict from sklearn.ensemble import AdaBoostRegressor as sk_AdaBoostRegressor from ._regressor import Regressor @@ -14,8 +14,8 @@ class AdaBoost(Regressor): """ def __init__(self) -> None: - self._regression = sk_AdaBoostRegressor() - self.target_name = "" + self._wrapped_regressor = sk_AdaBoostRegressor() + self._target_name = "" def fit(self, training_set: TaggedTable) -> None: """ @@ -31,9 +31,10 @@ def fit(self, training_set: TaggedTable) -> None: LearningError If the tagged table contains invalid values or if the training failed. """ - self.target_name = safeds.ml._util_sklearn.fit(self._regression, training_set) + fit(self._wrapped_regressor, training_set) + self._target_name = training_set.target_values.name - def predict(self, dataset: Table) -> Table: + def predict(self, dataset: Table) -> TaggedTable: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -44,7 +45,7 @@ def predict(self, dataset: Table) -> Table: Returns ------- - table : Table + table : TaggedTable A dataset containing the given feature vectors and the predicted target vector. Raises @@ -52,8 +53,4 @@ def predict(self, dataset: Table) -> Table: PredictionError If prediction with the given dataset failed. """ - return safeds.ml._util_sklearn.predict( - self._regression, - dataset, - self.target_name, - ) + return predict(self._wrapped_regressor, dataset, self._target_name) diff --git a/src/safeds/ml/regression/_decision_tree.py b/src/safeds/ml/regression/_decision_tree.py index e305b9da2..d6417b705 100644 --- a/src/safeds/ml/regression/_decision_tree.py +++ b/src/safeds/ml/regression/_decision_tree.py @@ -1,6 +1,6 @@ # noinspection PyProtectedMember -import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable +from safeds.ml._util_sklearn import fit, predict from sklearn.tree import DecisionTreeRegressor as sk_DecisionTreeRegressor from ._regressor import Regressor @@ -14,8 +14,8 @@ class DecisionTree(Regressor): """ def __init__(self) -> None: - self._regression = sk_DecisionTreeRegressor() - self.target_name = "" + self._wrapped_regressor = sk_DecisionTreeRegressor() + self._target_name = "" def fit(self, training_set: TaggedTable) -> None: """ @@ -31,9 +31,10 @@ def fit(self, training_set: TaggedTable) -> None: LearningError If the tagged table contains invalid values or if the training failed. """ - self.target_name = safeds.ml._util_sklearn.fit(self._regression, training_set) + fit(self._wrapped_regressor, training_set) + self._target_name = training_set.target_values.name - def predict(self, dataset: Table) -> Table: + def predict(self, dataset: Table) -> TaggedTable: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -44,7 +45,7 @@ def predict(self, dataset: Table) -> Table: Returns ------- - table : Table + table : TaggedTable A dataset containing the given feature vectors and the predicted target vector. Raises @@ -52,8 +53,4 @@ def predict(self, dataset: Table) -> Table: PredictionError If prediction with the given dataset failed. """ - return safeds.ml._util_sklearn.predict( - self._regression, - dataset, - self.target_name, - ) + return predict(self._wrapped_regressor, dataset, self._target_name) diff --git a/src/safeds/ml/regression/_elastic_net_regression.py b/src/safeds/ml/regression/_elastic_net_regression.py index e74763283..98c53b629 100644 --- a/src/safeds/ml/regression/_elastic_net_regression.py +++ b/src/safeds/ml/regression/_elastic_net_regression.py @@ -1,6 +1,6 @@ # noinspection PyProtectedMember -import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable +from safeds.ml._util_sklearn import fit, predict from sklearn.linear_model import ElasticNet as sk_ElasticNet from ._regressor import Regressor @@ -14,8 +14,8 @@ class ElasticNetRegression(Regressor): """ def __init__(self) -> None: - self._regression = sk_ElasticNet() - self.target_name = "" + self._wrapped_regressor = sk_ElasticNet() + self._target_name = "" def fit(self, training_set: TaggedTable) -> None: """ @@ -31,9 +31,10 @@ def fit(self, training_set: TaggedTable) -> None: LearningError If the tagged table contains invalid values or if the training failed. """ - self.target_name = safeds.ml._util_sklearn.fit(self._regression, training_set) + fit(self._wrapped_regressor, training_set) + self._target_name = training_set.target_values.name - def predict(self, dataset: Table) -> Table: + def predict(self, dataset: Table) -> TaggedTable: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -44,7 +45,7 @@ def predict(self, dataset: Table) -> Table: Returns ------- - table : Table + table : TaggedTable A dataset containing the given feature vectors and the predicted target vector. Raises @@ -52,8 +53,4 @@ def predict(self, dataset: Table) -> Table: PredictionError If prediction with the given dataset failed """ - return safeds.ml._util_sklearn.predict( - self._regression, - dataset, - self.target_name, - ) + return predict(self._wrapped_regressor, dataset, self._target_name) diff --git a/src/safeds/ml/regression/_gradient_boosting_regression.py b/src/safeds/ml/regression/_gradient_boosting_regression.py index 32cf84d77..c1d61cec0 100644 --- a/src/safeds/ml/regression/_gradient_boosting_regression.py +++ b/src/safeds/ml/regression/_gradient_boosting_regression.py @@ -1,7 +1,7 @@ # noinspection PyProtectedMember -import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable -from sklearn.ensemble import GradientBoostingRegressor +from safeds.ml._util_sklearn import fit, predict +from sklearn.ensemble import GradientBoostingRegressor as sk_GradientBoostingRegressor from ._regressor import Regressor @@ -14,8 +14,8 @@ class GradientBoosting(Regressor): """ def __init__(self) -> None: - self._regression = GradientBoostingRegressor() - self.target_name = "" + self._wrapped_regressor = sk_GradientBoostingRegressor() + self._target_name = "" def fit(self, training_set: TaggedTable) -> None: """ @@ -32,10 +32,12 @@ def fit(self, training_set: TaggedTable) -> None: LearningError If the tagged table contains invalid values or if the training failed. """ - self.target_name = safeds.ml._util_sklearn.fit(self._regression, training_set) + fit(self._wrapped_regressor, training_set) + self._target_name = training_set.target_values.name - # noinspection PyProtectedMember - def predict(self, dataset: Table) -> Table: + # noinspection PyProtectedMember + + def predict(self, dataset: Table) -> TaggedTable: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -46,7 +48,7 @@ def predict(self, dataset: Table) -> Table: Returns ------- - table : Table + table : TaggedTable A dataset containing the given feature vectors and the predicted target vector. Raises @@ -54,8 +56,4 @@ def predict(self, dataset: Table) -> Table: PredictionError If prediction with the given dataset failed. """ - return safeds.ml._util_sklearn.predict( - self._regression, - dataset, - self.target_name, - ) + return predict(self._wrapped_regressor, dataset, self._target_name) diff --git a/src/safeds/ml/regression/_k_nearest_neighbors.py b/src/safeds/ml/regression/_k_nearest_neighbors.py index 6ea2a7e9c..a690fe4dc 100644 --- a/src/safeds/ml/regression/_k_nearest_neighbors.py +++ b/src/safeds/ml/regression/_k_nearest_neighbors.py @@ -1,7 +1,7 @@ # noinspection PyProtectedMember -import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable -from sklearn.neighbors import KNeighborsRegressor +from safeds.ml._util_sklearn import fit, predict +from sklearn.neighbors import KNeighborsRegressor as sk_KNeighborsRegressor from ._regressor import Regressor @@ -18,8 +18,8 @@ class KNearestNeighbors(Regressor): """ def __init__(self, n_neighbors: int) -> None: - self._regression = KNeighborsRegressor(n_neighbors) - self.target_name = "" + self._wrapped_regressor = sk_KNeighborsRegressor(n_neighbors) + self._target_name = "" def fit(self, training_set: TaggedTable) -> None: """ @@ -35,9 +35,10 @@ def fit(self, training_set: TaggedTable) -> None: LearningError If the tagged table contains invalid values or if the training failed. """ - self.target_name = safeds.ml._util_sklearn.fit(self._regression, training_set) + fit(self._wrapped_regressor, training_set) + self._target_name = training_set.target_values.name - def predict(self, dataset: Table) -> Table: + def predict(self, dataset: Table) -> TaggedTable: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -48,7 +49,7 @@ def predict(self, dataset: Table) -> Table: Returns ------- - table : Table + table : TaggedTable A dataset containing the given feature vectors and the predicted target vector. Raises @@ -56,8 +57,4 @@ def predict(self, dataset: Table) -> Table: PredictionError If prediction with the given dataset failed. """ - return safeds.ml._util_sklearn.predict( - self._regression, - dataset, - self.target_name, - ) + return predict(self._wrapped_regressor, dataset, self._target_name) diff --git a/src/safeds/ml/regression/_lasso_regression.py b/src/safeds/ml/regression/_lasso_regression.py index ecd234df2..bc2c6e53f 100644 --- a/src/safeds/ml/regression/_lasso_regression.py +++ b/src/safeds/ml/regression/_lasso_regression.py @@ -1,6 +1,6 @@ # noinspection PyProtectedMember -import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable +from safeds.ml._util_sklearn import fit, predict from sklearn.linear_model import Lasso as sk_Lasso from ._regressor import Regressor @@ -14,8 +14,8 @@ class LassoRegression(Regressor): """ def __init__(self) -> None: - self._regression = sk_Lasso() - self.target_name = "" + self._wrapped_regressor = sk_Lasso() + self._target_name = "" def fit(self, training_set: TaggedTable) -> None: """ @@ -31,9 +31,10 @@ def fit(self, training_set: TaggedTable) -> None: LearningError If the tagged table contains invalid values or if the training failed. """ - self.target_name = safeds.ml._util_sklearn.fit(self._regression, training_set) + fit(self._wrapped_regressor, training_set) + self._target_name = training_set.target_values.name - def predict(self, dataset: Table) -> Table: + def predict(self, dataset: Table) -> TaggedTable: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -44,7 +45,7 @@ def predict(self, dataset: Table) -> Table: Returns ------- - table : Table + table : TaggedTable A dataset containing the given feature vectors and the predicted target vector. Raises @@ -52,8 +53,4 @@ def predict(self, dataset: Table) -> Table: PredictionError If prediction with the given dataset failed. """ - return safeds.ml._util_sklearn.predict( - self._regression, - dataset, - self.target_name, - ) + return predict(self._wrapped_regressor, dataset, self._target_name) diff --git a/src/safeds/ml/regression/_linear_regression.py b/src/safeds/ml/regression/_linear_regression.py index 9b8dc68b6..bd7efdea0 100644 --- a/src/safeds/ml/regression/_linear_regression.py +++ b/src/safeds/ml/regression/_linear_regression.py @@ -1,6 +1,6 @@ # noinspection PyProtectedMember -import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable +from safeds.ml._util_sklearn import fit, predict from sklearn.linear_model import LinearRegression as sk_LinearRegression from ._regressor import Regressor @@ -14,8 +14,8 @@ class LinearRegression(Regressor): """ def __init__(self) -> None: - self._regression = sk_LinearRegression(n_jobs=-1) - self.target_name = "" + self._wrapped_regressor = sk_LinearRegression(n_jobs=-1) + self._target_name = "" def fit(self, training_set: TaggedTable) -> None: """ @@ -31,9 +31,10 @@ def fit(self, training_set: TaggedTable) -> None: LearningError If the tagged table contains invalid values or if the training failed. """ - self.target_name = safeds.ml._util_sklearn.fit(self._regression, training_set) + fit(self._wrapped_regressor, training_set) + self._target_name = training_set.target_values.name - def predict(self, dataset: Table) -> Table: + def predict(self, dataset: Table) -> TaggedTable: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -44,7 +45,7 @@ def predict(self, dataset: Table) -> Table: Returns ------- - table : Table + table : TaggedTable A dataset containing the given feature vectors and the predicted target vector. Raises @@ -52,8 +53,4 @@ def predict(self, dataset: Table) -> Table: PredictionError If prediction with the given dataset failed. """ - return safeds.ml._util_sklearn.predict( - self._regression, - dataset, - self.target_name, - ) + return predict(self._wrapped_regressor, dataset, self._target_name) diff --git a/src/safeds/ml/regression/_random_forest.py b/src/safeds/ml/regression/_random_forest.py index 7c9c3452f..b9d0d7cfa 100644 --- a/src/safeds/ml/regression/_random_forest.py +++ b/src/safeds/ml/regression/_random_forest.py @@ -1,7 +1,7 @@ # noinspection PyProtectedMember -import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable -from sklearn.ensemble import RandomForestRegressor +from safeds.ml._util_sklearn import fit, predict +from sklearn.ensemble import RandomForestRegressor as sk_RandomForestRegressor from ._regressor import Regressor @@ -13,8 +13,8 @@ class RandomForest(Regressor): """ def __init__(self) -> None: - self._regression = RandomForestRegressor(n_jobs=-1) - self.target_name = "" + self._wrapped_regressor = sk_RandomForestRegressor(n_jobs=-1) + self._target_name = "" def fit(self, training_set: TaggedTable) -> None: """ @@ -30,9 +30,10 @@ def fit(self, training_set: TaggedTable) -> None: LearningError If the tagged table contains invalid values or if the training failed. """ - self.target_name = safeds.ml._util_sklearn.fit(self._regression, training_set) + fit(self._wrapped_regressor, training_set) + self._target_name = training_set.target_values.name - def predict(self, dataset: Table) -> Table: + def predict(self, dataset: Table) -> TaggedTable: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -43,7 +44,7 @@ def predict(self, dataset: Table) -> Table: Returns ------- - table : Table + table : TaggedTable A dataset containing the given feature vectors and the predicted target vector. Raises @@ -51,8 +52,4 @@ def predict(self, dataset: Table) -> Table: PredictionError If prediction with the given dataset failed. """ - return safeds.ml._util_sklearn.predict( - self._regression, - dataset, - self.target_name, - ) + return predict(self._wrapped_regressor, dataset, self._target_name) diff --git a/src/safeds/ml/regression/_regressor.py b/src/safeds/ml/regression/_regressor.py index 1b6d2a149..a32c7f770 100644 --- a/src/safeds/ml/regression/_regressor.py +++ b/src/safeds/ml/regression/_regressor.py @@ -4,6 +4,10 @@ class Regressor(ABC): + """ + Abstract base class for all regressors. + """ + @abstractmethod def fit(self, training_set: TaggedTable) -> None: """ @@ -21,7 +25,7 @@ def fit(self, training_set: TaggedTable) -> None: """ @abstractmethod - def predict(self, dataset: Table) -> Table: + def predict(self, dataset: Table) -> TaggedTable: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -32,7 +36,7 @@ def predict(self, dataset: Table) -> Table: Returns ------- - table : Table + table : TaggedTable A dataset containing the given feature vectors and the predicted target vector. Raises diff --git a/src/safeds/ml/regression/_ridge_regression.py b/src/safeds/ml/regression/_ridge_regression.py index e05450cde..9b132ff90 100644 --- a/src/safeds/ml/regression/_ridge_regression.py +++ b/src/safeds/ml/regression/_ridge_regression.py @@ -1,6 +1,6 @@ # noinspection PyProtectedMember -import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable +from safeds.ml._util_sklearn import fit, predict from sklearn.linear_model import Ridge as sk_Ridge from ._regressor import Regressor @@ -14,8 +14,8 @@ class RidgeRegression(Regressor): """ def __init__(self) -> None: - self._regression = sk_Ridge() - self.target_name = "" + self._wrapped_regressor = sk_Ridge() + self._target_name = "" def fit(self, training_set: TaggedTable) -> None: """ @@ -31,9 +31,10 @@ def fit(self, training_set: TaggedTable) -> None: LearningError If the tagged table contains invalid values or if the training failed. """ - self.target_name = safeds.ml._util_sklearn.fit(self._regression, training_set) + fit(self._wrapped_regressor, training_set) + self._target_name = training_set.target_values.name - def predict(self, dataset: Table) -> Table: + def predict(self, dataset: Table) -> TaggedTable: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -44,7 +45,7 @@ def predict(self, dataset: Table) -> Table: Returns ------- - table : Table + table : TaggedTable A dataset containing the given feature vectors and the predicted target vector. Raises @@ -52,8 +53,4 @@ def predict(self, dataset: Table) -> Table: PredictionError If prediction with the given dataset failed. """ - return safeds.ml._util_sklearn.predict( - self._regression, - dataset, - self.target_name, - ) + return predict(self._wrapped_regressor, dataset, self._target_name)