Preparing Data for Machine Learning Models

Introduction & Overview

Welcome back! In the previous lesson, you explored the California housing dataset and learned how to inspect, summarize, and visualize your data. Your exploration revealed several important insights: extreme outliers in multiple features (like AveOccup with a maximum of 1,243 people per household, AveRooms with 141.91 rooms, and AveBedrms with 34.07 bedrooms), artificial capping in the target variable, and strong correlations between certain features. These findings directly inform the data preprocessing steps you need to take.

In this lesson, you will learn how to transform your raw data into a form that is suitable for modeling. This process is called data preprocessing, and it is one of the most important stages in any machine learning workflow. We will focus on four key tasks: creating meaningful new features from existing data, splitting your data into training and testing sets for fair model evaluation, systematically handling outliers across all features while avoiding data leakage, and saving your processed datasets for future use. Each step builds directly on the insights from your exploratory data analysis.

Feature Engineering: Creating Meaningful Derived Features

Feature engineering is the process of creating new input features from your existing data. This can help your model capture important patterns that might not be obvious from the original features alone. From our correlation analysis in the previous lesson, we saw that AveRooms and AveBedrms were highly correlated (0.85), and both relate to household space. We can create a more meaningful feature by combining AveRooms with AveOccup to understand space per person—a potentially important factor in determining house values.

Python
# Create a new feature by dividing average rooms by average occupants
# This tells us how many rooms are available per person in a household
df['RoomsPerHousehold'] = df['AveRooms'] / df['AveOccup']

# Display the first few rows to verify the new feature
print(df[['AveRooms', 'AveOccup', 'RoomsPerHousehold']].head())

The output shows our new feature calculation:

   AveRooms  AveOccup  RoomsPerHousehold
0  6.984127  2.555556           2.732919
1  6.238137  2.109842           2.956685
2  8.288136  2.802260           2.957661
3  5.817352  2.547945           2.283154
4  6.281853  2.181467           2.879646

Notice that the RoomsPerHousehold values (around 2-3 rooms per person) are realistic and interpretable

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