Skip to content

Polynomial Feaatures + SklearnWrapper weird behavior #489

Description

@datacubeR

Describe the bug
A clear and concise description of what the bug is.

When using PolynomialFeaturs + SklearnWrappers the base features are duplicated, when trying to dedup using DropDuplicateFeatures the values are repeated again!!

Using the simple Titanic Dataset you can run something like this:

df = pd.read_csv('https://www.openml.org/data/get_csv/16826755/phpMYEkMl')
X = df[['pclass','sex','age','fare','embarked']]
y = df.survived

X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)
pipe = Pipeline(steps = [
    ('ci', CategoricalImputer(imputation_method='frequent')),
    ('mmi', MeanMedianImputer(imputation_method='mean')),
    ('od', OrdinalEncoder(encoding_method='arbitrary')),
    ('pl', SklearnTransformerWrapper(PolynomialFeatures(degree = 2, interaction_only = True, include_bias=False), variables=['pclass','sex'])),
    #('drop', DropDuplicateFeatures()),
    #('sc', SklearnTransformerWrapper(StandardScaler(), variables=['Age','Fare'])),
    #('lr', LogisticRegression(random_state=42))

])
pipe.fit_transform(X_train)

This returns the first issue:

image

I'm getting pclass and sex duplicated, I'm expecting to get back only interactions. This is expected from Sklearn Docs but why would I want duplicated features?

Looking into Feature Engine Docs I found DropDuplicateFeatures(), but if applying into the Pipeline I get this:

df = pd.read_csv('https://www.openml.org/data/get_csv/16826755/phpMYEkMl')
X = df[['pclass','sex','age','fare','embarked']]
y = df.survived

X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)
pipe = Pipeline(steps = [
    ('ci', CategoricalImputer(imputation_method='frequent')),
    ('mmi', MeanMedianImputer(imputation_method='mean')),
    ('od', OrdinalEncoder(encoding_method='arbitrary')),
    ('pl', SklearnTransformerWrapper(PolynomialFeatures(degree = 2, interaction_only = True, include_bias=False), variables=['pclass','sex'])),
    ('drop', DropDuplicateFeatures()),
    #('sc', SklearnTransformerWrapper(StandardScaler(), variables=['Age','Fare'])),
    #('lr', LogisticRegression(random_state=42))

])
pipe.fit_transform(X_train)

image

getting tons of repeated features, which is totally unexpected.

Expected behavior
Not getting repeated/duplicated features.

Screenshots
Shown above.

Desktop (please complete the following information):

  • Ubuntu 20.04
  • Feature Engine 1.4.0

Thanks in Advance,

Alfonso

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working as expectedgood first issueGood for newcomersurgenturgent attention needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions