Skip to content

Commit

Permalink
feat: allow using tables that already contain target for prediction (#…
Browse files Browse the repository at this point in the history
…687)

Closes #636

### Summary of Changes

No longer raise an error if a table that already contains the target is
passed to `predict`. It's now simply ignored for training and
overwritten.
  • Loading branch information
lars-reimann authored May 1, 2024
1 parent 841657f commit e9f1cfb
Show file tree
Hide file tree
Showing 24 changed files with 0 additions and 72 deletions.
3 changes: 0 additions & 3 deletions src/safeds/exceptions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
OutOfBoundsError,
)
from safeds.exceptions._ml import (
DatasetContainsTargetError,
DatasetMissesDataError,
DatasetMissesFeaturesError,
FeatureDataMismatchError,
Expand Down Expand Up @@ -63,7 +62,6 @@
"ValueNotPresentWhenFittedError": "._data:ValueNotPresentWhenFittedError",
"WrongFileExtensionError": "._data:WrongFileExtensionError",
# ML exceptions
"DatasetContainsTargetError": "._ml:DatasetContainsTargetError",
"DatasetMissesDataError": "._ml:DatasetMissesDataError",
"DatasetMissesFeaturesError": "._ml:DatasetMissesFeaturesError",
"FeatureDataMismatchError": "._ml:FeatureDataMismatchError",
Expand Down Expand Up @@ -100,7 +98,6 @@
"ValueNotPresentWhenFittedError",
"WrongFileExtensionError",
# ML exceptions
"DatasetContainsTargetError",
"DatasetMissesDataError",
"DatasetMissesFeaturesError",
"FeatureDataMismatchError",
Expand Down
14 changes: 0 additions & 14 deletions src/safeds/exceptions/_ml.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
class DatasetContainsTargetError(ValueError):
"""
Raised when a dataset contains the target column already.
Parameters
----------
target_name:
The name of the target column.
"""

def __init__(self, target_name: str):
super().__init__(f"Dataset already contains the target column '{target_name}'.")


class DatasetMissesFeaturesError(ValueError):
"""
Raised when a dataset misses feature columns.
Expand Down
5 changes: 0 additions & 5 deletions src/safeds/ml/classical/_util_sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from safeds.data.labeled.containers import TabularDataset
from safeds.data.tabular.containers import Table
from safeds.exceptions import (
DatasetContainsTargetError,
DatasetMissesDataError,
DatasetMissesFeaturesError,
LearningError,
Expand Down Expand Up @@ -102,8 +101,6 @@ def predict(model: Any, dataset: Table, feature_names: list[str] | None, target_
------
ModelNotFittedError
If the model has not been fitted yet.
DatasetContainsTargetError
If the dataset contains the target column already.
DatasetMissesFeaturesError
If the dataset misses feature columns.
PredictionError
Expand All @@ -118,8 +115,6 @@ def predict(model: Any, dataset: Table, feature_names: list[str] | None, target_
# Validation
if model is None or target_name is None or feature_names is None:
raise ModelNotFittedError
if dataset.has_column(target_name):
raise DatasetContainsTargetError(target_name)
missing_feature_names = [feature_name for feature_name in feature_names if not dataset.has_column(feature_name)]
if missing_feature_names:
raise DatasetMissesFeaturesError(missing_feature_names)
Expand Down
2 changes: 0 additions & 2 deletions src/safeds/ml/classical/classification/_ada_boost.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ def predict(self, dataset: Table) -> TabularDataset:
------
ModelNotFittedError
If the model has not been fitted yet.
DatasetContainsTargetError
If the dataset contains the target column already.
DatasetMissesFeaturesError
If the dataset misses feature columns.
PredictionError
Expand Down
2 changes: 0 additions & 2 deletions src/safeds/ml/classical/classification/_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ def predict(self, dataset: Table) -> TabularDataset:
------
ModelNotFittedError
If the model has not been fitted yet.
DatasetContainsTargetError
If the dataset contains the target column already.
DatasetMissesFeaturesError
If the dataset misses feature columns.
PredictionError
Expand Down
2 changes: 0 additions & 2 deletions src/safeds/ml/classical/classification/_decision_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ def predict(self, dataset: Table) -> TabularDataset:
------
ModelNotFittedError
If the model has not been fitted yet.
DatasetContainsTargetError
If the dataset contains the target column already.
DatasetMissesFeaturesError
If the dataset misses feature columns.
PredictionError
Expand Down
2 changes: 0 additions & 2 deletions src/safeds/ml/classical/classification/_gradient_boosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,6 @@ def predict(self, dataset: Table) -> TabularDataset:
------
ModelNotFittedError
If the model has not been fitted yet.
DatasetContainsTargetError
If the dataset contains the target column already.
DatasetMissesFeaturesError
If the dataset misses feature columns.
PredictionError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ def predict(self, dataset: Table) -> TabularDataset:
------
ModelNotFittedError
If the model has not been fitted yet.
DatasetContainsTargetError
If the dataset contains the target column already.
DatasetMissesFeaturesError
If the dataset misses feature columns.
PredictionError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ def predict(self, dataset: Table) -> TabularDataset:
------
ModelNotFittedError
If the model has not been fitted yet.
DatasetContainsTargetError
If the dataset contains the target column already.
DatasetMissesFeaturesError
If the dataset misses feature columns.
PredictionError
Expand Down
2 changes: 0 additions & 2 deletions src/safeds/ml/classical/classification/_random_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ def predict(self, dataset: Table) -> TabularDataset:
------
ModelNotFittedError
If the model has not been fitted yet.
DatasetContainsTargetError
If the dataset contains the target column already.
DatasetMissesFeaturesError
If the dataset misses feature columns.
PredictionError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,6 @@ def predict(self, dataset: Table) -> TabularDataset:
------
ModelNotFittedError
If the model has not been fitted yet.
DatasetContainsTargetError
If the dataset contains the target column already.
DatasetMissesFeaturesError
If the dataset misses feature columns.
PredictionError
Expand Down
2 changes: 0 additions & 2 deletions src/safeds/ml/classical/regression/_ada_boost.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ def predict(self, dataset: Table) -> TabularDataset:
------
ModelNotFittedError
If the model has not been fitted yet.
DatasetContainsTargetError
If the dataset contains the target column already.
DatasetMissesFeaturesError
If the dataset misses feature columns.
PredictionError
Expand Down
2 changes: 0 additions & 2 deletions src/safeds/ml/classical/regression/_decision_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ def predict(self, dataset: Table) -> TabularDataset:
------
ModelNotFittedError
If the model has not been fitted yet.
DatasetContainsTargetError
If the dataset contains the target column already.
DatasetMissesFeaturesError
If the dataset misses feature columns.
PredictionError
Expand Down
2 changes: 0 additions & 2 deletions src/safeds/ml/classical/regression/_elastic_net_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,6 @@ def predict(self, dataset: Table) -> TabularDataset:
------
ModelNotFittedError
If the model has not been fitted yet.
DatasetContainsTargetError
If the dataset contains the target column already.
DatasetMissesFeaturesError
If the dataset misses feature columns.
PredictionError
Expand Down
2 changes: 0 additions & 2 deletions src/safeds/ml/classical/regression/_gradient_boosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,6 @@ def predict(self, dataset: Table) -> TabularDataset:
------
ModelNotFittedError
If the model has not been fitted yet.
DatasetContainsTargetError
If the dataset contains the target column already.
DatasetMissesFeaturesError
If the dataset misses feature columns.
PredictionError
Expand Down
2 changes: 0 additions & 2 deletions src/safeds/ml/classical/regression/_k_nearest_neighbors.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ def predict(self, dataset: Table) -> TabularDataset:
------
ModelNotFittedError
If the model has not been fitted yet.
DatasetContainsTargetError
If the dataset contains the target column already.
DatasetMissesFeaturesError
If the dataset misses feature columns.
PredictionError
Expand Down
2 changes: 0 additions & 2 deletions src/safeds/ml/classical/regression/_lasso_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ def predict(self, dataset: Table) -> TabularDataset:
------
ModelNotFittedError
If the model has not been fitted yet.
DatasetContainsTargetError
If the dataset contains the target column already.
DatasetMissesFeaturesError
If the dataset misses feature columns.
PredictionError
Expand Down
2 changes: 0 additions & 2 deletions src/safeds/ml/classical/regression/_linear_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ def predict(self, dataset: Table) -> TabularDataset:
------
ModelNotFittedError
If the model has not been fitted yet.
DatasetContainsTargetError
If the dataset contains the target column already.
DatasetMissesFeaturesError
If the dataset misses feature columns.
PredictionError
Expand Down
2 changes: 0 additions & 2 deletions src/safeds/ml/classical/regression/_random_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ def predict(self, dataset: Table) -> TabularDataset:
------
ModelNotFittedError
If the model has not been fitted yet.
DatasetContainsTargetError
If the dataset contains the target column already.
DatasetMissesFeaturesError
If the dataset misses feature columns.
PredictionError
Expand Down
2 changes: 0 additions & 2 deletions src/safeds/ml/classical/regression/_regressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ def predict(self, dataset: Table) -> TabularDataset:
------
ModelNotFittedError
If the model has not been fitted yet.
DatasetContainsTargetError
If the dataset contains the target column already.
DatasetMissesFeaturesError
If the dataset misses feature columns.
PredictionError
Expand Down
2 changes: 0 additions & 2 deletions src/safeds/ml/classical/regression/_ridge_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ def predict(self, dataset: Table) -> TabularDataset:
------
ModelNotFittedError
If the model has not been fitted yet.
DatasetContainsTargetError
If the dataset contains the target column already.
DatasetMissesFeaturesError
If the dataset misses feature columns.
PredictionError
Expand Down
2 changes: 0 additions & 2 deletions src/safeds/ml/classical/regression/_support_vector_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,6 @@ def predict(self, dataset: Table) -> TabularDataset:
------
ModelNotFittedError
If the model has not been fitted yet.
DatasetContainsTargetError
If the dataset contains the target column already.
DatasetMissesFeaturesError
If the dataset misses feature columns.
PredictionError
Expand Down
6 changes: 0 additions & 6 deletions tests/safeds/ml/classical/classification/test_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import pytest
from safeds.data.tabular.containers import Table
from safeds.exceptions import (
DatasetContainsTargetError,
DatasetMissesDataError,
DatasetMissesFeaturesError,
MissingValuesColumnError,
Expand Down Expand Up @@ -190,11 +189,6 @@ def test_should_raise_if_not_fitted(self, classifier: Classifier, valid_data: Ta
with pytest.raises(ModelNotFittedError):
classifier.predict(valid_data.features)

def test_should_raise_if_dataset_contains_target(self, classifier: Classifier, valid_data: TabularDataset) -> None:
fitted_classifier = classifier.fit(valid_data)
with pytest.raises(DatasetContainsTargetError, match="target"):
fitted_classifier.predict(valid_data.to_table())

def test_should_raise_if_dataset_misses_features(self, classifier: Classifier, valid_data: TabularDataset) -> None:
fitted_classifier = classifier.fit(valid_data)
with pytest.raises(DatasetMissesFeaturesError, match="[feat1, feat2]"):
Expand Down
6 changes: 0 additions & 6 deletions tests/safeds/ml/classical/regression/test_regressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from safeds.data.tabular.containers import Column, Table
from safeds.exceptions import (
ColumnLengthMismatchError,
DatasetContainsTargetError,
DatasetMissesDataError,
DatasetMissesFeaturesError,
MissingValuesColumnError,
Expand Down Expand Up @@ -191,11 +190,6 @@ def test_should_raise_if_not_fitted(self, regressor: Regressor, valid_data: Tabu
with pytest.raises(ModelNotFittedError):
regressor.predict(valid_data.features)

def test_should_raise_if_dataset_contains_target(self, regressor: Regressor, valid_data: TabularDataset) -> None:
fitted_regressor = regressor.fit(valid_data)
with pytest.raises(DatasetContainsTargetError, match="target"):
fitted_regressor.predict(valid_data.to_table())

def test_should_raise_if_dataset_misses_features(self, regressor: Regressor, valid_data: TabularDataset) -> None:
fitted_regressor = regressor.fit(valid_data)
with pytest.raises(DatasetMissesFeaturesError, match="[feat1, feat2]"):
Expand Down

0 comments on commit e9f1cfb

Please sign in to comment.