Feature-engine#

A user-friendly feature engineering alternative to Scikit-learn#

_images/FeatureEngine.png

Feature-engine rocks!#

Feature-engine is a Python library with multiple transformers to engineer and select features for machine learning models. Feature-engine, like Scikit-learn, uses the methods fit() and transform() to learn parameters from and then transform the data.

Working with dataframes? 👉 Feature-engine is a no-brainer#

Unlike Scikit-learn, Feature-engine is designed to work with dataframes. No column order or name changes. A dataframe comes in, same dataframe comes out, with the transformed variables.

We normally apply different feature engineering processes to different feature subsets. With sklearn, we restrict the feature engineering techniques to a certain group of variables by using an auxiliary class: the ColumnTransformer. This class also results in a change in the name of the variables after the transformation.

Feature-engine, instead, allows you to select the variables you want to transform within each transformer. This way, different engineering procedures can be easily applied to different feature subsets without the need for additional transformers or changes in the feature names.

Sitting at the interface of pandas and scikit-learn#

Pandas is great for data analysis and transformation. We ❤️ it too. But it doesn’t automatically learn and store parameters from the data. And that is key for machine learning. That’s why we created Feature-engine.

Feature-engine wraps pandas functionality in Scikit-learn like transformers, capturing much of the pandas logic needed to learn and store parameters, while making these transformations compatible with Scikit-learn estimators, selectors, cross-validation functions and hyperparameter search methods.

If your work is primarily data analysis and transformation for machine learning, and pandas and scikit-learn are your main tools, then Feature-engine is your friend.

Feature-engine transformers#

Feature-engine includes transformers for:

  • Missing data imputation

  • Encoding of categorical features

  • Discretization

  • Outlier capping or removal

  • Feature transformation

  • Creation of new features

  • Feature selection

  • Datetime features

  • Time series

  • Preprocessing

  • Scaling

Feature-engine transformers are fully compatible with scikit-learn. That means that you can assemble Feature-engine transformers within a Scikit-learn pipeline, or use them in a grid or random search for hyperparameters. Check **Quick Start** for an example.

How did you find us? 👀#

We want to share Feature-engine with more people. It’d help us loads if you tell us how you discovered us.

Then we can know what we are doing right and which channels we should use to share the love.

_images/how-did-you-discover.png

🙏 Please share your story by answering 1 quick question at this link 😃

What is feature engineering?#

Feature engineering is the process of using domain knowledge and statistical tools to create features for machine learning algorithms. The raw data that we normally gather as part of our business activities is rarely fit to train machine learning models. Instead, data scientists spend a large part of their time on data analysis, preprocessing, and feature engineering.

Pandas is a common library for data preprocessing and feature engineering. It supports pretty much every method that is commonly used to transform raw data. However, pandas is not compatible with sklearn out of the box and is also not able to learn and store the feature engineering parameters.

Feature-engine’s transformers wrap pandas functionality around an API that exposes fit and transform methods to learn and store parameters from data and then use these parameters to transform the variables. Like this, Feature-engine makes the awesome functionality available in pandas fully compatible with Scikit-Learn.

What is unique about Feature-engine?#

The following characteristics make Feature-engine unique:

  • Feature-engine contains the most exhaustive collection of feature engineering transformations.

  • Feature-engine can transform a specific group of variables in the dataframe, right out-of-the-box.

  • Feature-engine returns dataframes, hence suitable for data analysis and model deployment.

  • Feature-engine is compatible with the Scikit-learn pipeline, Grid and Random search and cross validation.

  • Feature-engine automatically recognizes numerical, categorical and datetime variables.

  • Feature-engine alerts you if a transformation is not possible, e.g., if applying logarithm to negative variables or divisions by 0.

Installation#

Feature-engine is a Python 3 package and works well with 3.9 or later.

The simplest way to install Feature-engine is from PyPI with pip:

$ pip install feature-engine

Note, you can also install it with a _ as follows:

$ pip install feature_engine

Feature-engine is an active project and routinely publishes new releases. To upgrade Feature-engine to the latest version, use pip like this:

$ pip install -U feature-engine

If you’re using Anaconda, you can install the Anaconda Feature-engine package:

$ conda install -c conda-forge feature_engine

Feature-engine’s Transformers#

Feature-engine hosts the following groups of transformers:

Missing Data Imputation: Imputers#

