SlidingWindowFunctionTransformer¶
yohou.preprocessing.SlidingWindowFunctionTransformer
¶
Bases: BaseActualTransformer
Transform time series by applying a function over sliding windows.
This transformer applies a user-defined function to sliding windows of the input time series. It is useful for computing rolling aggregates, custom statistics, or any windowed transformation.
The function receives a polars DataFrame containing window_size rows
(one window) and should return a scalar or a 1D array for that window.
Parameters ¶
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
Function to apply to each sliding window. It receives a polars DataFrame with shape (window_size, n_features + 1); the window is a full slice of the input and includes the "time" column, which the function must handle or exclude explicitly (see the example below). It should return: - A scalar (applied to all columns) - A dict mapping column names to scalars - A numpy array of shape (n_features,) - Any other value castable to float (applied to all columns) |
required |
window_size
|
int
|
Size of the sliding window. Must be >= 1. |
1
|
kw_args
|
dict or None
|
Dictionary of additional keyword arguments to pass to func. |
None
|
Attributes ¶
| Name | Type | Description |
|---|---|---|
n_features_in_ |
int
|
Number of features seen during fit. |
feature_names_in_ |
list of str
|
Names of features seen during fit. |
Examples ¶
>>> import polars as pl
>>> from datetime import datetime
>>> import numpy as np
>>> from yohou.preprocessing import SlidingWindowFunctionTransformer
>>> times = pl.datetime_range(
... start=datetime(2020, 1, 1), end=datetime(2020, 1, 10), interval="1d", eager=True
... )
>>> X = pl.DataFrame({"time": times, "value": [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]})
>>> # Compute rolling mean with window size 3
>>> def rolling_mean(window):
... return window.select(pl.all().exclude("time").mean()).to_numpy().flatten()
>>> transformer = SlidingWindowFunctionTransformer(func=rolling_mean, window_size=3)
>>> transformer.fit(X)
SlidingWindowFunctionTransformer(...)
>>> X_t = transformer.transform(X)
>>> len(X_t) # Original length minus (window_size - 1)
8
See Also ¶
LagTransformer: Create lagged features.RollingStatisticsTransformer: Pre-built rolling statistics.FunctionTransformer: Apply function element-wise.
Source Code ¶
Source code in src/yohou/preprocessing/window.py
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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | |
Methods ¶
observation_horizon
property
¶
Return the number of past observations needed.
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
|
Column names of the input features. If |
None
|
Returns ¶
| Type | Description |
|---|---|
list of str
|
Output feature names after transformation. |
Source Code ¶
Source code in src/yohou/preprocessing/window.py
Tutorials¶
The following example notebooks use this component:
-
How to Apply Window Transformations
Feature engineering with LagTransformer, RollingStatisticsTransformer, SlidingWindowFunctionTransformer, and ExponentialMovingAverage on time series data.