Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api_doc/preprocessing/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Feature-engine's preprocessing transformers apply general data pre-processing
and transformation procedures.

.. toctree::
:maxdepth: 2
:maxdepth: 1

MatchCategories
MatchVariables
2 changes: 1 addition & 1 deletion docs/user_guide/preprocessing/MatchCategories.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Now, we set up :class:`MatchCategories()` and fit it to the train set.
.. code:: python

# set up the transformer
match_categories = MatchCategories(errors="ignore")
match_categories = MatchCategories(missing_values="ignore")

# learn the mapping of categories to integers in the train set
match_categories.fit(train)
Expand Down
62 changes: 41 additions & 21 deletions docs/user_guide/wrappers/Wrapper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,34 +128,52 @@ subset of categories using the :class:SklearnTransformerWrapper().
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from feature_engine.wrappers import SklearnTransformerWrapper

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'])
# Load dataset
def load_titanic():
data = pd.read_csv('https://www.openml.org/data/get_csv/16826755/phpMYEkMl')
data = data.replace('?', np.nan)
data['cabin'] = data['cabin'].astype(str).str[0]
data['pclass'] = data['pclass'].astype('O')
data['embarked'].fillna('C', inplace=True)
data.drop(["name", "home.dest", "ticket", "boat", "body"], axis=1, inplace=True)
return data

df = load_titanic()

X_train, X_test, y_train, y_test= train_test_split(
df.drop("survived", axis=1),
df["survived"],
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
We can examine the result by executing the following:

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
.. code:: python

print(X_train_transformed.head())

The resulting dataframe is:

.. code:: python

age sibsp parch fare cabin embarked pclass_2 pclass_3 sex_male
772 17 0 0 7.8958 n S 0.0 1.0 1.0
543 36 0 0 10.5 n S 1.0 0.0 1.0
289 18 0 2 79.65 E S 0.0 0.0 0.0
10 47 1 0 227.525 C C 0.0 0.0 1.0
147 NaN 0 0 42.4 n S 0.0 0.0 1.0


Let's say you want to use :class:`SklearnTransformerWrapper()` in a more complex
Expand Down Expand Up @@ -185,7 +203,9 @@ Scikit-Learn's PolynomialFeatures.
('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']))
('pl', SklearnTransformerWrapper(
PolynomialFeatures(interaction_only = True, include_bias=False),
variables=['pclass','sex']))
])
pipeline.fit(X_train)
X_train_transformed = pipeline.transform(X_train)
Expand Down
2 changes: 1 addition & 1 deletion feature_engine/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.5.0
1.5.1