-
Notifications
You must be signed in to change notification settings - Fork 362
fix Sklearnwrapper output for OneHotEncoder and PolynomialFeatures to avoid duplicated features #491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
solegalli
merged 4 commits into
feature-engine:main
from
datacubeR:creators_wrapper_fix
Aug 30, 2022
+186
−17
Merged
fix Sklearnwrapper output for OneHotEncoder and PolynomialFeatures to avoid duplicated features #491
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4221098
Fix repeated features for OneHotEncoder and PolynomialFeatures
datacubeR b8a4386
Fixed Tests for SklearnTransformerWrapper using OneHotEncoder and Pol…
datacubeR c204083
New Tests + Examples for Wrapped OneHotEncoder and Polynomial Features
datacubeR ec12e8c
Adding last Fixes and Docs
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,6 +116,96 @@ to select only a subset of the variables. | |
| X_train_t = selector.transform(X_train.fillna(0)) | ||
| X_test_t = selector.transform(X_test.fillna(0)) | ||
|
|
||
| Even though Feature-engine has its own implementation of OneHotEncoder, you may want | ||
| to use Scikit-Learn's transformer in order to access different options, | ||
| such as drop first Category. | ||
| In the following example, we show you how to apply Scikit-learn's OneHotEncoder to a | ||
| subset of categories using the :class:SklearnTransformerWrapper(). | ||
|
|
||
| .. code:: python | ||
|
|
||
| import pandas as pd | ||
| import numpy as np | ||
| from sklearn.model_selection import train_test_split | ||
| from sklearn.preprocessing import OneHotEncoder | ||
|
|
||
| df = pd.read_csv('https://www.openml.org/data/get_csv/16826755/phpMYEkMl') | ||
| X = df | ||
| y = df.survived | ||
| X_train, X_test, y_train, y_test= train_test_split(X, y, test_size=0.2, random_state=42) | ||
|
|
||
| ohe = SklearnTransformerWrapper(OneHotEncoder(sparse=False, drop='first'), variables = ['pclass','sex']) | ||
|
|
||
| ohe.fit(X_train) | ||
|
|
||
| X_train_transformed = ohe.transform(X_train) | ||
| X_test_transformed = ohe.transform(X_test) | ||
|
|
||
| print(X_train_transformed.head()) | ||
| age fare embarked pclass_2 pclass_3 sex_male | ||
| 772 17 7.8958 S 0.0 1.0 1.0 | ||
| 543 36 10.5 S 1.0 0.0 1.0 | ||
| 289 18 79.65 S 0.0 0.0 0.0 | ||
| 10 47 227.525 C 0.0 0.0 1.0 | ||
| 147 NaN 42.4 S 0.0 0.0 1.0 | ||
|
|
||
| print(X_test_transformed.head()) | ||
| age fare embarked pclass_2 pclass_3 sex_male | ||
| 1148 35 7.125 S 0.0 1.0 1.0 | ||
| 1049 20 15.7417 C 0.0 1.0 1.0 | ||
| 982 NaN 7.8958 S 0.0 1.0 1.0 | ||
| 808 NaN 8.05 S 0.0 1.0 1.0 | ||
| 1195 NaN 7.75 Q 0.0 1.0 1.0 | ||
|
|
||
|
|
||
| Let's say you want to use :class:`SklearnTransformerWrapper()` in a more complex | ||
| context. As you may note there are `?` signs to denote unknown values. Due to the | ||
| complexity of the transformations needed we'll use a Pipeline to impute missing values, | ||
| encode categorical features and create interactions for specific variables using | ||
| Scikit-Learn's PolynomialFeatures. | ||
|
|
||
| .. code:: python | ||
|
|
||
| import pandas as pd | ||
| import numpy as np | ||
| from sklearn.model_selection import train_test_split | ||
| from sklearn.preprocessing import PolynomialFeatures | ||
| from sklearn.pipeline import Pipeline | ||
| from feature_engine.imputation import CategoricalImputer, MeanMedianImputer | ||
| from feature_engine.encoding import OrdinalEncoder | ||
| from feature_engine.wrappers import SklearnTransformerWrapper | ||
|
|
||
| df = pd.read_csv('https://www.openml.org/data/get_csv/16826755/phpMYEkMl') | ||
| X = df[['pclass','sex','age','fare','embarked']].replace('?',np.nan) | ||
| X[['age', 'fare']] = X[['age', 'fare']].astype('float64') | ||
| y = df.survived | ||
|
|
||
| X_train, X_test, y_train, y_test= train_test_split(X, y, test_size=0.2, random_state=42) | ||
| pipeline = Pipeline(steps = [ | ||
| ('ci', CategoricalImputer(imputation_method='frequent')), | ||
| ('mmi', MeanMedianImputer(imputation_method='mean')), | ||
| ('od', OrdinalEncoder(encoding_method='arbitrary')), | ||
| ('pl', SklearnTransformerWrapper(PolynomialFeatures(interaction_only = True, include_bias=False), variables=['pclass','sex'])) | ||
| ]) | ||
| pipeline.fit(X_train) | ||
| X_train_transformed = pipeline.transform(X_train) | ||
| X_test_transformed = pipeline.transform(X_test) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great example, thank you!! Could you add a 5 row display of the resulting dataframe? like in my previous comment. |
||
|
|
||
| print(X_train_transformed.head()) | ||
| age fare embarked pclass sex pclass sex | ||
| 772 17.000000 7.8958 0 3.0 0.0 0.0 | ||
| 543 36.000000 10.5000 0 2.0 0.0 0.0 | ||
| 289 18.000000 79.6500 0 1.0 1.0 1.0 | ||
| 10 47.000000 227.5250 1 1.0 0.0 0.0 | ||
| 147 29.532738 42.4000 0 1.0 0.0 0.0 | ||
|
|
||
| print(X_test_transformed.head()) | ||
| age fare embarked pclass sex pclass sex | ||
| 1148 35.000000 7.1250 0 3.0 0.0 0.0 | ||
| 1049 20.000000 15.7417 1 3.0 0.0 0.0 | ||
| 982 29.532738 7.8958 0 3.0 0.0 0.0 | ||
| 808 29.532738 8.0500 0 3.0 0.0 0.0 | ||
| 1195 29.532738 7.7500 2 3.0 0.0 0.0 | ||
|
|
||
| More details | ||
| ^^^^^^^^^^^^ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to add an image or a printout of the result, for example execute X_train_transform.head() and after that show an image of the final df, or instead print(X_train_transform.head()) and then copy the code output in a code block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure