Skip to content

Commit

Permalink
feat: better names for properties of TaggedTable (#74)
Browse files Browse the repository at this point in the history
### Summary of Changes

In `TaggedTable`:
* Rename `feature_vectors` to `features`
* Rename `target_values` to `target`

---------

Co-authored-by: lars-reimann <[email protected]>
  • Loading branch information
lars-reimann and lars-reimann authored Mar 24, 2023
1 parent 5d5f5a6 commit fee398b
Show file tree
Hide file tree
Showing 37 changed files with 90 additions and 92 deletions.
2 changes: 1 addition & 1 deletion docs/tutorials/machine_learning.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"\n",
"tagged_table = TaggedTable(\n",
" training_set,\n",
" target_column=\"result\"\n",
" target_name=\"result\"\n",
")"
],
"metadata": {
Expand Down
14 changes: 7 additions & 7 deletions src/safeds/data/tabular/containers/_tagged_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ class TaggedTable(Table):
Parameters
----------
table : Table
The table used to derive the feature and target vectors.
target_column : str
The table used to derive the features and target.
target_name : str
Name of the target column.
"""

def __init__(self, table: Table, target_column: str):
def __init__(self, table: Table, target_name: str):
super().__init__(table._data)

self._y: Column = table.get_column(target_column)
self._X: Table = table.drop_columns([target_column])
self._y: Column = table.get_column(target_name)
self._X: Table = table.drop_columns([target_name])

@property
def feature_vectors(self) -> Table:
def features(self) -> Table:
return self._X

@property
def target_values(self) -> Column:
def target(self) -> Column:
return self._y

def __repr__(self) -> str:
Expand Down
14 changes: 7 additions & 7 deletions src/safeds/ml/_util_sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def fit(model: Any, tagged_table: TaggedTable) -> None:
"""
try:
model.fit(
tagged_table.feature_vectors._data,
tagged_table.target_values._data,
tagged_table.features._data,
tagged_table.target._data,
)
except ValueError as exception:
raise LearningError(str(exception)) from exception
Expand All @@ -41,16 +41,16 @@ def predict(model: Any, dataset: Table, target_name: str) -> TaggedTable:
Parameters
----------
model
Classifier or Regression from scikit-learn.
Classifier or regressor from scikit-learn.
dataset : Table
The dataset containing the feature vectors.
The dataset containing the features.
target_name : str
The name of the target vector, the name of the target column inferred from fit is used by default.
The name of the target column.
Returns
-------
table : Table
A dataset containing the given feature vectors and the predicted target vector.
A dataset containing the given features and the predicted target.
Raises
------
Expand All @@ -67,7 +67,7 @@ def predict(model: Any, dataset: Table, target_name: str) -> TaggedTable:
f"Dataset already contains '{target_name}' column. Please rename this column"
)
result_set[target_name] = predicted_target_vector
return TaggedTable(Table(result_set), target_column=target_name)
return TaggedTable(Table(result_set), target_name=target_name)
except NotFittedError as exception:
raise PredictionError("The model was not trained") from exception
except ValueError as exception:
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/ml/classification/_ada_boost.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def fit(self, training_set: TaggedTable) -> None:
If the tagged table contains invalid values or if the training failed.
"""
fit(self._wrapped_classifier, training_set)
self._target_name = training_set.target_values.name
self._target_name = training_set.target.name

def predict(self, dataset: Table) -> TaggedTable:
"""
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/ml/classification/_decision_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def fit(self, training_set: TaggedTable) -> None:
If the tagged table contains invalid values or if the training failed.
"""
fit(self._wrapped_classifier, training_set)
self._target_name = training_set.target_values.name
self._target_name = training_set.target.name

def predict(self, dataset: Table) -> TaggedTable:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def fit(self, training_set: TaggedTable) -> None:
If the tagged table contains invalid values or if the training failed.
"""
fit(self._wrapped_classifier, training_set)
self._target_name = training_set.target_values.name
self._target_name = training_set.target.name

# noinspection PyProtectedMember
def predict(self, dataset: Table) -> TaggedTable:
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/ml/classification/_k_nearest_neighbors.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def fit(self, training_set: TaggedTable) -> None:
If the tagged table contains invalid values or if the training failed.
"""
fit(self._wrapped_classifier, training_set)
self._target_name = training_set.target_values.name
self._target_name = training_set.target.name

def predict(self, dataset: Table) -> TaggedTable:
"""
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/ml/classification/_logistic_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def fit(self, training_set: TaggedTable) -> None:
If the tagged table contains invalid values or if the training failed.
"""
fit(self._wrapped_classifier, training_set)
self._target_name = training_set.target_values.name
self._target_name = training_set.target.name

def predict(self, dataset: Table) -> TaggedTable:
"""
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/ml/classification/_random_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def fit(self, training_set: TaggedTable) -> None:
If the tagged table contains invalid values or if the training failed.
"""
fit(self._wrapped_classifier, training_set)
self._target_name = training_set.target_values.name
self._target_name = training_set.target.name

def predict(self, dataset: Table) -> TaggedTable:
"""
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/ml/regression/_ada_boost.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def fit(self, training_set: TaggedTable) -> None:
If the tagged table contains invalid values or if the training failed.
"""
fit(self._wrapped_regressor, training_set)
self._target_name = training_set.target_values.name
self._target_name = training_set.target.name

def predict(self, dataset: Table) -> TaggedTable:
"""
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/ml/regression/_decision_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def fit(self, training_set: TaggedTable) -> None:
If the tagged table contains invalid values or if the training failed.
"""
fit(self._wrapped_regressor, training_set)
self._target_name = training_set.target_values.name
self._target_name = training_set.target.name

def predict(self, dataset: Table) -> TaggedTable:
"""
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/ml/regression/_elastic_net_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def fit(self, training_set: TaggedTable) -> None:
If the tagged table contains invalid values or if the training failed.
"""
fit(self._wrapped_regressor, training_set)
self._target_name = training_set.target_values.name
self._target_name = training_set.target.name

def predict(self, dataset: Table) -> TaggedTable:
"""
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/ml/regression/_gradient_boosting_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def fit(self, training_set: TaggedTable) -> None:
If the tagged table contains invalid values or if the training failed.
"""
fit(self._wrapped_regressor, training_set)
self._target_name = training_set.target_values.name
self._target_name = training_set.target.name

# noinspection PyProtectedMember

Expand Down
2 changes: 1 addition & 1 deletion src/safeds/ml/regression/_k_nearest_neighbors.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def fit(self, training_set: TaggedTable) -> None:
If the tagged table contains invalid values or if the training failed.
"""
fit(self._wrapped_regressor, training_set)
self._target_name = training_set.target_values.name
self._target_name = training_set.target.name

def predict(self, dataset: Table) -> TaggedTable:
"""
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/ml/regression/_lasso_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def fit(self, training_set: TaggedTable) -> None:
If the tagged table contains invalid values or if the training failed.
"""
fit(self._wrapped_regressor, training_set)
self._target_name = training_set.target_values.name
self._target_name = training_set.target.name

def predict(self, dataset: Table) -> TaggedTable:
"""
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/ml/regression/_linear_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def fit(self, training_set: TaggedTable) -> None:
If the tagged table contains invalid values or if the training failed.
"""
fit(self._wrapped_regressor, training_set)
self._target_name = training_set.target_values.name
self._target_name = training_set.target.name

def predict(self, dataset: Table) -> TaggedTable:
"""
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/ml/regression/_random_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def fit(self, training_set: TaggedTable) -> None:
If the tagged table contains invalid values or if the training failed.
"""
fit(self._wrapped_regressor, training_set)
self._target_name = training_set.target_values.name
self._target_name = training_set.target.name

def predict(self, dataset: Table) -> TaggedTable:
"""
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/ml/regression/_ridge_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def fit(self, training_set: TaggedTable) -> None:
If the tagged table contains invalid values or if the training failed.
"""
fit(self._wrapped_regressor, training_set)
self._target_name = training_set.target_values.name
self._target_name = training_set.target.name

