-
Notifications
You must be signed in to change notification settings - Fork 5
feat: easily create a baseline model #811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
ebcb811
add basic implementation of BaselineClassifier
sibre28 970021c
add basic implementation of BaselineRegressor
sibre28 1e78d18
change stuff to terminate execution after max 30 sec
sibre28 608f1f6
Add data validation
sibre28 91db191
Add tests
sibre28 eab2a39
Linter fixes
sibre28 4b6edd7
Linter fixes
sibre28 ab6296e
add docs
sibre28 1f6347c
Merge branch 'main' into 710-easily-create-a-baseline-model
sibre28 a6e3061
linter fixes
sibre28 f6b1002
linter fixes
sibre28 6af2d0b
linter fixes
sibre28 a1b4153
style: apply automated linter fixes
megalinter-bot 6baff71
Add DatasetMissesTargetError
sibre28 96f6dee
Merge remote-tracking branch 'origin/710-easily-create-a-baseline-mod…
sibre28 da67f73
add docs
sibre28 8e12811
linter fix
sibre28 a62c617
style: apply automated linter fixes
megalinter-bot 310513b
fix model tests
sibre28 967430a
Merge remote-tracking branch 'origin/710-easily-create-a-baseline-mod…
sibre28 b96fbf3
fix codecov
sibre28 8a15d45
style: apply automated linter fixes
megalinter-bot e659d24
Merge branch 'main' into 710-easily-create-a-baseline-model
sibre28 af06e73
rename DatasetMissesTargetError to TargetDataMismatchError
sibre28 faab0a8
style: apply automated linter fixes
megalinter-bot abab90b
style: apply automated linter fixes
megalinter-bot a95a179
Merge branch 'main' into 710-easily-create-a-baseline-model
sibre28 b4b120e
Merge branch 'main' into 710-easily-create-a-baseline-model
sibre28 e7579a0
Merge branch 'main' into 710-easily-create-a-baseline-model
sibre28 d5150c6
Merge branch 'main' into 710-easily-create-a-baseline-model
sibre28 b8229f6
remove todos
sibre28 aa0abf2
Merge remote-tracking branch 'origin/710-easily-create-a-baseline-mod…
sibre28 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
src/safeds/ml/classical/classification/_baseline_classifier.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| import copy | ||
| from concurrent.futures import ALL_COMPLETED, wait | ||
| from typing import Self | ||
|
|
||
| from safeds._validation._check_columns_are_numeric import _check_columns_are_numeric | ||
| from safeds.data.labeled.containers import TabularDataset | ||
| from safeds.exceptions import ( | ||
| DatasetMissesDataError, | ||
| FeatureDataMismatchError, | ||
| ModelNotFittedError, | ||
| TargetDataMismatchError, | ||
| ) | ||
| from safeds.ml.classical.classification import ( | ||
| AdaBoostClassifier, | ||
| Classifier, | ||
| DecisionTreeClassifier, | ||
| GradientBoostingClassifier, | ||
| RandomForestClassifier, | ||
| SupportVectorClassifier, | ||
| ) | ||
|
|
||
|
|
||
| def _fit_single_model(model: Classifier, train_data: TabularDataset) -> Classifier: | ||
| return model.fit(train_data) # pragma: no cover | ||
|
|
||
|
|
||
| def _predict_single_model(model: Classifier, test_data: TabularDataset) -> TabularDataset: | ||
| return model.predict(test_data) # pragma: no cover | ||
|
|
||
|
|
||
| class BaselineClassifier: | ||
| """ | ||
| Baseline Classifier. | ||
|
|
||
| Get a baseline by fitting data on multiple different models and comparing the best metrics. | ||
|
|
||
| Parameters ---------- extended_search: If set to true, an extended set of models will be used to fit the | ||
| classifier. This might result in significantly higher runtime. | ||
| """ | ||
|
|
||
| def __init__(self, extended_search: bool = False): | ||
| self._is_fitted = False | ||
| self._list_of_model_types = [ | ||
| AdaBoostClassifier(), | ||
| DecisionTreeClassifier(), | ||
| SupportVectorClassifier(), | ||
| RandomForestClassifier(), | ||
| ] | ||
| if extended_search: | ||
| self._list_of_model_types.extend([GradientBoostingClassifier()]) # pragma: no cover | ||
|
|
||
| self._fitted_models: list[Classifier] = [] | ||
| self._feature_names: list[str] | None = None | ||
| self._target_name: str = "none" | ||
|
|
||
| def fit(self, train_data: TabularDataset) -> Self: | ||
| """ | ||
| Train the Classifier with given training data. | ||
|
|
||
| The original model is not modified. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| train_data: | ||
| The data the network should be trained on. | ||
|
|
||
| Returns | ||
| ------- | ||
| trained_classifier: | ||
| The trained Classifier | ||
|
|
||
| Raises | ||
| ------ | ||
| DatasetMissesDataError | ||
| If the given train_data contains no data. | ||
| ColumnTypeError | ||
| If one or more columns contain non-numeric values. | ||
| """ | ||
| from concurrent.futures import ProcessPoolExecutor | ||
|
|
||
| # Validate Data | ||
| train_data_as_table = train_data.to_table() | ||
| if train_data_as_table.row_count == 0: | ||
| raise DatasetMissesDataError | ||
| _check_columns_are_numeric(train_data_as_table, train_data.features.add_columns(train_data.target).column_names) | ||
|
|
||
| copied_model = copy.deepcopy(self) | ||
|
|
||
| with ProcessPoolExecutor(max_workers=len(self._list_of_model_types)) as executor: | ||
| futures = [] | ||
| for model in self._list_of_model_types: | ||
| futures.append(executor.submit(_fit_single_model, model, train_data)) | ||
| [done, _] = wait(futures, return_when=ALL_COMPLETED) | ||
| for future in done: | ||
| copied_model._fitted_models.append(future.result()) | ||
| executor.shutdown() | ||
|
|
||
| copied_model._is_fitted = True | ||
| copied_model._feature_names = train_data.features.column_names | ||
| copied_model._target_name = train_data.target.name | ||
| return copied_model | ||
|
|
||
| def predict(self, test_data: TabularDataset) -> dict[str, float]: | ||
| """ | ||
| Make a prediction for the given test data and calculate the best metrics. | ||
|
|
||
| The original Model is not modified. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| test_data: | ||
| The data the Classifier should predict. | ||
|
|
||
| Returns | ||
| ------- | ||
| best_metrics: | ||
| A dictionary with the best metrics that were achieved. | ||
|
|
||
| Raises | ||
| ------ | ||
| ModelNotFittedError | ||
| If the model has not been fitted yet | ||
| FeatureDataMismatchError | ||
| If the features of the test data do not match with the features of the trained Classifier. | ||
| DatasetMissesDataError | ||
| If the given test_data contains no data. | ||
| TargetDataMismatchError | ||
| If the target column of the test data does not match the target column of the training data. | ||
| ColumnTypeError | ||
| If one or more columns contain non-numeric values. | ||
| """ | ||
| from concurrent.futures import ProcessPoolExecutor | ||
|
|
||
| from safeds.ml.metrics import ClassificationMetrics | ||
|
|
||
| if not self._is_fitted: | ||
| raise ModelNotFittedError | ||
|
|
||
| # Validate data | ||
| if not self._feature_names == test_data.features.column_names: | ||
| raise FeatureDataMismatchError | ||
| if not self._target_name == test_data.target.name: | ||
| raise TargetDataMismatchError( | ||
| actual_target_name=test_data.target.name, | ||
| missing_target_name=self._target_name, | ||
| ) | ||
| test_data_as_table = test_data.to_table() | ||
| if test_data_as_table.row_count == 0: | ||
| raise DatasetMissesDataError | ||
| _check_columns_are_numeric(test_data_as_table, test_data.features.add_columns(test_data.target).column_names) | ||
|
|
||
| with ProcessPoolExecutor(max_workers=len(self._list_of_model_types)) as executor: | ||
| results = [] | ||
| futures = [] | ||
| for model in self._fitted_models: | ||
| futures.append(executor.submit(_predict_single_model, model, test_data)) | ||
| [done, _] = wait(futures, return_when=ALL_COMPLETED) | ||
| for future in done: | ||
| results.append(future.result()) | ||
| executor.shutdown() | ||
|
|
||
| max_metrics = {"accuracy": 0.0, "f1score": 0.0, "precision": 0.0, "recall": 0.0} | ||
| for result in results: | ||
| accuracy = ClassificationMetrics.accuracy(result, test_data) | ||
|
|
||
| positive_class = test_data.target.get_value(0) | ||
| f1score = ClassificationMetrics.f1_score(result, test_data, positive_class) | ||
| precision = ClassificationMetrics.precision(result, test_data, positive_class) | ||
| recall = ClassificationMetrics.recall(result, test_data, positive_class) | ||
|
|
||
| if max_metrics.get("accuracy", 0.0) < accuracy: | ||
| max_metrics.update({"accuracy": accuracy}) | ||
|
|
||
| if max_metrics.get("f1score", 0.0) < f1score: | ||
| max_metrics.update({"f1score": f1score}) | ||
|
|
||
| if max_metrics.get("precision", 0.0) < precision: | ||
| max_metrics.update({"precision": precision}) | ||
|
|
||
| if max_metrics.get("recall", 0.0) < recall: | ||
| max_metrics.update({"recall": recall}) | ||
|
|
||
| return max_metrics | ||
|
|
||
| @property | ||
| def is_fitted(self) -> bool: | ||
| """Whether the model is fitted.""" | ||
| return self._is_fitted | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.