[New Book] Click to get The Beginner's Guide to Data Science!
Use the offer code 20offearlybird to get 20% off. Hurry, sale ends soon!

How to Use Feature Extraction on Tabular Data for Machine Learning

Machine learning predictive modeling performance is only as good as your data, and your data is only as good as the way you prepare it for modeling.

The most common approach to data preparation is to study a dataset and review the expectations of a machine learning algorithm, then carefully choose the most appropriate data preparation techniques to transform the raw data to best meet the expectations of the algorithm. This is slow, expensive, and requires a vast amount of expertise.

An alternative approach to data preparation is to apply a suite of common and commonly useful data preparation techniques to the raw data in parallel and combine the results of all of the transforms together into a single large dataset from which a model can be fit and evaluated.

This is an alternative philosophy for data preparation that treats data transforms as an approach to extract salient features from raw data to expose the structure of the problem to the learning algorithms. It requires learning algorithms that are scalable of weight input features and using those input features that are most relevant to the target that is being predicted.

This approach requires less expertise, is computationally effective compared to a full grid search of data preparation methods, and can aid in the discovery of unintuitive data preparation solutions that achieve good or best performance for a given predictive modeling problem.

In this tutorial, you will discover how to use feature extraction for data preparation with tabular data.

After completing this tutorial, you will know:

  • Feature extraction provides an alternate approach to data preparation for tabular data, where all data transforms are applied in parallel to raw input data and combined together to create one large dataset.
  • How to use the feature extraction method for data preparation to improve model performance over a baseline for a standard classification dataset.
  • How to add feature selection to the feature extraction modeling pipeline to give a further lift in modeling performance on a standard dataset.

Kick-start your project with my new book Data Preparation for Machine Learning, including step-by-step tutorials and the Python source code files for all examples.

Let’s get started.

How to Use Feature Extraction on Tabular Data for Data Preparation

How to Use Feature Extraction on Tabular Data for Data Preparation
Photo by Nicolas Valdes, some rights reserved.

Tutorial Overview

This tutorial is divided into three parts; they are:

  1. Feature Extraction Technique for Data Preparation
  2. Dataset and Performance Baseline
    1. Wine Classification Dataset
    2. Baseline Model Performance
  3. Feature Extraction Approach to Data Preparation

Feature Extraction Technique for Data Preparation

Data preparation can be challenging.

The approach that is most often prescribed and followed is to analyze the dataset, review the requirements of the algorithms, and transform the raw data to best meet the expectations of the algorithms.

This can be effective, but is also slow and can require deep expertise both with data analysis and machine learning algorithms.

An alternative approach is to treat the preparation of input variables as a hyperparameter of the modeling pipeline and to tune it along with the choice of algorithm and algorithm configuration.

This too can be an effective approach exposing unintuitive solutions and requiring very little expertise, although it can be computationally expensive.

An approach that seeks a middle ground between these two approaches to data preparation is to treat the transformation of input data as a feature engineering or feature extraction procedure. This involves applying a suite of common or commonly useful data preparation techniques to the raw data, then aggregating all features together to create one large dataset, then fit and evaluate a model on this data.

The philosophy of the approach treats each data preparation technique as a transform that extracts salient features from raw data to be presented to the learning algorithm. Ideally, such transforms untangle complex relationships and compound input variables, in turn allowing the use of simpler modeling algorithms, such as linear machine learning techniques.

For lack of a better name, we will refer to this as the “Feature Engineering Method” or the “Feature Extraction Method” for configuring data preparation for a predictive modeling project.

It allows data analysis and algorithm expertise to be used in the selection of data preparation methods and allows unintuitive solutions to be found but at a much lower computational cost.

The exclusion in the number of input features can also be explicitly addressed through the use of feature selection techniques that attempt to rank order the importance or value of the vast number of extracted features and only select a small subset of the most relevant to predicting the target variable.

We can explore this approach to data preparation with a worked example.

Before we dive into a worked example, let’s first select a standard dataset and develop a baseline in performance.

Want to Get Started With Data Preparation?