Missing data imputation consists in replacing missing values in categorical data and numerical variables with estimates of those nan values or arbitrary data points. Feature-engine supports the following missing data imputation methods:

  • MeanMedianImputer: replaces missing data in numerical variables by the mean or median

  • ArbitraryNumberImputer: replaces missing data in numerical variables by an arbitrary number

  • EndTailImputer: replaces missing data in numerical variables by numbers at the distribution tails

  • CategoricalImputer: replaces missing data with an arbitrary string or by the most frequent category

  • RandomSampleImputer: replaces missing data by random sampling observations from the variable

  • AddMissingIndicator: adds a binary missing indicator to flag observations with missing data

  • DropMissingData: removes observations (rows) containing missing values from dataframe

Categorical Encoders: Encoders#

Categorical encoding is the process of replacing categorical values by numerical values. Most machine learning models, and in particular, those supported by scikit-learn, don’t accept strings as inputs. Hence, we need to convert these strings into numbers that can be interpeted by these models.

There are various categorical encoding techniques, including one hot encoding, ordinal encoding and target encoding. Feature-engine supports the following methods:

Variable Discretization: Discretizers#

Discretization, or binning, consists in sorting numerical features into discrete intervals. The most commonly used methods are equal-width and equal-frequency discretization. Feature-engine supports these and more advanced methods, like discretization with decision trees:

Outlier Capping or Removal#

Outliers are values that are very different with respect to the distribution observed by the variable. Some machine-learning models and statistical tests are sensitive to outliers. In some cases, we may want to remove outliers or replace them with permitted values.

Numerical Transformation: Transformers#

We normally use variance stabilizing transformations to make the data meet the assumptions of certain statistical tests, like anova, and machine learning models, like linear regression. Feature-engine supports the following transformations:

Feature Creation:#

Feature-engine allows you to create new features by combining them mathematically or transforming them with mathematical functions:

  • MathFeatures: creates new variables by combining features with mathematical operations

  • RelativeFeatures: combines variables with reference features

  • CyclicalFeatures: creates variables using sine and cosine, suitable for cyclical features

  • DecisionTreeFeatures: creates variables resulting from predictions of decision trees on 1 or more features

Datetime:#

Data scientists rarely use datetime features in their original representation with machine learning models. Instead, we extract many new features from the date and time parts of the datetime variable:

Feature Selection:#

Simpler models are easier to interpret, deploy, and maintain. Feature-engine expands the feature selection functionality existing in other libraries like sklearn and MLXtend, with additional methods:

Forecasting:#

To address forecasting as a regression by using traditional machine learning algorithms, we first need to transform the time series into a table of static fetaures. We can do this through lags and windows combined with aggregations over past data:

Preprocessing:#

When transforming variables and doing data cleaning, we usually change the variables data types (dtype in pandas). These can cause problems further down the pipeline. To tackle this head on, Feature-engine has transformers to ensure the data types and variable names match.

  • MatchCategories: ensures categorical variables are of type ‘category’

  • MatchVariables: ensures that columns in test set match those in train set

Scaling:#

Scaling the data can help to balance the impact of all variables on the model, and can improve its performance.

Scikit-learn Wrapper:#

An alternative to scikit-learn’s ColumnTransformer:

Feature scaling#

Scikit-learn offers a comprehensive array of tools to apply data normalization, standardization, and min-max scaling, among other processes, so we felt that there was no need to bring that functionality to Feature-engine. If you want to apply these procedures to a subset of the variables only, check out the SklearnTransformerWrapper:

Getting Help#

Can’t get something to work? Here are places where you can find help.

  1. The **User Guide** in the docs.

  2. Stack Overflow. If you ask a question, please mention “feature_engine” in it.

  3. If you are enrolled in the Feature Engineering for Machine Learning course , post a question in a relevant section.

  4. If you are enrolled in the Feature Selection for Machine Learning course , post a question in a relevant section.

  5. Join our gitter community. You an ask questions here as well.

  6. Ask a question in the repo by filing an issue (check before if there is already a similar issue created :) ).

Contributing#

Interested in contributing to Feature-engine? That is great news!

Feature-engine is a welcoming and inclusive project and we would be delighted to have you on board. We follow the Python Software Foundation Code of Conduct.

Regardless of your skill level you can help us. We appreciate bug reports, user testing, feature requests, bug fixes, addition of tests, product enhancements, and documentation improvements. We also appreciate blogs about Feature-engine. If you happen to have one, let us know!

For more details on how to contribute check the contributing page. Click on the **Contribute** guide.

Open Source#

Feature-engine’s license is an open source BSD 3-Clause.

Feature-engine is hosted on GitHub. The issues and pull requests are tracked there.