Skip to content

Commit

Permalink
feat: remove target_name parameter of predict (#70)
Browse files Browse the repository at this point in the history
### 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 <[email protected]>
  • Loading branch information
lars-reimann and lars-reimann authored Mar 24, 2023
1 parent cfbcd64 commit b513454
Show file tree
Hide file tree
Showing 17 changed files with 32 additions and 98 deletions.
8 changes: 2 additions & 6 deletions src/safeds/ml/classification/_ada_boost.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

# noinspection PyProtectedMember
import safeds.ml._util_sklearn
from safeds.data.tabular.containers import Table, TaggedTable
Expand Down Expand Up @@ -37,16 +35,14 @@ 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.
Parameters
----------
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
-------
Expand All @@ -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,
)
5 changes: 1 addition & 4 deletions src/safeds/ml/classification/_classifier.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from abc import ABC, abstractmethod
from typing import Optional

from safeds.data.tabular.containers import Table, TaggedTable

Expand All @@ -22,16 +21,14 @@ 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.
Parameters
----------
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
-------
Expand Down
8 changes: 2 additions & 6 deletions src/safeds/ml/classification/_decision_tree.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

# noinspection PyProtectedMember
import safeds.ml._util_sklearn
from safeds.data.tabular.containers import Table, TaggedTable
Expand Down Expand Up @@ -37,16 +35,14 @@ 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.
Parameters
----------
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
-------
Expand All @@ -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,
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

# noinspection PyProtectedMember
import safeds.ml._util_sklearn
from safeds.data.tabular.containers import Table, TaggedTable
Expand Down Expand Up @@ -38,16 +36,14 @@ 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.
Parameters
----------
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
-------
Expand All @@ -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,
)
8 changes: 2 additions & 6 deletions src/safeds/ml/classification/_k_nearest_neighbors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

# noinspection PyProtectedMember
import safeds.ml._util_sklearn
from safeds.data.tabular.containers import Table, TaggedTable
Expand Down Expand Up @@ -41,16 +39,14 @@ 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
Parameters
----------
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
-------
Expand All @@ -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,
)
8 changes: 2 additions & 6 deletions src/safeds/ml/classification/_logistic_regression.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

# noinspection PyProtectedMember
import safeds.ml._util_sklearn
from safeds.data.tabular.containers import Table, TaggedTable
Expand Down Expand Up @@ -37,16 +35,14 @@ 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.
Parameters
----------
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
-------
Expand All @@ -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,
)
8 changes: 2 additions & 6 deletions src/safeds/ml/classification/_random_forest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

# noinspection PyProtectedMember
import safeds.ml._util_sklearn
from safeds.data.tabular.containers import Table, TaggedTable
Expand Down Expand Up @@ -36,16 +34,14 @@ 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.
Parameters
----------
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
-------
Expand All @@ -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,
)
8 changes: 2 additions & 6 deletions src/safeds/ml/regression/_ada_boost.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

# noinspection PyProtectedMember
import safeds.ml._util_sklearn
from safeds.data.tabular.containers import Table, TaggedTable
Expand Down Expand Up @@ -35,16 +33,14 @@ 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.
Parameters
----------
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
-------
Expand All @@ -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,
)
8 changes: 2 additions & 6 deletions src/safeds/ml/regression/_decision_tree.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

# noinspection PyProtectedMember
import safeds.ml._util_sklearn
from safeds.data.tabular.containers import Table, TaggedTable
Expand Down Expand Up @@ -35,16 +33,14 @@ 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.
Parameters
----------
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
-------
Expand All @@ -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,
)
8 changes: 2 additions & 6 deletions src/safeds/ml/regression/_elastic_net_regression.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

# noinspection PyProtectedMember
import safeds.ml._util_sklearn
from safeds.data.tabular.containers import Table, TaggedTable
Expand Down Expand Up @@ -35,16 +33,14 @@ 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.
Parameters
----------
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
-------
Expand All @@ -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,
)
8 changes: 2 additions & 6 deletions src/safeds/ml/regression/_gradient_boosting_regression.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

# noinspection PyProtectedMember
import safeds.ml._util_sklearn
from safeds.data.tabular.containers import Table, TaggedTable
Expand Down Expand Up @@ -37,16 +35,14 @@ 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.
Parameters
----------
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
-------
Expand All @@ -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,
)
8 changes: 2 additions & 6 deletions src/safeds/ml/regression/_k_nearest_neighbors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

# noinspection PyProtectedMember
import safeds.ml._util_sklearn
from safeds.data.tabular.containers import Table, TaggedTable
Expand Down Expand Up @@ -39,16 +37,14 @@ 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.
Parameters
----------
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
-------
Expand All @@ -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,
)
8 changes: 2 additions & 6 deletions src/safeds/ml/regression/_lasso_regression.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

# noinspection PyProtectedMember
import safeds.ml._util_sklearn
from safeds.data.tabular.containers import Table, TaggedTable
Expand Down Expand Up @@ -35,16 +33,14 @@ 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.
Parameters
----------
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
-------
Expand All @@ -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,
)
Loading

0 comments on commit b513454

Please sign in to comment.