Introduction to Feature Engineering

Welcome to the very first lesson of the Foundations of Feature Engineering. As you embark on this journey, you will explore the critical role of feature engineering in data analysis and machine learning. Feature engineering is the process of transforming raw data into meaningful features that improve the performance of machine learning models, making it a vital step that can significantly influence the success of your analytical projects. In this lesson, we will also introduce you to essential tools that are commonly used, such as the pandas library in Python, which offers powerful functionalities for data manipulation.

Understanding Datasets and Features

In data analysis, a dataset is a collection of data, often presented in a table format, where:

  • Rows represent individual data points.
  • Columns represent features.

Features are the different aspects, attributes, or properties of the data used to train machine learning models. Understanding which features your data contains and how they relate to the problem you are trying to solve is crucial. This knowledge allows you to make informed decisions about which features to select, manipulate, or create, facilitating a more effective analysis process.

Here's a generic example of a poorly structured dataset:

IDNameAgeCityTemperatureLoan Approved
1John25New York28Yes
2AliceSan Francisco55No
3Mike35New York30Yes

In this dataset, the data suffers from several issues. There are missing values in the "Age" column for Alice, which can affect model accuracy. The "Temperature" feature appears irrelevant to predicting "Loan Approved" and could be removed or transformed. Lastly, the "City" feature, while informative, can have high cardinality, so it may require encoding into a more usable format, or creating region-based categories to reduce its impact on performance. Feature engineering allows us to address these issues, improving data quality and model performance.

The Titanic Dataset

In this course, we will work with the Titanic dataset, a classic real-world dataset that contains information about the passengers aboard the RMS Titanic, which tragically sank during its maiden voyage in 1912. This dataset includes a variety of features such as demographic details, passenger class, fare, and survival status. It provides an excellent opportunity to apply feature engineering techniques, as it encompasses both numerical and categorical data, missing values, and other complexities typical of real-world datasets.

By utilizing the Titanic dataset, you'll gain practical experience in handling data imperfections, transforming variables, creating new features, and preparing data for machine learning models using pandas. This hands-on approach will help you build a strong foundation in feature engineering, essential for effective data analysis and predictive modeling.

Getting Started With Pandas

Let's start by getting familiar with pandas, a popular Python library that makes it easy to work with structured data like tables. If you've ever used spreadsheets, think of pandas as a powerful tool to handle similar data in Python.

If you're working on your own computer, you can install pandas using pip, Python's package installer. Open your terminal or command prompt and run:

pip install pandas

But since we'll be using the CodeSignal IDE for this course, good news—pandas is already installed there! So we can dive right into working with the data without worrying about setup.

Loading the Titanic Dataset with Pandas

Now, let's load the Titanic dataset into our Python environment. We'll use the pd.read_csv() function from pandas to read the data from a CSV file into a DataFrame, which is a special pandas object that looks like a table.

First, we need to import pandas:

import pandas as pd

Then, we can load the dataset:

# Load the Titanic dataset
df = pd.read_csv("titanic.csv")

Now, df is a DataFrame containing our Titanic data.

Exploring the Dataset Shape

We might be curious about how big our dataset is—how many passengers are included, and how many features (columns) we have. We can use the shape attribute to find out:

# Display the shape of the dataset
print("Dataset Shape:", df.shape)

When we run this code, we'll get:

Dataset Shape: (891, 15)

This output tells us that the dataset contains 891 rows and 15 columns. Each row represents a passenger, and each column represents a feature or attribute of the passengers.

Getting Information About the Features

To understand what kind of data we're dealing with, we can use the info() function. It gives us details about each column, such as the data type and how many non-null values there are:

# Display information about the dataset's features
print("\nFeature Information:")
df.info()

When we execute this code, we'll see:

Feature Information:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 15 columns):
 #   Column       Non-Null Count  Dtype  
---  ------       --------------  -----  
 0   survived     891 non-null    int64  
 1   pclass       891 non-null    int64  
 2   sex          891 non-null    object 
 3   age          714 non-null    float64
 4   sibsp        891 non-null    int64  
 5   parch        891 non-null    int64  
 6   fare         891 non-null    float64
 7   embarked     889 non-null    object 
 8   class        891 non-null    object 
 9   who          891 non-null    object 
 10  adult_male   891 non-null    bool   
 11  deck         203 non-null    object 
 12  embark_town  889 non-null    object 
 13  alive        891 non-null    object 
 14  alone        891 non-null    bool   
dtypes: bool(2), float64(2), int64(4), object(7)
memory usage: 92.4+ KB
None

This output provides us with valuable information:

  • Non-Null Count: Shows how many entries have actual data (not missing) in each column. For example, the age column has 714 non-null entries, meaning there are missing age values for some passengers.
  • Dtype: Indicates the data type of each column, such as integers (int64), floating-point numbers (float64), objects (typically strings), and booleans (bool).
  • Total Columns: We can confirm there are 15 columns in total.

Understanding the types of data and where missing values exist is crucial for effective feature engineering.

Looking at the First Few Rows

Let's peek at the first few rows of the dataset to see what the data looks like. We can use the head() function for this:

# Display the first few rows of the dataset
print("\nFirst few rows:")
print(df.head())

When we run this code, we'll see:

First few rows:
   survived  pclass     sex   age  ...  deck  embark_town  alive  alone
0         0       3    male  22.0  ...   NaN  Southampton     no  False
1         1       1  female  38.0  ...     C    Cherbourg    yes  False
2         1       3  female  26.0  ...   NaN  Southampton    yes   True
3         1       1  female  35.0  ...     C  Southampton    yes  False
4         0       3    male  35.0  ...   NaN  Southampton     no   True

[5 rows x 15 columns]

This gives us a snapshot of the dataset:

  • Rows 0-4: The first five passengers.
  • Columns: Features like survived, pclass, sex, age, etc.
  • NaN Values: Indicates missing data. For instance, the deck column has NaN, meaning the deck information is missing for these passengers.

Viewing the data helps us understand its structure and identify any immediate issues like missing values.

Generating Descriptive Statistics

To get a quick summary of the numerical data in our dataset, we can use the describe() function. It calculates statistics like mean, standard deviation, and quartiles:

# Display basic statistics
print("\nBasic Statistics:")
print(df.describe())

Running this code gives us:

Basic Statistics:
         survived      pclass         age       sibsp       parch        fare
count  891.000000  891.000000  714.000000  891.000000  891.000000  891.000000
mean     0.383838    2.308642   29.699118    0.523008    0.381594   32.204208
std      0.486592    0.836071   14.526497    1.102743    0.806057   49.693429
min      0.000000    1.000000    0.420000    0.000000    0.000000    0.000000
25%      0.000000    2.000000   20.125000    0.000000    0.000000    7.910400
50%      0.000000    3.000000   28.000000    0.000000    0.000000   14.454200
75%      1.000000    3.000000   38.000000    1.000000    0.000000   31.000000
max      1.000000    3.000000   80.000000    8.000000    6.000000  512.329200

Here's what these statistics tell us:

  • Count: The number of non-missing values for each column. Notice that age has 714 entries, again highlighting missing values.
  • Mean: The average value. For example, the average fare was about £32.20.
  • Std: Standard deviation, showing how spread out the values are.
  • Min and Max: The range of values. For age, the youngest passenger was 0.42 years old (approximately 5 months), and the oldest was 80 years old.
  • Percentiles (25%, 50%, 75%): These show the distribution of the data. For instance, 50% of passengers were 28 years old or younger.

This statistical overview helps us understand the distributions and can reveal outliers or unusual data points that may affect our analysis.

Review and Next Steps

To sum up, in this introductory lesson, we have laid the groundwork for feature engineering by covering the importance of feature engineering, understanding datasets and their features, and learning to use pandas to explore the Titanic dataset. These foundations are critical for tackling more advanced topics in future lessons. As you move forward, reflect on these techniques and try to apply them to build confidence. Stay curious and experiment as much as possible to maximize your learning experience!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal