Python Tutorial : Feature engineering and overfitting

DataCamp · Beginner ·🛠️ AI Tools & Apps ·6y ago
Skills: ML Pipelines80%

Key Takeaways

This video tutorial covers feature engineering and overfitting in machine learning using Python, discussing techniques such as label encoding, one hot encoding, and feature selection using the select K best algorithm.

Full Transcript

feature engineering uses domain knowledge and common sense to describe an object with numbers although adding more features can improve performance it can also increase the risk of overfitting in this lesson you will learn more about this interesting trade of sometimes the raw data cannot fit into the form of a table for example consider electrocardiogram or ECG traces for a number of individuals each ECG trace is a time series possibly of variable length that cannot fit in one cell of a table instead in the datasets shown here experts extracted over 250 one-dimensional numerical summaries from each ECG these range from simple summaries like heart rate to very complex properties of the signal with weird names like T wave amp all of which can be useful in detecting a medical condition known as arrhythmia even if the data are tabular some of the columns might be non numeric here is an example from the credit scoring data set the purpose of the loan takes values such as buy a new car education or retraining label encoder will map these values on to a range of numbers but the classifier is then confused it thinks that the categories have a natural ordering for example a decision tree might try to split the range in two if it splits at 4 it is putting loans for business together with loans for a microwave oven a different approach is to use one hot encoding implemented by the get dummies and us method this creates one new dummy variable for each category taking the value one for each example that falls in that category and zero otherwise you can see the first row of the data on the Left printed vertically for readability no artificial ordering is introduced how about capturing semantic similarity notice that similar categories share keywords for example all consumer loans feature the keyword buy you can count common keywords using count vectorizer from the feature extraction module first replace underscores with spaces for easier tokenization then apply the encoder using its fit transform method finally convert the resulting matrix to a data frame naming the columns using the debt feature names method of the count vectorizer object note that as we improve our feature engineering pipeline the dimension of our data frame increases the question arises how many features is too many well with more columns the algorithm has more opportunity to mistake coincidental patterns for real signal we can test this by adding columns to the data containing purely random numbers totally unrelated to the class as we add more columns on the horizontal axis overfitting kicks in accuracy improves in sample but deteriorates out-of-sample a popular solution is to add features freely and then select the best ones using some feature selection technique let's try the trick from the preview slide and augment the credit scoring data set with 100 fake variables then we use the select K best algorithm from the feature selector module to select the twenty highest scoring columns we use the chi-squared scoring method the feature selector has a fit method to fit it to the data and a dead support method that returns the index of the selected columns thankfully only a handful of fake columns remain in the selected features so remember this every decision you make in your pipeline might affect other aspects of it and in particular the risk of overfitting the following exercises confirm this inside

Original Description

Want to learn more? Take the full course at https://learn.datacamp.com/courses/designing-machine-learning-workflows-in-python at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work. --- Feature engineering uses domain knowledge and common sense to describe an object with numbers. Although adding more features can improve performance, it can also increase the risk of overfitting. In this lesson, you will learn more about this interesting trade-off. Sometimes, the raw data can not fit into the form of a table. For example, consider electrocardiogram (or ECG) traces for a number of individuals. Each ECG trace is a time series, possibly of variable length, that cannot fit in one cell of a table. Instead, in the dataset shown here experts extracted over 250 one-dimensional numerical summaries from each ECG. These range from simple summaries like heart-rate to very complex properties of the signal with weird names like T-wave-amp, all of which can be useful in detecting a medical condition known as arrhythmia. Even if the data are tabular, some of the columns might be non-numeric. Here is an example from the credit scoring dataset: the purpose of the loan takes values such as "buy a new car", "education" or "retraining". LabelEncoder will map these values onto a range of numbers. But the classifier is then confused. It thinks that the categories have a natural ordering. For example, a decision tree might try to split the range in two. If it splits at 4, it is putting loans for business together with loans for a microwave oven! A different approach is to use one-hot-encoding, implemented by the .get_dummies() pandas method. This creates one new dummy variable for each category, taking the value 1 for each example that falls in that category and 0 otherwise. You can see the first row of the data on the left, printed vertically for readability. No artificial ordering is introduced. How about capturing semantic sim
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from DataCamp · DataCamp · 0 of 60

← Previous Next →
1 SQL Server Tutorial: Date manipulation
SQL Server Tutorial: Date manipulation
DataCamp
2 R Tutorial: Intermediate Interactive Data Visualization with plotly in R
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
3 R Tutorial: Adding aesthetics to represent a variable
R Tutorial: Adding aesthetics to represent a variable
DataCamp
4 R Tutorial: Moving Beyond Simple Interactivity
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
5 Python Tutorial: Why use ML for marketing? Strategies and use cases
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
6 Python Tutorial: Preparation for modeling
Python Tutorial: Preparation for modeling
DataCamp
7 Python Tutorial: Machine Learning modeling steps
Python Tutorial: Machine Learning modeling steps
DataCamp
8 R Tutorial: The prior model
R Tutorial: The prior model
DataCamp
9 R Tutorial: Data & the likelihood
R Tutorial: Data & the likelihood
DataCamp
10 R Tutorial: The posterior model
R Tutorial: The posterior model
DataCamp
11 R Tutorial: An Introduction to plotly
R Tutorial: An Introduction to plotly
DataCamp
12 R Tutorial: Plotting a single variable
R Tutorial: Plotting a single variable
DataCamp
13 R Tutorial: Bivariate graphics
R Tutorial: Bivariate graphics
DataCamp
14 Python Tutorial: Customer Segmentation in Python
Python Tutorial: Customer Segmentation in Python
DataCamp
15 Python Tutorial: Time cohorts
Python Tutorial: Time cohorts
DataCamp
16 Python Tutorial: Calculate cohort metrics
Python Tutorial: Calculate cohort metrics
DataCamp
17 Python Tutorial: Cohort analysis visualization
Python Tutorial: Cohort analysis visualization
DataCamp
18 R Tutorial: Building Dashboards with flexdashboard
R Tutorial: Building Dashboards with flexdashboard
DataCamp
19 R Tutorial: Anatomy of a flexdashboard
R Tutorial: Anatomy of a flexdashboard
DataCamp
20 R Tutorial: Layout basics
R Tutorial: Layout basics
DataCamp
21 R Tutorial: Advanced layouts
R Tutorial: Advanced layouts
DataCamp
22 Python Tutorial: Time Series Analysis in Python
Python Tutorial: Time Series Analysis in Python
DataCamp
23 Python Tutorial: Correlation of Two Time Series
Python Tutorial: Correlation of Two Time Series
DataCamp
24 Python Tutorial: Simple Linear Regressions
Python Tutorial: Simple Linear Regressions
DataCamp
25 Python Tutorial: Autocorrelation
Python Tutorial: Autocorrelation
DataCamp
26 R Tutorial: The gapminder dataset
R Tutorial: The gapminder dataset
DataCamp
27 R Tutorial: The filter verb
R Tutorial: The filter verb
DataCamp
28 R Tutorial: The arrange verb
R Tutorial: The arrange verb
DataCamp
29 R Tutorial: The mutate verb
R Tutorial: The mutate verb
DataCamp
30 R Tutorial: What is cluster analysis?
R Tutorial: What is cluster analysis?
DataCamp
31 R Tutorial: Distance between two observations
R Tutorial: Distance between two observations
DataCamp
32 R Tutorial: The importance of scale
R Tutorial: The importance of scale
DataCamp
33 R Tutorial: Measuring distance for categorical data
R Tutorial: Measuring distance for categorical data
DataCamp
34 Python Tutorial: Plotting multiple graphs
Python Tutorial: Plotting multiple graphs
DataCamp
35 Python Tutorial: Customizing axes
Python Tutorial: Customizing axes
DataCamp
36 Python Tutorial: Legends, annotations, & styles
Python Tutorial: Legends, annotations, & styles
DataCamp
37 Python Tutorial: Introduction to iterators
Python Tutorial: Introduction to iterators
DataCamp
38 Python Tutorial: Playing with iterators
Python Tutorial: Playing with iterators
DataCamp
39 Python Tutorial: Using iterators to load large files into memory
Python Tutorial: Using iterators to load large files into memory
DataCamp
40 SQL Tutorial: Introduction to Relational Databases in SQL
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
41 SQL Tutorial: Tables: At the core of every database
SQL Tutorial: Tables: At the core of every database
DataCamp
42 SQL Tutorial: Update your database as the structure changes
SQL Tutorial: Update your database as the structure changes
DataCamp
43 Python Tutorial: Classification-Tree Learning
Python Tutorial: Classification-Tree Learning
DataCamp
44 Python Tutorial: Decision-Tree for Classification
Python Tutorial: Decision-Tree for Classification
DataCamp
45 Python Tutorial: Decision-Tree for Regression
Python Tutorial: Decision-Tree for Regression
DataCamp
46 Python Tutorial: Census Subject Tables
Python Tutorial: Census Subject Tables
DataCamp
47 Python Tutorial: Census Geography
Python Tutorial: Census Geography
DataCamp
48 Python Tutorial: Using the Census API
Python Tutorial: Using the Census API
DataCamp
49 R Tutorial: A/B Testing in R
R Tutorial: A/B Testing in R
DataCamp
50 R Tutorial: Baseline Conversion Rates
R Tutorial: Baseline Conversion Rates
DataCamp
51 R Tutorial: Designing an Experiment - Power Analysis
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
52 R Tutorial: Introduction to qualitative data
R Tutorial: Introduction to qualitative data
DataCamp
53 R Tutorial: Understanding your qualitative variables
R Tutorial: Understanding your qualitative variables
DataCamp
54 R Tutorial: Making Better Plots
R Tutorial: Making Better Plots
DataCamp
55 SQL Tutorial: OLTP and OLAP
SQL Tutorial: OLTP and OLAP
DataCamp
56 SQL Tutorial: Storing data
SQL Tutorial: Storing data
DataCamp
57 SQL Tutorial: Database design
SQL Tutorial: Database design
DataCamp
58 Python Tutorial: Introduction to spaCy
Python Tutorial: Introduction to spaCy
DataCamp
59 Python Tutorial: Statistical Models
Python Tutorial: Statistical Models
DataCamp
60 Python Tutorial: Rule-based Matching
Python Tutorial: Rule-based Matching
DataCamp

This video tutorial teaches feature engineering and overfitting in machine learning using Python, covering techniques such as label encoding, one hot encoding, and feature selection. It highlights the importance of careful feature engineering to avoid overfitting and improve model performance.

Key Takeaways
  1. Load and preprocess the data
  2. Apply label encoding or one hot encoding to non-numeric columns
  3. Use count vectorizer to capture semantic similarity
  4. Add fake variables to test overfitting
  5. Use the select K best algorithm to select the best features
  6. Evaluate model performance using chi-squared scoring
💡 Careful feature engineering is crucial to avoid overfitting and improve model performance, and techniques such as label encoding, one hot encoding, and feature selection can be used to achieve this.

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →