Skip to content
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

feat: move ImputerStrategy to safeds.data.tabular.typing #174

Merged
merged 4 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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