Take my free 7-day email crash course now (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

Dataset and Performance Baseline

In this section, we will first select a standard machine learning dataset and establish a baseline in performance on this dataset. This will provide the context for exploring the feature extraction method of data preparation in the next section.

Wine Classification Dataset

We will use the wine classification dataset.

This dataset has 13 input variables that describe the chemical composition of samples of wine and requires that the wine be classified as one of three types.

You can learn more about the dataset here:

No need to download the dataset as we will download it automatically as part of our worked examples.

Open the dataset and review the raw data. The first few rows of data are listed below.

We can see that it is a multi-class classification predictive modeling problem with numerical input variables, each of which has different scales.

The example loads the dataset and splits it into the input and output columns, then summarizes the data arrays.

Running the example, we can see that the dataset was loaded correctly and that there are 179 rows of data with 13 input variables and a single target variable.

Next, let’s evaluate a model on this dataset and establish a baseline in performance.

Baseline Model Performance

We can establish a baseline in performance on the wine classification task by evaluating a model on the raw input data.

In this case, we will evaluate a logistic regression model.

First, we can perform minimum data preparation by ensuring the input variables are numeric and that the target variable is label encoded, as expected by the scikit-learn library.

Next, we can define our predictive model.

We will evaluate the model using the gold standard of repeated stratified k-fold cross-validation with 10 folds and three repeats.

Model performance will be evaluated using classification accuracy.

At the end of the run, we will report the mean and standard deviation of the accuracy scores collected across all repeats and evaluation folds.

Tying this together, the complete example of evaluating a logistic regression model on the raw wine classification dataset is listed below.

Running the example evaluates the model performance and reports the mean and standard deviation classification accuracy.

Note: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.

In this case, we can see that the logistic regression model fit on the raw input data achieved the average classification accuracy of about 95.3 percent, providing a baseline in performance.

Next, let’s explore whether we can improve the performance using the feature extraction based approach to data preparation.

Feature Extraction Approach to Data Preparation

In this section, we can explore whether we can improve performance using the feature extraction approach to data preparation.

The first step is to select a suite of common and commonly useful data preparation techniques.

In this case, given that the input variables are numeric, we will use a range of transforms to change the scale of the input variables such as MinMaxScaler, StandardScaler, and RobustScaler, as well as transforms for chaining the distribution of the input variables such as QuantileTransformer and KBinsDiscretizer. Finally, we will also use transforms that remove linear dependencies between the input variables such as PCA and TruncatedSVD.

The FeatureUnion class can be used to define a list of transforms to perform, the results of which will be aggregated together, i.e. unioned. This will create a new dataset that has a vast number of columns.

An estimate of the number of columns would be 13 input variables times five transforms or 65 plus the 14 columns output from the PCA and SVD dimensionality reduction methods, to give a total of about 79 features.

We can then create a modeling Pipeline with the FeatureUnion as the first step and the logistic regression model as the final step.

The pipeline can then be evaluated using repeated stratified k-fold cross-validation as before.

Tying this together, the complete example is listed below.

Running the example evaluates the model performance and reports the mean and standard deviation classification accuracy.

Note: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.

In this case, we can see a lift in performance over the baseline performance, achieving a mean classification accuracy of about 96.8 percent as compared to 95.3 percent in the previous section.

Try adding more data preparation methods to the FeatureUnion to see if you can improve the performance.

Can you get better results?
Let me know what you discover in the comments below.

We can also use feature selection to reduce the approximately 80 extracted features down to a subset of those that are most relevant to the model. In addition to reducing the complexity of the model, it can also result in a lift in performance by removing irrelevant and redundant input features.

In this case, we will use the Recursive Feature Elimination, or RFE, technique for feature selection and configure it to select the 15 most relevant features.

We can then add the RFE feature selection to the modeling pipeline after the FeatureUnion and before the LogisticRegression algorithm.

Tying this together, the complete example of the feature selection data preparation method with feature selection is listed below.

Running the example evaluates the model performance and reports the mean and standard deviation classification accuracy.

Note: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.

Again, we can see a further lift in performance from 96.8 percent with all extracted features to about 98.9 with feature selection used prior to modeling.

Can you achieve better performance with a different feature selection technique or with more or fewer selected features?
Let me know what you discover in the comments below.

Further Reading

This section provides more resources on the topic if you are looking to go deeper.

Related Tutorials

Books

APIs

Summary

In this tutorial, you discovered how to use feature extraction for data preparation with tabular data.

Specifically, you learned:

  • Feature extraction provides an alternate approach to data preparation for tabular data, where all data transforms are applied in parallel to raw input data and combined together to create one large dataset.
  • How to use the feature extraction method for data preparation to improve model performance over a baseline for a standard classification dataset.
  • How to add feature selection to the feature extraction modeling pipeline to give a further lift in modeling performance on a standard dataset.

Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.

Get a Handle on Modern Data Preparation!

Data Preparation for Machine Learning

Prepare Your Machine Learning Data in Minutes

...with just a few lines of python code

Discover how in my new Ebook:
Data Preparation for Machine Learning

It provides self-study tutorials with full working code on:
Feature Selection, RFE, Data Cleaning, Data Transforms, Scaling, Dimensionality Reduction, and much more...

Bring Modern Data Preparation Techniques to
Your Machine Learning Projects


See What's Inside

33 Responses to How to Use Feature Extraction on Tabular Data for Machine Learning

  1. Avatar
    Puneet Rajput July 6, 2020 at 5:55 am #

    Hi Jason! Big fan of yours.

    Thanks for the article. I learnt few new libraries through it. I have a question instead of doing both feature extraction and the selection, is there anyway through which we can understand what to use by looking at the data or some stats at the very beginning ?

    And can we use feature selection separately or do we need to use it with the large dataset created by feature extraction methods ? How to understand that ?

  2. Avatar
    Damon Resnick July 6, 2020 at 8:33 am #

    Nice. Thank you Jason. Is there a function that also selects the interaction of columns as well? Or would you have to create all those new columns first? For instance generate interaction features by multiplying features together to get a new feature. Given how many possible relevent combinations there might be, it be would nice if there was a function that could automate that.

  3. Avatar
    Hatta July 6, 2020 at 9:07 am #

    Hi Jason, good work!

    I am wonder what is your opinion about tsfresh module?

    • Avatar
      Jason Brownlee July 6, 2020 at 2:05 pm #

      I have not used it, sorry.

    • Avatar
      Paul Christian July 14, 2020 at 12:47 am #

      Hatta, Thank you for the heads-up on the tsfresh module. Looks very useful.

  4. Avatar
    Lev July 7, 2020 at 4:43 am #

    Another great post – thank you very much. I would just add that it make sense to check if the selected features indeed makes sense for you. Sometimes it allows you to find a bug in you code and sometimes to learn something new about your data

  5. Avatar
    Diego July 7, 2020 at 5:56 am #

    Great article.

    Let’s say I split data into train/test at the beggining.

    How would you make a prediction on new data ?
    How do you make a model using all those steps and pipeline?

    Thanks a lot!!

    • Avatar
      Jason Brownlee July 7, 2020 at 6:46 am #

      You can fit the pipeline on all data and call pipleine.predict() to make a prediction on new data.

  6. Avatar
    Diego July 8, 2020 at 3:58 am #

    Jason, if you wanted to run more than one model, i.e logistic regression and Random Forest, using the same features and preprocessing steps, how would you do that ?

    Do you have an example?

    Thanks in advance,

  7. Avatar
    Diego July 8, 2020 at 10:29 am #

    Jason, sorry,

    I meant to also try another classifier apart from logistic regression.
    You made a CV of a RL, is it possible to add a few lines more and try some other models to see if they make better predictions ?
    Not stacking them but checking wich one is the best.

    Thanks

    • Avatar
      Jason Brownlee July 8, 2020 at 1:43 pm #

      Yes, you can change the model to anything you like.

  8. Avatar
    Isah July 10, 2020 at 10:07 am #

    Good day, please I’m new to machine learning and I want to learn and have research interest. What are the requirements to start ? Thank you

  9. Avatar
    Basant Agrawal July 12, 2020 at 6:04 pm #

    I wonder why do we need to label encode the target variable which was already numeric (with value 1,2 and 3).

    • Avatar
      Jason Brownlee July 13, 2020 at 5:59 am #

      Good default practice, perhaps. Also, ensure the integer values start at zero in case they don’t.

  10. Avatar
    Paul Christian July 14, 2020 at 6:21 am #

    Jason,

    Great book, “Data Preparation For ML”. I read the entire book in a day – I could not put it down! Question, can you direct me to a simple example that utilizes FeatureUnion, ColumnTransformer, and the TransformedTargetRegressor together? I am assuming that the TransformedTargetRegressor only operates on the target variable(s) and not the features themselves so I would need to do that before the TransformedTargetRegressor? Is there a method to discover the final selected RFE?

    • Avatar
      Jason Brownlee July 17, 2020 at 6:43 am #

      Very impressive!

      You can use RFE in isolation and report the features found by the method. It would be a mess though.

  11. Avatar
    Sow Souleymane July 19, 2020 at 10:24 am #

    Un excellent post – merci beaucoup pour vos articles.

    En fait je voulais savoir quel est l’intérêt d’utiliser deux techniques de normalisations “MinMaxScaler” et un “StandarScaler” sur un même ensemble de données ?

    • Avatar
      Jason Brownlee July 19, 2020 at 1:42 pm #

      Thanks!

      Good question, it is possible that vars with a gaussian variable may better respond to standardization than normalization, but you’re probably right that one scaling method would be sufficient.

  12. Avatar
    Souleymane Sow July 22, 2020 at 10:52 pm #

    Merci de votre réponse.

    Dans ce cas serait-il pas plus rigoureux de faire un test d’hypothèse statistique sur les variables afin de déterminer les variables qui ont une distribution gaussienne et en appliqué une standardisation à ces variables.

  13. Avatar
    Souleymane Sow July 24, 2020 at 8:53 am #

    Merci

    Je suis tout à fait d’accord avec vous , on obtient de meilleurs résultats en enfreignant les règles.

    Ton article sur la “selectively scale” est Top!!! Merci encore Jason.

  14. Avatar
    sukhpal October 30, 2020 at 1:41 am #

    sir is we apply GAN based data augmentation at test time to pre-trained AlexNet

  15. Avatar
    vanitha February 22, 2021 at 9:29 pm #

    which feature selection method is best for analysis weblog data

Leave a Reply