Skip to content

Commit

Permalink
feat: move ImputerStrategy to safeds.data.tabular.typing (#174)
Browse files Browse the repository at this point in the history
### Summary of Changes

* Move the abstract base class `ImputerStrategy` to
`safeds.data.tabular.typing` since it is only needed for type
annotations
* Improve documentation

---------

Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
  • Loading branch information
lars-reimann and megalinter-bot committed Apr 4, 2023
1 parent 526b96e commit 205c8e2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/safeds/data/tabular/transformation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"""Classes for transforming tabular data."""

from ._imputer import Imputer, ImputerStrategy
from ._imputer import Imputer
from ._label_encoder import LabelEncoder
from ._one_hot_encoder import OneHotEncoder
from ._table_transformer import InvertibleTableTransformer, TableTransformer

__all__ = [
"Imputer",
"ImputerStrategy",
"LabelEncoder",
"OneHotEncoder",
"InvertibleTableTransformer",
Expand Down
26 changes: 17 additions & 9 deletions src/safeds/data/tabular/transformation/_imputer.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import Any

import pandas as pd
from sklearn.impute import SimpleImputer as sk_SimpleImputer

from safeds.data.tabular.containers import Table
from safeds.data.tabular.transformation._table_transformer import TableTransformer
from safeds.data.tabular.typing import ImputerStrategy
from safeds.exceptions import TransformerNotFittedError, UnknownColumnNameError


class ImputerStrategy(ABC):
@abstractmethod
def _augment_imputer(self, imputer: sk_SimpleImputer) -> None:
pass


class Imputer(TableTransformer):
"""
Impute the data for a given Table.
Replace missing values with the given strategy.
Parameters
----------
strategy : ImputerStrategy
The strategy used to impute missing values.
The strategy used to impute missing values. Use the classes nested inside `Imputer.Strategy` to specify it.
Examples
--------
>>> from safeds.data.tabular.containers import Column, Table
>>> from safeds.data.tabular.transformation import Imputer
>>>
>>> table = Table.from_columns(
... [
... Column("a", [1, 3, None]),
... Column("b", [None, 2, 3]),
... ],
... )
>>> transformer = Imputer(Imputer.Strategy.Constant(0))
>>> transformed_table = transformer.fit_and_transform(table)
"""

class Strategy:
Expand Down
2 changes: 2 additions & 0 deletions src/safeds/data/tabular/typing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""Types used to define the schema of a tabular dataset."""

from ._column_type import Anything, Boolean, ColumnType, Integer, RealNumber, String
from ._imputer_strategy import ImputerStrategy
from ._schema import Schema

__all__ = [
"Anything",
"Boolean",
"ColumnType",
"ImputerStrategy",
"Integer",
"RealNumber",
"Schema",
Expand Down
15 changes: 15 additions & 0 deletions src/safeds/data/tabular/typing/_imputer_strategy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from abc import ABC, abstractmethod

from sklearn.impute import SimpleImputer as sk_SimpleImputer


class ImputerStrategy(ABC):
"""
The abstract base class of the different imputation strategies supported by the `Imputer`.
This class is only needed for type annotations. Use the subclasses nested inside `Imputer.Strategy` instead.
"""

@abstractmethod
def _augment_imputer(self, imputer: sk_SimpleImputer) -> None:
pass
3 changes: 2 additions & 1 deletion tests/safeds/data/tabular/transformation/test_imputer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from safeds.data.tabular.containers import Column, Table
from safeds.data.tabular.transformation import Imputer, ImputerStrategy
from safeds.data.tabular.transformation import Imputer
from safeds.data.tabular.typing import ImputerStrategy
from safeds.exceptions import TransformerNotFittedError, UnknownColumnNameError


Expand Down

0 comments on commit 205c8e2

Please sign in to comment.