Interpreting Generated Feature Names
Scaling first puts the original features on comparable footing before they are combined.
Why Column Names Matter
A linear model is only as useful as the table of numbers it receives. Before a model can learn anything from the Boston Housing data, the data has to be loaded, divided into parts, rescaled, and expanded into a richer set of columns. Each of those stages changes the table in a specific way, and a mistake at any one of them can quietly produce a model that looks fine but learns from the wrong numbers.
This article follows that preparation from start to finish. You will trace each stage, predict what it should produce, and then check the result against what the program reports. The central skill is reading the generated feature names. Once you can tell which original feature or pair of features each output column came from, you can check any later result against its input and locate the step where something went wrong.
Tracing the Load and Split
The first stage imports three tools from scikit-learn, loads the dataset, and divides it into a training portion and a test portion. Read the code from top to bottom and notice that every name on the left of an equals sign is a new object created by the step on the right. The split is performed by train_test_split, which receives the input data and the target values together and returns four objects. The random_state argument fixes the random choices so that the same split is produced each time the code runs.
In the loaded dataset, the rows of boston.data are the samples and its columns are the features. boston.target holds the value the model is meant to predict. The split keeps inputs and targets aligned, so every row of X_train still belongs with the matching entry of y_train. The scaling lines come after the split, and that ordering matters, as later stages will show.
What do you think happens?
Before reading the next step, what do you expect the training matrix to look like? How many rows and columns does it start with?
Reveal answer
The training matrix starts with 379 rows and 13 columns, one row per training sample and one column per original feature.
The source states that the training matrix starts with 379 rows and 13 columns, and that degree-2 polynomial features will be generated from those 13 columns.
Scaling the Training and Test Data
The source rescales the data with MinMaxScaler so that its values lie within a common range. Rescaling matters here because the polynomial step multiplies features together. If one feature has values far larger than the others, its products would dominate the new columns. Scaling first puts the original features on comparable footing before they are combined.
The two scaling lines follow a pattern worth memorizing. The scaler is created once. Then fit_transform is called on the training data, which both learns the scaling parameters from that training data and applies them. For the test data, the code calls transform only, which applies the parameters already learned from the training set. The test set is therefore scaled with the same rule as the training set, and the scaler never looks at the test data while it is being fitted.
Counting the Expanded Columns
The next stage asks the transformer to create new columns from the existing ones. The degree argument sets the largest number of original features that may be multiplied together in one new column. With degree=2, each output column is either an original feature, a constant, a feature multiplied by itself (its square), or a product of two different original features. The transformer is fitted on the scaled training data, and that same fitted object is then used to transform both the scaled training data and the scaled test data.
The printed shapes show the change in width. The original training matrix has 13 columns, and the expanded one has 105. The number of rows stays at 379 because each sample still produces exactly one row. Only the description of each sample becomes longer. Each of the 13 original features contributes a square term, and each pair of different features contributes one interaction term.
This expression is a way of checking the count rather than a formula printed in the source. Here n is the number of original features and N is the number of output columns. It counts one constant column, then the n original columns, then the squares and the distinct pairwise products. The term n(n+1)/2 combines the n squares with the n(n-1)/2 distinct pairs, since n + n(n-1)/2 = n(n+1)/2.
Predicting the column count for 13 features
The training matrix has n = 13 original features. How many columns does the degree-2 expansion produce, and how is that total split into groups?
- 1Count the constant column
The expansion always starts with one constant column, which contributes 1.
- 2Count the original columns
The 13 original features are kept, which contributes 13.
- 3Count the squares and products
There are 13 squares, one per feature. The distinct pairs number 13 times 12 divided by 2, which is 78. Together the squares and pairs contribute 13 plus 78, which is 91.
- 4Add the groups
1 plus 13 plus 91 gives 105, which matches the printed shape of the expanded matrix.
The expanded matrix has 105 columns, made up of 1 constant, 13 original features, 13 squares, and 78 pairwise products. The row count stays at 379 for the training set.
Reading the Feature Name Lookup Table
A table of 105 unlabeled numbers is hard to interpret, so the transformer can report which input produced each output. The get_feature_names method returns those labels. The labels are not arbitrary. The first is a constant, the next thirteen are the original features named x0 through x12, and the names after those show the squares and products. In the printed output, a caret indicates a square, and a space indicates a product of two features.
The full sequence follows a regular order. Names beginning with x0 come first, then x1, and so on. For each starting feature, the square comes first and is followed by the products with each later feature. The diagram below reads the list as a lookup table, showing selected positions so you can see the pattern.
Read the diagram as a lookup table. Position 0 is the constant, positions 1 through 13 are the original features, and position 14 begins the squared and product terms built from the first feature. Because the names record the origin of each column, you can check any coefficient or diagnostic later against the input it came from.
Mapping a Column Back to Its Inputs
Tracing column 15 back to its input
The expanded matrix has a column at position 15 named x0 x1. Which original features does it come from, and what does its value depend on?
- 1Read the name
The name has no caret, so it is not a square. The space means it is a product of two different features.
- 2Identify the inputs
The two features are x0 and x1, the first and second original features in the input order.
- 3Locate the value
Each entry in this column is the value of x0 for that row multiplied by the value of x1 for the same row. Both values are the scaled values, because scaling happened before expansion.
- 4Check the position
Position 15 is the first product in the block that starts with x0, which is consistent with the pattern of square first, then products with later features.
Column 15 is the scaled x0 multiplied by the scaled x1, row by row. Its name alone tells you which two inputs it depends on.
What do you think happens?
Which original features does the column at position 27, named x12, depend on?
Reveal answer
It depends only on x1, the second original feature, because the caret means the feature is squared with itself.
Position 27 comes after the 14 constant and original entries plus the 13 entries for x0, so it begins the block for x1. A caret marks a square of a single feature.
Mistakes in the Preparation Order
Preparation errors often do not crash the program. The shapes can look correct even when the values are wrong. A shape check confirms the column count but not the values, so the most useful habit is to ask which stage produced the surprising number and to trace each stage in the order the data passes through it.
Common mistake Fitting the scaler on the test data
What it looks likeCalling fit_transform on the test set instead of transform.
Why it failsThe scaling parameters would be learned from the test data, so the test set would be scaled by a different rule than the training set.
Do this insteadCall fit_transform only on the training data, and call transform on the test data using the fitted scaler.
Common mistake Expanding before scaling
What it looks likeApplying the polynomial expansion to the unscaled training data instead of the scaled data.
Why it failsThe source notes that if one feature has values far larger than the others, its products dominate the new columns. Expanding unscaled data lets large raw values control the product columns.
Do this insteadScale the original features first, then generate the polynomial columns from the scaled values.
Common mistake Trusting the shape alone
What it looks likeSeeing 105 columns and concluding the preparation is correct.
Why it failsThe column count can be right even when the values or their order are wrong.
Do this insteadCheck the feature names for the expected inputs and inspect values at each stage, not only the shape.
Practice Tracing the Pipeline
Suppose a preprocessing run produces an expanded training matrix with 379 rows and 105 columns, but the test matrix has 379 rows and 50 columns. Name the most likely stage where the test data went wrong, and explain how you would confirm it using the feature names and shapes.
Hints
- Compare the training and test matrices after each stage, not only at the end.
- The number of rows should be unchanged by expansion, but the number of columns should match the training expansion for the same fitted transformer.
- Check whether the same fitted transformer was applied to both sets.
Write out the feature names for the first four positions of the expanded matrix, then name the input or input pair for each one.
Hints
- Position 0 is the constant.
- Positions 1 and 2 are the first two original features.
Summary of the Preparation Trace
- 1Loading and splitting produce a 379 by 13 training matrix; the split keeps inputs and targets aligned.
- 2MinMaxScaler is fitted on the training data only, and the same fitted scaler transforms the test data.
- 3Degree-2 expansion of 13 features gives 105 columns: 1 constant, 13 originals, 13 squares, and 78 pairwise products, with the row count unchanged.
- 4Generated names such as 1, x0, x02, and x0 x1 record the input or input pair behind each column, so each column can be traced back to its source.
At a glance
- The workflow loads the data, splits it into training and test sets, scales the training data with MinMaxScaler, and applies the same fitted scaler to the test data.
- Degree-2 polynomial features turn 13 original features into 105 columns while keeping the 379 training rows unchanged.
- Generated names such as 1, x0, x02, and x0 x1 map each output column back to its input feature or feature pair.
- Shape checks confirm the column count but not the values, so trace each stage in the order the data passes through it, and check that scaling happens before expansion.