def predict(self, dataset: Table) -> TaggedTable:
"""
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from safeds.data.tabular.containers import Table, TaggedTable
from tests.fixtures import resolve_resource_path


def test_tagged_table_features() -> None:
table = Table.from_csv(resolve_resource_path("test_tagged_table.csv"))
tagged_table = TaggedTable(table, "T")
assert "T" not in tagged_table.features._data
assert tagged_table.features.schema.has_column("A")
assert tagged_table.features.schema.has_column("B")
assert tagged_table.features.schema.has_column("C")
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from tests.fixtures import resolve_resource_path


def test_tagged_table_target_values() -> None:
def test_tagged_table_target() -> None:
table = Table.from_csv(resolve_resource_path("test_tagged_table.csv"))
tagged_table = TaggedTable(table, "T")
assert tagged_table.target_values._data[0] == 0
assert tagged_table.target_values._data[1] == 1
assert tagged_table.target._data[0] == 0
assert tagged_table.target._data[1] == 1
6 changes: 3 additions & 3 deletions tests/safeds/ml/classification/_ada_boost/test_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_ada_boost_predict() -> None:
tagged_table = TaggedTable(table, "T")
ada_boost = AdaBoost()
ada_boost.fit(tagged_table)
ada_boost.predict(tagged_table.feature_vectors)
ada_boost.predict(tagged_table.features)
assert True # This asserts that the predict method succeeds


Expand All @@ -19,7 +19,7 @@ def test_ada_boost_predict_not_fitted() -> None:
tagged_table = TaggedTable(table, "T")
ada_boost = AdaBoost()
with pytest.raises(PredictionError):
ada_boost.predict(tagged_table.feature_vectors)
ada_boost.predict(tagged_table.features)


def test_ada_boost_predict_invalid() -> None:
Expand All @@ -30,4 +30,4 @@ def test_ada_boost_predict_invalid() -> None:
ada_boost = AdaBoost()
ada_boost.fit(tagged_table)
with pytest.raises(PredictionError):
ada_boost.predict(invalid_tagged_table.feature_vectors)
ada_boost.predict(invalid_tagged_table.features)
6 changes: 3 additions & 3 deletions tests/safeds/ml/classification/_decision_tree/test_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_decision_tree_predict() -> None:
tagged_table = TaggedTable(table, "T")
decision_tree = DecisionTree()
decision_tree.fit(tagged_table)
decision_tree.predict(tagged_table.feature_vectors)
decision_tree.predict(tagged_table.features)
assert True # This asserts that the predict method succeeds


Expand All @@ -19,7 +19,7 @@ def test_decision_tree_predict_not_fitted() -> None:
tagged_table = TaggedTable(table, "T")
decision_tree = DecisionTree()
with pytest.raises(PredictionError):
decision_tree.predict(tagged_table.feature_vectors)
decision_tree.predict(tagged_table.features)


def test_decision_tree_predict_invalid() -> None:
Expand All @@ -32,4 +32,4 @@ def test_decision_tree_predict_invalid() -> None:
decision_tree = DecisionTree()
decision_tree.fit(tagged_table)
with pytest.raises(PredictionError):
decision_tree.predict(invalid_tagged_table.feature_vectors)
decision_tree.predict(invalid_tagged_table.features)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_gradient_boosting_predict() -> None:
tagged_table = TaggedTable(table, "T")
gradient_boosting_classification = GradientBoosting()
gradient_boosting_classification.fit(tagged_table)
gradient_boosting_classification.predict(tagged_table.feature_vectors)
gradient_boosting_classification.predict(tagged_table.features)
assert True # This asserts that the predict method succeeds


Expand All @@ -23,7 +23,7 @@ def test_gradient_boosting_predict_not_fitted() -> None:
tagged_table = TaggedTable(table, "T")
gradient_boosting = GradientBoosting()
with pytest.raises(PredictionError):
gradient_boosting.predict(tagged_table.feature_vectors)
gradient_boosting.predict(tagged_table.features)


def test_gradient_boosting_predict_invalid() -> None:
Expand All @@ -38,4 +38,4 @@ def test_gradient_boosting_predict_invalid() -> None:
gradient_boosting = GradientBoosting()
gradient_boosting.fit(tagged_table)
with pytest.raises(PredictionError):
gradient_boosting.predict(invalid_tagged_table.feature_vectors)
gradient_boosting.predict(invalid_tagged_table.features)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_k_nearest_neighbors_predict() -> None:
tagged_table = TaggedTable(table, "T")
k_nearest_neighbors = KNearestNeighborsClassifier(2)
k_nearest_neighbors.fit(tagged_table)
k_nearest_neighbors.predict(tagged_table.feature_vectors)
k_nearest_neighbors.predict(tagged_table.features)
assert True # This asserts that the predict method succeeds


Expand All @@ -19,7 +19,7 @@ def test_k_nearest_neighbors_predict_not_fitted() -> None:
tagged_table = TaggedTable(table, "T")
k_nearest_neighbors = KNearestNeighborsClassifier(2)
with pytest.raises(PredictionError):
k_nearest_neighbors.predict(tagged_table.feature_vectors)
k_nearest_neighbors.predict(tagged_table.features)


def test_k_nearest_neighbors_predict_invalid() -> None:
Expand All @@ -32,4 +32,4 @@ def test_k_nearest_neighbors_predict_invalid() -> None:
k_nearest_neighbors = KNearestNeighborsClassifier(2)
k_nearest_neighbors.fit(tagged_table)
with pytest.raises(PredictionError):
k_nearest_neighbors.predict(invalid_tagged_table.feature_vectors)
k_nearest_neighbors.predict(invalid_tagged_table.features)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_logistic_regression_predict() -> None:
tagged_table = TaggedTable(table, "T")
log_regression = LogisticRegression()
log_regression.fit(tagged_table)
log_regression.predict(tagged_table.feature_vectors)
log_regression.predict(tagged_table.features)
assert True # This asserts that the predict method succeeds


Expand All @@ -19,7 +19,7 @@ def test_logistic_regression_predict_not_fitted() -> None:
tagged_table = TaggedTable(table, "T")
log_regression = LogisticRegression()
with pytest.raises(PredictionError):
log_regression.predict(tagged_table.feature_vectors)
log_regression.predict(tagged_table.features)


def test_logistic_regression_predict_invalid() -> None:
Expand All @@ -32,4 +32,4 @@ def test_logistic_regression_predict_invalid() -> None:
log_regression = LogisticRegression()
log_regression.fit(tagged_table)
with pytest.raises(PredictionError):
log_regression.predict(invalid_tagged_table.feature_vectors)
log_regression.predict(invalid_tagged_table.features)
6 changes: 3 additions & 3 deletions tests/safeds/ml/classification/_random_forest/test_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_random_forest_predict() -> None:
tagged_table = TaggedTable(table, "T")
random_forest = RandomForestClassifier()
random_forest.fit(tagged_table)
random_forest.predict(tagged_table.feature_vectors)
random_forest.predict(tagged_table.features)
assert True # This asserts that the predict method succeeds


Expand All @@ -19,7 +19,7 @@ def test_random_forest_predict_not_fitted() -> None:
tagged_table = TaggedTable(table, "T")
random_forest = RandomForestClassifier()
with pytest.raises(PredictionError):
random_forest.predict(tagged_table.feature_vectors)
random_forest.predict(tagged_table.features)


def test_random_forest_predict_invalid() -> None:
Expand All @@ -32,4 +32,4 @@ def test_random_forest_predict_invalid() -> None:
random_forest = RandomForestClassifier()
random_forest.fit(tagged_table)
with pytest.raises(PredictionError):
random_forest.predict(invalid_tagged_table.feature_vectors)
random_forest.predict(invalid_tagged_table.features)
Loading

0 comments on commit fee398b

Please sign in to comment.