Skip to content

Commit 3fa9c8a

Browse files
authored
Add code examples in transformation module's docstrings (#646)
* Adding code examples for Transformation module * Fixing details in the examples
1 parent 2f63bd3 commit 3fa9c8a

File tree

6 files changed

+130
-2
lines changed

6 files changed

+130
-2
lines changed

feature_engine/transformation/arcsin.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,24 @@ class ArcsinTransformer(BaseNumericalTransformer):
8080
transform:
8181
Apply the arcsin transformation.
8282
83+
Examples
84+
--------
85+
86+
>>> import numpy as np
87+
>>> import pandas as pd
88+
>>> from feature_engine.transformation import ArcsinTransformer
89+
>>> np.random.seed(42)
90+
>>> X = pd.DataFrame(dict(x = np.random.beta(1, 1, size = 100)))
91+
>>> ast = ArcsinTransformer()
92+
>>> ast.fit(X)
93+
>>> X = ast.transform(X)
94+
>>> X.head()
95+
x
96+
0 0.785437
97+
1 0.253389
98+
2 0.144664
99+
3 0.783236
100+
4 0.650777
83101
"""
84102

85103
def __init__(

feature_engine/transformation/boxcox.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,25 @@ class BoxCoxTransformer(BaseNumericalTransformer):
9393
.. [1] Box and Cox. "An Analysis of Transformations". Read at a RESEARCH MEETING,
9494
1964.
9595
https://rss.onlinelibrary.wiley.com/doi/abs/10.1111/j.2517-6161.1964.tb00553.x
96+
97+
Examples
98+
--------
99+
100+
>>> import numpy as np
101+
>>> import pandas as pd
102+
>>> from feature_engine.transformation import BoxCoxTransformer
103+
>>> np.random.seed(42)
104+
>>> X = pd.DataFrame(dict(x = np.random.lognormal(size = 100)))
105+
>>> bct = BoxCoxTransformer()
106+
>>> bct.fit(X)
107+
>>> X = bct.transform(X)
108+
>>> X.head()
109+
x
110+
0 0.505485
111+
1 -0.137595
112+
2 0.662654
113+
3 1.607518
114+
4 -0.232237
96115
"""
97116

98117
def __init__(

feature_engine/transformation/log.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ class LogTransformer(BaseNumericalTransformer):
7777
transform:
7878
Transform the variables using the logarithm.
7979
80+
Examples
81+
--------
82+
83+
>>> import numpy as np
84+
>>> import pandas as pd
85+
>>> from feature_engine.transformation import LogTransformer
86+
>>> np.random.seed(42)
87+
>>> X = pd.DataFrame(dict(x = np.random.lognormal(size = 100)))
88+
>>> lt = LogTransformer()
89+
>>> lt.fit(X)
90+
>>> X = lt.transform(X)
91+
>>> X.head()
92+
x
93+
0 0.496714
94+
1 -0.138264
95+
2 0.647689
96+
3 1.523030
97+
4 -0.234153
8098
"""
8199

82100
def __init__(
@@ -263,6 +281,24 @@ class LogCpTransformer(BaseNumericalTransformer, FitFromDictMixin):
263281
transform:
264282
Transform the variables with the logarithm of x plus C.
265283
284+
Examples
285+
--------
286+
287+
>>> import numpy as np
288+
>>> import pandas as pd
289+
>>> from feature_engine.transformation import LogCpTransformer
290+
>>> np.random.seed(42)
291+
>>> X = pd.DataFrame(dict(x = np.random.lognormal(size = 100)))
292+
>>> lct = LogCpTransformer()
293+
>>> lct.fit(X)
294+
>>> X = lct.transform(X)
295+
>>> X.head()
296+
x
297+
0 0.944097
298+
1 0.586701
299+
2 1.043204
300+
3 1.707159
301+
4 0.541405
266302
"""
267303

268304
def __init__(

feature_engine/transformation/power.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ class PowerTransformer(BaseNumericalTransformer):
7474
transform:
7575
Apply the power transformation to the variables.
7676
77+
Examples
78+
--------
79+
80+
>>> import numpy as np
81+
>>> import pandas as pd
82+
>>> from feature_engine.transformation import PowerTransformer
83+
>>> np.random.seed(42)
84+
>>> X = pd.DataFrame(dict(x = np.random.lognormal(size = 100)))
85+
>>> pt = PowerTransformer()
86+
>>> pt.fit(X)
87+
>>> X = pt.transform(X)
88+
>>> X.head()
89+
x
90+
0 1.281918
91+
1 0.933203
92+
2 1.382432
93+
3 2.141518
94+
4 0.889517
7795
"""
7896

7997
def __init__(

feature_engine/transformation/reciprocal.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ class ReciprocalTransformer(BaseNumericalTransformer):
7373
transform:
7474
Apply the reciprocal 1 / x transformation.
7575
76+
Examples
77+
--------
78+
79+
>>> import numpy as np
80+
>>> import pandas as pd
81+
>>> from feature_engine.transformation import ReciprocalTransformer
82+
>>> np.random.seed(42)
83+
>>> X = pd.DataFrame(dict(x = 10 - np.random.exponential(size = 100)))
84+
>>> rt = ReciprocalTransformer()
85+
>>> rt.fit(X)
86+
>>> X = rt.transform(X)
87+
>>> X.head()
88+
x
89+
0 0.104924
90+
1 0.143064
91+
2 0.115164
92+
3 0.110047
93+
4 0.101726
7694
"""
7795

7896
def __init__(

feature_engine/transformation/yeojohnson.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,30 @@ class YeoJohnsonTransformer(BaseNumericalTransformer):
7474
References
7575
----------
7676
.. [1] Yeo, In-Kwon and Johnson, Richard (2000).
77-
A new family of power transformations to improve normality or symmetry.
78-
Biometrika, 87, 954-959.
77+
A new family of power transformations to improve normality or symmetry.
78+
Biometrika, 87, 954-959.
7979
8080
.. [2] Weisberg S. "Yeo-Johnson Power Transformations".
8181
https://www.stat.umn.edu/arc/yjpower.pdf
82+
83+
Examples
84+
--------
85+
86+
>>> import numpy as np
87+
>>> import pandas as pd
88+
>>> from feature_engine.transformation import YeoJohnsonTransformer
89+
>>> np.random.seed(42)
90+
>>> X = pd.DataFrame(dict(x = np.random.lognormal(size = 100) - 10))
91+
>>> yjt = YeoJohnsonTransformer()
92+
>>> yjt.fit(X)
93+
>>> X = yjt.transform(X)
94+
>>> X.head()
95+
x
96+
0 -267042.906453
97+
1 -444357.138990
98+
2 -221626.115742
99+
3 -23647.632651
100+
4 -467264.993249
82101
"""
83102

84103
def __init__(

0 commit comments

Comments
 (0)