Feature Engineering and Derived Features
The Boston Housing dataset holds 506 data points, each described by 13 features, and supports regression toward median home values in Boston neighborhoods during the 1970s.
Predicting the Count Before Reading It
Before you train any model, you need to know what data you are working with. The Boston Housing dataset is a real-world regression dataset, and it is useful for learning feature engineering because its column count changes when we add new inputs built from old ones. Keep one question in mind as you read: if a dataset starts with 13 features, and we add every product of two features to the list, how many features do we end up with?
What do you think happens?
Starting from 13 original features, and adding every product of two features (including each feature multiplied by itself), what is the total number of features?
Reveal answer
104
The expanded version keeps the 13 originals and adds 91 combinations of two features, and 13 + 91 = 104. The rest of this article shows where the 91 comes from.
What the 506 by 13 Table Holds
The shape of a dataset is reported as a pair of numbers in a fixed order: data points first, then features. For the Boston Housing dataset, the shape is 506 data points and 13 features. Reading this tuple in the correct order is the first check you should make on any dataset, because swapping the two numbers would tell you something false about the table.
Each row of the table is one neighborhood in Boston, and each of the 506 rows is a separate data point. Each column is one feature, meaning a single measured property of that neighborhood. The features include information such as the crime rate, proximity to the Charles River, and highway accessibility. So a single data point is not a house. It is one neighborhood described by 13 numbers, and the whole table is a stack of 506 such descriptions.
The Prediction Task Behind the Data
The measurements are collected for a reason, and that reason is the prediction task attached to the dataset. The goal is to predict the median value of homes in several Boston neighborhoods during the 1970s, using the other measurements as inputs.
In supervised learning, the inputs are the features a model reads, and the target is the value the model learns to predict. For the Boston Housing data, the 13 features are the inputs and the median home value is the target.
Because the target is a continuous quantity, a number that can take many values, rather than a category such as yes or no, the dataset is a regression problem. Its purpose is to estimate a number, not to assign a label.
Building an Interaction Feature
The 13 original measurements are not the only inputs the source uses. Feature engineering creates new inputs from existing ones, and one common way to do this is to form interaction features. An interaction feature is the product of a pair of original features. Instead of using the crime rate and the highway accessibility as two separate inputs, the expanded version also includes a third column, whose value for each neighborhood is the crime rate multiplied by the highway accessibility.
The multiplication happens row by row. Each neighborhood has its own crime rate and its own highway accessibility value, so each neighborhood gets its own product. The new column is built by taking the two input columns, multiplying them position by position, and storing the results in a fresh column.
One neighborhood, one interaction value
Suppose a neighborhood has a crime rate of 0.5 and a highway accessibility value of 4. What is its value for the interaction feature formed from these two columns? (The numbers here are illustrative and are not taken from the dataset.)
- 1Identify the two inputs
The two original features are crime rate and highway accessibility, so the interaction is their product.
- 2Multiply the pair
Compute 0.5 times 4, which equals 2.0.
- 3Store the result
Place 2.0 in the new interaction column in this neighborhood's row. Every other row gets its own product from its own two values.
This neighborhood's interaction feature value is 2.0. The model now sees this product as one more input alongside the original 13 measurements.
Counting the 104 Features
The expanded version of the dataset is loaded with a dedicated function, and its shape shows how many columns the expansion produces. The source states the count directly: the 104 features are the 13 original features together with the 91 possible combinations of two features drawn from those 13. Adding the two parts gives the total.
The rows do not change. The expansion adds columns, so the table is still 506 rows tall, and only the width grows from 13 to 104.
The number 91 is worth checking by hand. Choosing two different features from 13 gives $\binom{13}{2} = 78$ pairs. The remaining 13 combinations must be squares, where a single feature is multiplied by itself. Since $78 + 13 = 91$, the count reconciles with the source. This check is our own arithmetic rather than a detail the source spells out, but it shows exactly which combinations the number includes.
Where Feature Engineering Sits in the Pipeline
Feature engineering is a step between the raw data and the model. The raw features are read from the table first. Derived features, such as interaction products, are then computed from them. The model is trained on the full set of inputs, both original and derived, and it produces the single output the task asks for: an estimate of the median home value.
Mistakes When Reading Shapes and Counting Features
Common mistake Reading the shape tuple in the wrong order
What it looks likeTreating 506 x 13 as 13 data points with 506 features.
Why it failsThe shape attribute reports data points first and features second, so the first number counts rows.
Do this insteadRead the tuple as (data points, features). For this dataset, 506 neighborhoods and 13 features.
Common mistake Thinking an interaction feature is a new row
What it looks likeExpecting the expanded dataset to have more than 506 rows after adding products.
Why it failsInteraction features add columns computed from existing rows. Each neighborhood keeps its single row.
Do this insteadExpect the row count to stay at 506 and the column count to grow to 104.
Common mistake Forgetting the squares when counting pairwise products
What it looks likeCounting only the 78 products of two different features and concluding the total is 91 combinations.
Why it failsThe source's 91 combinations include products where a feature is multiplied by itself. There are 13 of those, in addition to the 78 distinct pairs.
Do this insteadCount 78 distinct pairs plus 13 squares to reach 91, then add the 13 originals to reach 104.
Common mistake Treating the 104-feature count as a count of neighborhoods
Practice Counting Derived Features
Suppose a dataset has 5 original features and you add every product of two features, including squares. Using the same counting method as the Boston Housing example, how many derived features do you add, and what is the total number of features?
Hints
- Count the distinct pairs first using the binomial coefficient for choosing 2 items from 5.
- Add the number of squares, which equals the number of original features.
- The total is the original count plus all the derived combinations.
Key Takeaways
- The Boston Housing dataset has shape 506 by 13: 506 neighborhoods as rows, with 13 measured features as columns.
- The prediction task is regression: the 13 features are inputs, and the median home value in 1970s Boston neighborhoods is the continuous target.
- An interaction feature is the product of two original features, computed row by row and stored as a new column.
- The expanded version has 104 features: the 13 originals plus 91 pairwise combinations, which is 78 distinct pairs plus 13 squares.
- Feature engineering sits between the raw data and the model, creating derived inputs that the model learns from.