diff --git a/docs/api_doc/preprocessing/index.rst b/docs/api_doc/preprocessing/index.rst index dc9f54979..cca521d10 100644 --- a/docs/api_doc/preprocessing/index.rst +++ b/docs/api_doc/preprocessing/index.rst @@ -7,7 +7,7 @@ Feature-engine's preprocessing transformers apply general data pre-processing and transformation procedures. .. toctree:: - :maxdepth: 2 + :maxdepth: 1 MatchCategories MatchVariables diff --git a/docs/user_guide/preprocessing/MatchCategories.rst b/docs/user_guide/preprocessing/MatchCategories.rst index 3c810b84f..712a74f2c 100644 --- a/docs/user_guide/preprocessing/MatchCategories.rst +++ b/docs/user_guide/preprocessing/MatchCategories.rst @@ -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) diff --git a/docs/user_guide/wrappers/Wrapper.rst b/docs/user_guide/wrappers/Wrapper.rst index d866e3176..3e1df4bbb 100644 --- a/docs/user_guide/wrappers/Wrapper.rst +++ b/docs/user_guide/wrappers/Wrapper.rst @@ -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 @@ -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) diff --git a/feature_engine/VERSION b/feature_engine/VERSION index bc80560fa..26ca59460 100644 --- a/feature_engine/VERSION +++ b/feature_engine/VERSION @@ -1 +1 @@ -1.5.0 +1.5.1