From 321fe817c212ef5b1b54ea036248a579207d639f Mon Sep 17 00:00:00 2001 From: datacubeR Date: Sun, 20 Nov 2022 23:34:48 -0300 Subject: [PATCH] Adding Creation and Datetime Module Examples --- feature_engine/creation/cyclical_features.py | 19 +++++++++++++ feature_engine/creation/math_features.py | 30 ++++++++++++++++++++ feature_engine/creation/relative_features.py | 16 +++++++++++ feature_engine/datetime/datetime.py | 14 +++++++++ 4 files changed, 79 insertions(+) diff --git a/feature_engine/creation/cyclical_features.py b/feature_engine/creation/cyclical_features.py index 180b33308..1b519a461 100644 --- a/feature_engine/creation/cyclical_features.py +++ b/feature_engine/creation/cyclical_features.py @@ -100,6 +100,25 @@ class CyclicalFeatures( https://towardsdatascience.com/cyclical-features-encoding-its-about-time-ce23581845ca http://blog.davidkaleko.com/feature-engineering-cyclical-features.html + + Examples + -------- + + >>> import pandas as pd + >>> from feature_engine.creation import CyclicalFeatures + >>> X = pd.DataFrame(dict(x= [1,4,3,3,4,2,1,2])) + >>> cf = CyclicalFeatures() + >>> cf.fit(X) + >>> cf.transform(X) + x x_sin x_cos + 0 1 1.000000e+00 6.123234e-17 + 1 4 -2.449294e-16 1.000000e+00 + 2 3 -1.000000e+00 -1.836970e-16 + 3 3 -1.000000e+00 -1.836970e-16 + 4 4 -2.449294e-16 1.000000e+00 + 5 2 1.224647e-16 -1.000000e+00 + 6 1 1.000000e+00 6.123234e-17 + 7 2 1.224647e-16 -1.000000e+00 """ def __init__( diff --git a/feature_engine/creation/math_features.py b/feature_engine/creation/math_features.py index 6f798ecf5..5a74534eb 100644 --- a/feature_engine/creation/math_features.py +++ b/feature_engine/creation/math_features.py @@ -100,6 +100,36 @@ class MathFeatures(BaseCreation): In insurance, we can sum the damage to various parts of a car to obtain the total damage. + + Examples + -------- + + >>> import pandas as pd + >>> from feature_engine.creation import MathFeatures + >>> X = pd.DataFrame(dict(x1 = [1,2,3], x2 = [4,5,6])) + >>> mf = MathFeatures(variables = ["x1","x2"], func = "sum") + >>> mf.fit(X) + >>> mf.transform(X) + x1 x2 sum_x1_x2 + 0 1 4 5 + 1 2 5 7 + 2 3 6 9 + + >>> mf = MathFeatures(variables = ["x1","x2"], func = "prod") + >>> mf.fit(X) + >>> mf.transform(X) + x1 x2 prod_x1_x2 + 0 1 4 4 + 1 2 5 10 + 2 3 6 18 + + >>> mf = MathFeatures(variables = ["x1","x2"], func = "mean") + >>> mf.fit(X) + >>> mf.transform(X)) + x1 x2 mean_x1_x2 + 0 1 4 2.5 + 1 2 5 3.5 + 2 3 6 4.5 """ def __init__( diff --git a/feature_engine/creation/relative_features.py b/feature_engine/creation/relative_features.py index 352f31869..5d6a23a73 100644 --- a/feature_engine/creation/relative_features.py +++ b/feature_engine/creation/relative_features.py @@ -105,6 +105,22 @@ class RelativeFeatures(BaseCreation): - Ratio between income and debt to create the debt_to_income_ratio. - Subtraction of rent from income to obtain the disposable_income. + + Examples + -------- + + >>> import pandas as pd + >>> from feature_engine.creation import RelativeFeatures + >>> X = pd.DataFrame(dict(x1 = [1,2,3], x2 = [4,5,6], x3 = [3,4,5])) + >>> rf = RelativeFeatures(variables = ["x1","x2"], + >>> reference = ["x3"], + >>> func = ["div"]) + >>> rf.fit(X) + >>> rf.transform(X) + x1 x2 x3 x1_div_x3 x2_div_x3 + 0 1 4 3 0.333333 1.333333 + 1 2 5 4 0.500000 1.250000 + 2 3 6 5 0.600000 1.200000 """ def __init__( diff --git a/feature_engine/datetime/datetime.py b/feature_engine/datetime/datetime.py index d329cba95..b2a92e19e 100644 --- a/feature_engine/datetime/datetime.py +++ b/feature_engine/datetime/datetime.py @@ -152,6 +152,20 @@ class DatetimeFeatures(BaseEstimator, TransformerMixin, GetFeatureNamesOutMixin) -------- pandas.to_datetime pandas.dt + + Examples + -------- + + >>> import pandas as pd + >>> from feature_engine.datetime import DatetimeFeatures + >>> X = pd.DataFrame(dict(date = ["2022-09-18", "2022-10-27", "2022-12-24"])) + >>> dtf = DatetimeFeatures(features_to_extract = ["year", "month", "day_of_month"]) + >>> dtf.fit(X) + >>> dtf.transform(X) + date_year date_month date_day_of_month + 0 2022 9 18 + 1 2022 10 27 + 2 2022 12 24 """ def __init__(