MeanLagTransformer¶
yohou.preprocessing.window.MeanLagTransformer
¶
Bases: BaseTransformer
Create mean-lagged features by averaging across lag multiples.
For each input column and each base lag k, this transformer computes
the arithmetic mean of the column shifted by k, 2k, ..., n_lags * k
time steps. This captures averaged seasonal patterns as features for
supervised learning.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
lag
|
int >= 1 or list of ints >= 1
|
Base lag(s) to create. Can be a single integer or a list of integers. Each lag value must be >= 1. |
1
|
n_lags
|
int >= 1
|
Number of lag multiples to average. For a base lag |
1
|
Attributes¶
| Name | Type | Description |
|---|---|---|
lags_ |
list of int
|
Effective list of base lags used for transformation. |
Examples¶
>>> import polars as pl
>>> from datetime import datetime
>>> from yohou.preprocessing import MeanLagTransformer
>>> # Create sample data
>>> X = pl.DataFrame({
... "time": [datetime(2020, 1, i) for i in range(1, 13)],
... "value": list(range(12)),
... })
>>> # Average lag-3 and lag-6 into a single feature
>>> transformer = MeanLagTransformer(lag=3, n_lags=2)
>>> transformer.fit(X)
MeanLagTransformer(...)
>>> X_t = transformer.transform(X)
>>> X_t.columns
['time', 'value_mean_lag_3']
>>> len(X_t) # First 6 rows dropped (max(lag) * n_lags = 6)
6
See Also¶
LagTransformer : Creates individual lag features without averaging.
RollingStatisticsTransformer : Compute rolling statistics over consecutive windows.
Notes¶
For a base lag k and n_lags=3, the output at time t is
mean(x[t-k], x[t-2k], x[t-3k]). This differs from
RollingStatisticsTransformer which uses consecutive time steps
rather than seasonal multiples.
The first max(lags) * n_lags rows are dropped because they contain
incomplete lookback windows, setting
observation_horizon = max(lags) * n_lags.
When n_lags=1 the output values are identical to LagTransformer
and the original column dtype is preserved. When n_lags > 1 the
averaging produces Float64 columns.
Output column names follow the pattern {input_col}_mean_lag_{k}.
Source Code¶
Show/Hide source
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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | |
Methods¶
__sklearn_tags__()
¶
fit(X, y=None, **params)
¶
Fit the transformer to input data.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Input time series with a |
required |
y
|
DataFrame or None
|
Ignored. Present for API compatibility with yohou pipelines. |
None
|
**params
|
dict
|
Metadata to route to nested estimators. |
{}
|
Returns¶
| Type | Description |
|---|---|
self
|
The fitted transformer instance. |
Raises¶
| Type | Description |
|---|---|
ValueError
|
If |
Source Code¶
Show/Hide source
transform(X, **params)
¶
Transform the input time series.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
Input time series with a |
required |
**params
|
dict
|
Metadata to route to nested estimators. |
{}
|
Returns¶
| Type | Description |
|---|---|
DataFrame
|
Transformed time series with a |
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
|
array-like of str or None
|
Column names of the input features. If |
None
|
Returns¶
| Type | Description |
|---|---|
list of str
|
Output feature names after transformation. |