PowerTransformer¶
yohou.preprocessing.PowerTransformer
¶
Bases: SklearnTransformer
Apply a power transform featurewise to make data more Gaussian-like.
Power transforms are a family of parametric, monotonic transformations that are applied to make data more Gaussian-like. This is useful for modeling issues related to heteroscedasticity (non-constant variance), or other situations where normality is desired.
Currently, PowerTransformer supports the Box-Cox transform and the Yeo-Johnson transform. The optimal parameter for stabilizing variance and minimizing skewness is estimated through maximum likelihood.
Box-Cox requires input data to be strictly positive, while Yeo-Johnson supports both positive and negative data.
This is a Yohou wrapper that preserves the polars DataFrame structure and "time" column.
Parameters ¶
| Name | Type | Description | Default |
|---|---|---|---|
method
|
('yeo-johnson', 'box-cox')
|
The power transform method. Available methods are:
|
'yeo-johnson'
|
standardize
|
bool
|
Set to True to apply zero-mean, unit-variance normalization to the transformed output. |
True
|
copy
|
bool
|
If True, a copy of the data is made before transforming. Pass False only if in-place modification of the input is acceptable. |
True
|
Attributes ¶
| Name | Type | Description |
|---|---|---|
instance_ |
PowerTransformer
|
The fitted sklearn PowerTransformer instance. |
lambdas_ |
ndarray of float, shape (n_features,)
|
The parameters of the power transform for the selected features. |
Examples ¶
>>> import polars as pl
>>> from datetime import datetime
>>> from yohou.preprocessing import PowerTransformer
>>> X = pl.DataFrame({
... "time": [datetime(2024, 1, i) for i in range(1, 6)],
... "value": [1.0, 4.0, 9.0, 16.0, 25.0], # Skewed data
... })
>>> pt = PowerTransformer(method="yeo-johnson")
>>> pt.fit(X)
PowerTransformer(...)
>>> X_transformed = pt.transform(X)
>>> # Data is transformed to be more Gaussian-like
>>> "time" in X_transformed.columns
True
>>> # Can be inverted
>>> X_inv = pt.inverse_transform(X_transformed)
>>> abs(X_inv["value"][0] - 1.0) < 1e-10
True
See Also ¶
QuantileTransformer : Transform features using quantiles information.
Source Code ¶
Source code in src/yohou/preprocessing/sklearn_wrappers.py
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 | |
lambdas_
property
¶
The parameters of the power transform for the selected features.
Tutorials¶
The following example notebooks use this component:
-
How to Use Scikit-learn Scalers
Wrap sklearn scalers (StandardScaler, MinMaxScaler, RobustScaler, PowerTransformer, PolynomialFeatures) for polars DataFrames with inverse transforms.