From b513454c294f8ca03fbffa2b6f89a87e7d6fb9c6 Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Fri, 24 Mar 2023 13:57:19 +0100 Subject: [PATCH] feat: remove `target_name` parameter of `predict` (#70) ### Summary of Changes This can also be accomplished by renaming columns in the returned table. If the input table already contains the column we want to predict, it's an indicator for #9, so we shouldn't just let people change the name here. --------- Co-authored-by: lars-reimann --- src/safeds/ml/classification/_ada_boost.py | 8 ++------ src/safeds/ml/classification/_classifier.py | 5 +---- src/safeds/ml/classification/_decision_tree.py | 8 ++------ .../classification/_gradient_boosting_classification.py | 8 ++------ src/safeds/ml/classification/_k_nearest_neighbors.py | 8 ++------ src/safeds/ml/classification/_logistic_regression.py | 8 ++------ src/safeds/ml/classification/_random_forest.py | 8 ++------ src/safeds/ml/regression/_ada_boost.py | 8 ++------ src/safeds/ml/regression/_decision_tree.py | 8 ++------ src/safeds/ml/regression/_elastic_net_regression.py | 8 ++------ src/safeds/ml/regression/_gradient_boosting_regression.py | 8 ++------ src/safeds/ml/regression/_k_nearest_neighbors.py | 8 ++------ src/safeds/ml/regression/_lasso_regression.py | 8 ++------ src/safeds/ml/regression/_linear_regression.py | 8 ++------ src/safeds/ml/regression/_random_forest.py | 8 ++------ src/safeds/ml/regression/_regressor.py | 5 +---- src/safeds/ml/regression/_ridge_regression.py | 8 ++------ 17 files changed, 32 insertions(+), 98 deletions(-) diff --git a/src/safeds/ml/classification/_ada_boost.py b/src/safeds/ml/classification/_ada_boost.py index 2eabb311d..a1fff3dcd 100644 --- a/src/safeds/ml/classification/_ada_boost.py +++ b/src/safeds/ml/classification/_ada_boost.py @@ -1,5 +1,3 @@ -from typing import Optional - # noinspection PyProtectedMember import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable @@ -37,7 +35,7 @@ def fit(self, tagged_table: TaggedTable) -> None: self._classification, tagged_table ) - def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: + def predict(self, dataset: Table) -> Table: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -45,8 +43,6 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: ---------- dataset : Table The dataset containing the feature vectors. - target_name : Optional[str] - The name of the target vector. The name of the target column inferred from fit is used by default. Returns ------- @@ -61,5 +57,5 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: return safeds.ml._util_sklearn.predict( self._classification, dataset, - target_name if target_name is not None else self.target_name, + self.target_name, ) diff --git a/src/safeds/ml/classification/_classifier.py b/src/safeds/ml/classification/_classifier.py index b3b045ac7..ea3d19c60 100644 --- a/src/safeds/ml/classification/_classifier.py +++ b/src/safeds/ml/classification/_classifier.py @@ -1,5 +1,4 @@ from abc import ABC, abstractmethod -from typing import Optional from safeds.data.tabular.containers import Table, TaggedTable @@ -22,7 +21,7 @@ def fit(self, tagged_table: TaggedTable) -> None: """ @abstractmethod - def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: + def predict(self, dataset: Table) -> Table: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -30,8 +29,6 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: ---------- dataset : Table The dataset containing the feature vectors. - target_name : Optional[str] - The name of the target vector. The name of the target column inferred from fit is used by default. Returns ------- diff --git a/src/safeds/ml/classification/_decision_tree.py b/src/safeds/ml/classification/_decision_tree.py index bae5c1397..cdbc2f571 100644 --- a/src/safeds/ml/classification/_decision_tree.py +++ b/src/safeds/ml/classification/_decision_tree.py @@ -1,5 +1,3 @@ -from typing import Optional - # noinspection PyProtectedMember import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable @@ -37,7 +35,7 @@ def fit(self, tagged_table: TaggedTable) -> None: self._classification, tagged_table ) - def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: + def predict(self, dataset: Table) -> Table: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -45,8 +43,6 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: ---------- dataset : Table The dataset containing the feature vectors. - target_name : Optional[str] - The name of the target vector. The name of the target column inferred from fit is used by default. Returns ------- @@ -61,5 +57,5 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: return safeds.ml._util_sklearn.predict( self._classification, dataset, - target_name if target_name is not None else self.target_name, + self.target_name, ) diff --git a/src/safeds/ml/classification/_gradient_boosting_classification.py b/src/safeds/ml/classification/_gradient_boosting_classification.py index 999f8771f..636efa8dd 100644 --- a/src/safeds/ml/classification/_gradient_boosting_classification.py +++ b/src/safeds/ml/classification/_gradient_boosting_classification.py @@ -1,5 +1,3 @@ -from typing import Optional - # noinspection PyProtectedMember import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable @@ -38,7 +36,7 @@ def fit(self, tagged_table: TaggedTable) -> None: ) # noinspection PyProtectedMember - def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: + def predict(self, dataset: Table) -> Table: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -46,8 +44,6 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: ---------- dataset : Table The dataset containing the feature vectors. - target_name : Optional[str] - The name of the target vector. The name of the target column inferred from fit is used by default. Returns ------- @@ -62,5 +58,5 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: return safeds.ml._util_sklearn.predict( self._classification, dataset, - target_name if target_name is not None else self.target_name, + self.target_name, ) diff --git a/src/safeds/ml/classification/_k_nearest_neighbors.py b/src/safeds/ml/classification/_k_nearest_neighbors.py index 547ebfbbd..065f08d80 100644 --- a/src/safeds/ml/classification/_k_nearest_neighbors.py +++ b/src/safeds/ml/classification/_k_nearest_neighbors.py @@ -1,5 +1,3 @@ -from typing import Optional - # noinspection PyProtectedMember import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable @@ -41,7 +39,7 @@ def fit(self, tagged_table: TaggedTable) -> None: self._classification, tagged_table ) - def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: + def predict(self, dataset: Table) -> Table: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first @@ -49,8 +47,6 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: ---------- dataset : Table The dataset containing the feature vectors. - target_name : Optional[str] - The name of the target vector. The name of the target column inferred from fit is used by default. Returns ------- @@ -65,5 +61,5 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: return safeds.ml._util_sklearn.predict( self._classification, dataset, - target_name if target_name is not None else 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 45c759dbc..53d3f0221 100644 --- a/src/safeds/ml/classification/_logistic_regression.py +++ b/src/safeds/ml/classification/_logistic_regression.py @@ -1,5 +1,3 @@ -from typing import Optional - # noinspection PyProtectedMember import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable @@ -37,7 +35,7 @@ def fit(self, tagged_table: TaggedTable) -> None: self._classification, tagged_table ) - def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: + def predict(self, dataset: Table) -> Table: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -45,8 +43,6 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: ---------- dataset : Table The dataset containing the feature vectors. - target_name : Optional[str] - The name of the target vector. The name of the target column inferred from fit is used by default. Returns ------- @@ -61,5 +57,5 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: return safeds.ml._util_sklearn.predict( self._classification, dataset, - target_name if target_name is not None else self.target_name, + self.target_name, ) diff --git a/src/safeds/ml/classification/_random_forest.py b/src/safeds/ml/classification/_random_forest.py index ae4318c7b..5ff5599f0 100644 --- a/src/safeds/ml/classification/_random_forest.py +++ b/src/safeds/ml/classification/_random_forest.py @@ -1,5 +1,3 @@ -from typing import Optional - # noinspection PyProtectedMember import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable @@ -36,7 +34,7 @@ def fit(self, tagged_table: TaggedTable) -> None: self._classification, tagged_table ) - def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: + def predict(self, dataset: Table) -> Table: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -44,8 +42,6 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: ---------- dataset : Table The dataset containing the feature vectors. - target_name : Optional[str] - The name of the target vector. The name of the target column inferred from fit is used by default. Returns ------- @@ -60,5 +56,5 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: return safeds.ml._util_sklearn.predict( self._classification, dataset, - target_name if target_name is not None else self.target_name, + self.target_name, ) diff --git a/src/safeds/ml/regression/_ada_boost.py b/src/safeds/ml/regression/_ada_boost.py index 2b25dfa13..8968f8fc7 100644 --- a/src/safeds/ml/regression/_ada_boost.py +++ b/src/safeds/ml/regression/_ada_boost.py @@ -1,5 +1,3 @@ -from typing import Optional - # noinspection PyProtectedMember import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable @@ -35,7 +33,7 @@ def fit(self, tagged_table: TaggedTable) -> None: """ self.target_name = safeds.ml._util_sklearn.fit(self._regression, tagged_table) - def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: + def predict(self, dataset: Table) -> Table: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -43,8 +41,6 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: ---------- dataset : Table The dataset containing the feature vectors. - target_name : Optional[str] - The name of the target vector. The name of the target column inferred from fit is used by default. Returns ------- @@ -59,5 +55,5 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: return safeds.ml._util_sklearn.predict( self._regression, dataset, - target_name if target_name is not None else self.target_name, + self.target_name, ) diff --git a/src/safeds/ml/regression/_decision_tree.py b/src/safeds/ml/regression/_decision_tree.py index c4b242e4d..c3dc49b89 100644 --- a/src/safeds/ml/regression/_decision_tree.py +++ b/src/safeds/ml/regression/_decision_tree.py @@ -1,5 +1,3 @@ -from typing import Optional - # noinspection PyProtectedMember import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable @@ -35,7 +33,7 @@ def fit(self, tagged_table: TaggedTable) -> None: """ self.target_name = safeds.ml._util_sklearn.fit(self._regression, tagged_table) - def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: + def predict(self, dataset: Table) -> Table: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -43,8 +41,6 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: ---------- dataset : Table The dataset containing the feature vectors. - target_name: Optional[str] - The name of the target vector. The name of the target column inferred from fit is used by default. Returns ------- @@ -59,5 +55,5 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: return safeds.ml._util_sklearn.predict( self._regression, dataset, - target_name if target_name is not None else self.target_name, + self.target_name, ) diff --git a/src/safeds/ml/regression/_elastic_net_regression.py b/src/safeds/ml/regression/_elastic_net_regression.py index b8cd5da06..be7c54213 100644 --- a/src/safeds/ml/regression/_elastic_net_regression.py +++ b/src/safeds/ml/regression/_elastic_net_regression.py @@ -1,5 +1,3 @@ -from typing import Optional - # noinspection PyProtectedMember import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable @@ -35,7 +33,7 @@ def fit(self, tagged_table: TaggedTable) -> None: """ self.target_name = safeds.ml._util_sklearn.fit(self._regression, tagged_table) - def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: + def predict(self, dataset: Table) -> Table: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -43,8 +41,6 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: ---------- dataset : Table The dataset containing the feature vectors. - target_name: Optional[str] - The name of the target vector. The name of the target column inferred from fit is used by default. Returns ------- @@ -59,5 +55,5 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: return safeds.ml._util_sklearn.predict( self._regression, dataset, - target_name if target_name is not None else self.target_name, + self.target_name, ) diff --git a/src/safeds/ml/regression/_gradient_boosting_regression.py b/src/safeds/ml/regression/_gradient_boosting_regression.py index bd3d9672d..fe68ad31a 100644 --- a/src/safeds/ml/regression/_gradient_boosting_regression.py +++ b/src/safeds/ml/regression/_gradient_boosting_regression.py @@ -1,5 +1,3 @@ -from typing import Optional - # noinspection PyProtectedMember import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable @@ -37,7 +35,7 @@ def fit(self, tagged_table: TaggedTable) -> None: self.target_name = safeds.ml._util_sklearn.fit(self._regression, tagged_table) # noinspection PyProtectedMember - def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: + def predict(self, dataset: Table) -> Table: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -45,8 +43,6 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: ---------- dataset : Table The dataset containing the feature vectors. - target_name : Optional[str] - The name of the target vector. The name of the target column inferred from fit is used by default. Returns ------- @@ -61,5 +57,5 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: return safeds.ml._util_sklearn.predict( self._regression, dataset, - target_name if target_name is not None else self.target_name, + self.target_name, ) diff --git a/src/safeds/ml/regression/_k_nearest_neighbors.py b/src/safeds/ml/regression/_k_nearest_neighbors.py index ecda4d539..b8d7b9b4b 100644 --- a/src/safeds/ml/regression/_k_nearest_neighbors.py +++ b/src/safeds/ml/regression/_k_nearest_neighbors.py @@ -1,5 +1,3 @@ -from typing import Optional - # noinspection PyProtectedMember import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable @@ -39,7 +37,7 @@ def fit(self, tagged_table: TaggedTable) -> None: """ self.target_name = safeds.ml._util_sklearn.fit(self._regression, tagged_table) - def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: + def predict(self, dataset: Table) -> Table: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -47,8 +45,6 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: ---------- dataset : Table The dataset containing the feature vectors. - target_name: Optional[str] - The name of the target vector. The name of the target column inferred from fit is used by default. Returns ------- @@ -63,5 +59,5 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: return safeds.ml._util_sklearn.predict( self._regression, dataset, - target_name if target_name is not None else self.target_name, + self.target_name, ) diff --git a/src/safeds/ml/regression/_lasso_regression.py b/src/safeds/ml/regression/_lasso_regression.py index 88c79eb9c..3ab76ffd4 100644 --- a/src/safeds/ml/regression/_lasso_regression.py +++ b/src/safeds/ml/regression/_lasso_regression.py @@ -1,5 +1,3 @@ -from typing import Optional - # noinspection PyProtectedMember import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable @@ -35,7 +33,7 @@ def fit(self, tagged_table: TaggedTable) -> None: """ self.target_name = safeds.ml._util_sklearn.fit(self._regression, tagged_table) - def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: + def predict(self, dataset: Table) -> Table: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -43,8 +41,6 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: ---------- dataset : Table The dataset containing the feature vectors. - target_name: Optional[str] - The name of the target vector. The name of the target column inferred from fit is used by default. Returns ------- @@ -59,5 +55,5 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: return safeds.ml._util_sklearn.predict( self._regression, dataset, - target_name if target_name is not None else self.target_name, + self.target_name, ) diff --git a/src/safeds/ml/regression/_linear_regression.py b/src/safeds/ml/regression/_linear_regression.py index 6d748ed2c..0599433dd 100644 --- a/src/safeds/ml/regression/_linear_regression.py +++ b/src/safeds/ml/regression/_linear_regression.py @@ -1,5 +1,3 @@ -from typing import Optional - # noinspection PyProtectedMember import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable @@ -35,7 +33,7 @@ def fit(self, tagged_table: TaggedTable) -> None: """ self.target_name = safeds.ml._util_sklearn.fit(self._regression, tagged_table) - def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: + def predict(self, dataset: Table) -> Table: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -43,8 +41,6 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: ---------- dataset : Table The dataset containing the feature vectors. - target_name: Optional[str] - The name of the target vector. The name of the target column inferred from fit is used by default. Returns ------- @@ -59,5 +55,5 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: return safeds.ml._util_sklearn.predict( self._regression, dataset, - target_name if target_name is not None else self.target_name, + self.target_name, ) diff --git a/src/safeds/ml/regression/_random_forest.py b/src/safeds/ml/regression/_random_forest.py index 3bef1a3cf..d1eec68e6 100644 --- a/src/safeds/ml/regression/_random_forest.py +++ b/src/safeds/ml/regression/_random_forest.py @@ -1,5 +1,3 @@ -from typing import Optional - # noinspection PyProtectedMember import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable @@ -34,7 +32,7 @@ def fit(self, tagged_table: TaggedTable) -> None: """ self.target_name = safeds.ml._util_sklearn.fit(self._regression, tagged_table) - def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: + def predict(self, dataset: Table) -> Table: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -42,8 +40,6 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: ---------- dataset : Table The dataset containing the feature vectors. - target_name : Optional[str] - The name of the target vector. The name of the target column inferred from fit is used by default. Returns ------- @@ -58,5 +54,5 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: return safeds.ml._util_sklearn.predict( self._regression, dataset, - target_name if target_name is not None else self.target_name, + self.target_name, ) diff --git a/src/safeds/ml/regression/_regressor.py b/src/safeds/ml/regression/_regressor.py index 893fa689b..1cb166f1b 100644 --- a/src/safeds/ml/regression/_regressor.py +++ b/src/safeds/ml/regression/_regressor.py @@ -1,5 +1,4 @@ from abc import ABC, abstractmethod -from typing import Optional from safeds.data.tabular.containers import Table, TaggedTable @@ -22,7 +21,7 @@ def fit(self, tagged_table: TaggedTable) -> None: """ @abstractmethod - def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: + def predict(self, dataset: Table) -> Table: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -30,8 +29,6 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: ---------- dataset : Table The dataset containing the feature vectors. - target_name: Optional[str] - The name of the target vector. The name of the target column inferred from fit is used by default. Returns ------- diff --git a/src/safeds/ml/regression/_ridge_regression.py b/src/safeds/ml/regression/_ridge_regression.py index bff53857d..3aa99bf4b 100644 --- a/src/safeds/ml/regression/_ridge_regression.py +++ b/src/safeds/ml/regression/_ridge_regression.py @@ -1,5 +1,3 @@ -from typing import Optional - # noinspection PyProtectedMember import safeds.ml._util_sklearn from safeds.data.tabular.containers import Table, TaggedTable @@ -35,7 +33,7 @@ def fit(self, tagged_table: TaggedTable) -> None: """ self.target_name = safeds.ml._util_sklearn.fit(self._regression, tagged_table) - def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: + def predict(self, dataset: Table) -> Table: """ Predict a target vector using a dataset containing feature vectors. The model has to be trained first. @@ -43,8 +41,6 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: ---------- dataset : Table The dataset containing the feature vectors. - target_name : Optional[str] - The name of the target vector. The name of the target column inferred from fit is used by default Returns ------- @@ -59,5 +55,5 @@ def predict(self, dataset: Table, target_name: Optional[str] = None) -> Table: return safeds.ml._util_sklearn.predict( self._regression, dataset, - target_name if target_name is not None else self.target_name, + self.target_name, )