SklearnTransformer¶
yohou.preprocessing.sklearn_base.SklearnTransformer
¶
Bases: BaseClassWrapper, BaseTransformer
Wrapper to integrate sklearn transformers into the Yohou pipeline.
Preserves the polars DataFrame structure and "time" column while applying sklearn scaling transformations to numeric columns.
This class can be used to:
- Wrap any sklearn-compatible transformer for use in yohou pipelines
- Serve as a base class for creating yohou transformer extensions
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
transformer
|
type
|
The sklearn transformer class to wrap. Must be a subclass of
|
None
|
**params
|
dict
|
Parameters passed to the underlying sklearn transformer constructor. See the documentation of the specific transformer for available parameters. |
{}
|
Attributes¶
| Name | Type | Description |
|---|---|---|
instance_ |
TransformerMixin
|
The fitted sklearn transformer instance (created by |
Examples¶
>>> import polars as pl
>>> from datetime import datetime
>>> from sklearn.preprocessing import StandardScaler as SklearnStandardScaler
>>> from yohou.preprocessing import SklearnTransformer
>>> X = pl.DataFrame({
... "time": [datetime(2024, 1, i) for i in range(1, 6)],
... "value": [10.0, 20.0, 30.0, 40.0, 50.0],
... })
>>> transformer = SklearnTransformer(transformer=SklearnStandardScaler, with_mean=True)
>>> transformer.fit(X)
SklearnTransformer(...)
>>> X_transformed = transformer.transform(X)
>>> "time" in X_transformed.columns
True
See Also¶
StandardScaler: Pre-configured wrapper for sklearn's StandardScaler.MinMaxScaler: Pre-configured wrapper for sklearn's MinMaxScaler.RobustScaler: Pre-configured wrapper for sklearn's RobustScaler.MaxAbsScaler: Pre-configured wrapper for sklearn's MaxAbsScaler.
Source Code¶
Show/Hide source
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |
Methods¶
__sklearn_tags__()
¶
Get estimator tags.
Override to ensure stateful=False before and after fit. The invertible tag is set dynamically based on whether the wrapped transformer has inverse_transform.
Returns¶
| Type | Description |
|---|---|
Tags
|
Estimator tags with stateful=False and invertible based on underlying transformer. |
Source Code¶
Show/Hide source
fit(X, y=None, **params)
¶
Fit the transformer to the data.
Computes scaling parameters (e.g., mean, std, min, max) from the training data, excluding the "time" column.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Input time series with "time" column. |
required |
y
|
DataFrame or None
|
Target time series. Ignored and only present for API consistency. |
None
|
**params
|
dict
|
Metadata to route to nested estimators. |
{}
|
Returns¶
| Type | Description |
|---|---|
self
|
Fitted transformer. |
Raises¶
| Type | Description |
|---|---|
ValueError
|
If X does not have a "time" column. |
Source Code¶
Show/Hide source
transform(X, **params)
¶
Transform the input time series.
Applies the learned scaling transformation to each feature.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Feature time series with "time" column. |
required |
**params
|
dict
|
Metadata to route to nested estimators. |
{}
|
Returns¶
| Type | Description |
|---|---|
DataFrame
|
Transformed time series with "time" column preserved. |
Source Code¶
Show/Hide source
inverse_transform(X_t, X_p=None, **params)
¶
Apply the inverse transformer transformation to the data.
This method is only available if the underlying sklearn transformer supports inverse_transform (e.g., StandardScaler, PowerTransformer).
Reverts the scaling transformation, restoring the original data scale.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
X_t
|
DataFrame
|
Scaled features with "time" column. |
required |
X_p
|
DataFrame or None
|
Past observations for stateful inverse transformation. Ignored for sklearn wrappers since sklearn transformers are stateless. |
None
|
**params
|
dict
|
Metadata to route to nested estimators. |
{}
|
Returns¶
| Type | Description |
|---|---|
DataFrame
|
Unscaled features with "time" column preserved. |
Source Code¶
Show/Hide source
get_feature_names_out(input_features=None)
¶
Get output feature names for transformation.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
input_features
|
list of str or None
|
Input features. If None, uses feature names from fit. |
None
|
Returns¶
| Type | Description |
|---|---|
list of str
|
Transformed feature names (same as input features for transformers). |