|
2 | 2 |
|
3 | 3 | from typing import TYPE_CHECKING |
4 | 4 |
|
| 5 | +from safeds._utils import _structural_hash |
5 | 6 | from safeds._validation import _check_bounds, _check_columns_exist, _ClosedBound |
6 | 7 | from safeds.data.tabular.containers import Table |
7 | 8 | from safeds.exceptions import ( |
@@ -30,13 +31,36 @@ class Discretizer(TableTransformer): |
30 | 31 | If the given number_of_bins is less than 2. |
31 | 32 | """ |
32 | 33 |
|
33 | | - def __init__(self, number_of_bins: int = 5): |
| 34 | + # ------------------------------------------------------------------------------------------------------------------ |
| 35 | + # Dunder methods |
| 36 | + # ------------------------------------------------------------------------------------------------------------------ |
| 37 | + |
| 38 | + def __init__(self, number_of_bins: int = 5) -> None: |
| 39 | + TableTransformer.__init__(self) |
| 40 | + |
34 | 41 | _check_bounds("number_of_bins", number_of_bins, lower_bound=_ClosedBound(2)) |
35 | 42 |
|
36 | | - self._column_names: list[str] | None = None |
37 | 43 | self._wrapped_transformer: sk_KBinsDiscretizer | None = None |
38 | 44 | self._number_of_bins = number_of_bins |
39 | 45 |
|
| 46 | + def __hash__(self) -> int: |
| 47 | + return _structural_hash( |
| 48 | + TableTransformer.__hash__(self), |
| 49 | + self._number_of_bins, |
| 50 | + ) |
| 51 | + |
| 52 | + # ------------------------------------------------------------------------------------------------------------------ |
| 53 | + # Properties |
| 54 | + # ------------------------------------------------------------------------------------------------------------------ |
| 55 | + |
| 56 | + @property |
| 57 | + def number_of_bins(self) -> int: |
| 58 | + return self._number_of_bins |
| 59 | + |
| 60 | + # ------------------------------------------------------------------------------------------------------------------ |
| 61 | + # Learning and transformation |
| 62 | + # ------------------------------------------------------------------------------------------------------------------ |
| 63 | + |
40 | 64 | def fit(self, table: Table, column_names: list[str] | None) -> Discretizer: |
41 | 65 | """ |
42 | 66 | Learn a transformation for a set of columns in a table. |
@@ -137,62 +161,3 @@ def transform(self, table: Table) -> Table: |
137 | 161 | return Table._from_polars_lazy_frame( |
138 | 162 | table._lazy_frame.update(new_data.lazy()), |
139 | 163 | ) |
140 | | - |
141 | | - @property |
142 | | - def is_fitted(self) -> bool: |
143 | | - """Whether the transformer is fitted.""" |
144 | | - return self._wrapped_transformer is not None |
145 | | - |
146 | | - def get_names_of_added_columns(self) -> list[str]: |
147 | | - """ |
148 | | - Get the names of all new columns that have been added by the Discretizer. |
149 | | -
|
150 | | - Returns |
151 | | - ------- |
152 | | - added_columns: |
153 | | - A list of names of the added columns, ordered as they will appear in the table. |
154 | | -
|
155 | | - Raises |
156 | | - ------ |
157 | | - TransformerNotFittedError |
158 | | - If the transformer has not been fitted yet. |
159 | | - """ |
160 | | - if not self.is_fitted: |
161 | | - raise TransformerNotFittedError |
162 | | - return [] |
163 | | - |
164 | | - def get_names_of_changed_columns(self) -> list[str]: |
165 | | - """ |
166 | | - Get the names of all columns that may have been changed by the Discretizer. |
167 | | -
|
168 | | - Returns |
169 | | - ------- |
170 | | - changed_columns: |
171 | | - The list of (potentially) changed column names, as passed to fit. |
172 | | -
|
173 | | - Raises |
174 | | - ------ |
175 | | - TransformerNotFittedError |
176 | | - If the transformer has not been fitted yet. |
177 | | - """ |
178 | | - if self._column_names is None: |
179 | | - raise TransformerNotFittedError |
180 | | - return self._column_names |
181 | | - |
182 | | - def get_names_of_removed_columns(self) -> list[str]: |
183 | | - """ |
184 | | - Get the names of all columns that have been removed by the Discretizer. |
185 | | -
|
186 | | - Returns |
187 | | - ------- |
188 | | - removed_columns: |
189 | | - A list of names of the removed columns, ordered as they appear in the table the Discretizer was fitted on. |
190 | | -
|
191 | | - Raises |
192 | | - ------ |
193 | | - TransformerNotFittedError |
194 | | - If the transformer has not been fitted yet. |
195 | | - """ |
196 | | - if not self.is_fitted: |
197 | | - raise TransformerNotFittedError |
198 | | - return [] |
0 commit comments