Concepts / Feature Scaling for Linear Models

Feature Scaling for Linear Models

PolynomialFeatures creates new columns by raising existing features to powers, up to the chosen degree.

  • Programming

Why a Straight Line Can Fall Short

A linear model combines its input features with weights and adds them up. When you give it one input column, the model can only express a straight-line relationship between that column and the target. If the real relationship between the input and the target curves, a straight-line combination of the original column may fit poorly no matter how well the weights are chosen.

One way to give a linear model more flexibility is to add new columns that are powers of the existing ones. A column raised to the power 2, or to the power 3, bends in a way the original column cannot. These new columns are called polynomial features. The linear model still adds its weighted inputs together, but now some of those inputs are curved versions of the original value.

This article follows a code-first path. You will run the scikit-learn code that creates polynomial features, predict what the output will look like, and then use the output to explain how the features are built. The goal is to learn to read the result of the transformation and to find the exact place where your expectation and the real output disagree.

Tracing One Column Through the Transformer

The class PolynomialFeatures is imported from scikit-learn's preprocessing module. You create an object that records the settings you want, call fit on your data, and then call transform to produce the new array. The input here is a single column, so X has shape (3, 1): three samples, one feature each.

What do you think happens?

Before running the code, what shape do you expect the transformed array to have when degree is 3 and include_bias is False?

  • (3, 1)

  • (3, 3)

  • (1, 3)

  • (3, 4)

Reveal answer
Answer:

(3, 3)

There are three rows, one per sample. With include_bias turned off there is no constant column, so the columns are the original feature and its powers 2 and 3. Those are three columns in total.

python
Output (expected)
(3, 3)
[[2.    4.    8.   ]
 [3.    9.   27.   ]
 [0.5   0.25  0.125]]
Notes on the code
  • PolynomialFeatures is imported from the preprocessing module
  • degree=3 asks for powers up to 3; include_bias=False removes the constant column
  • fit records the settings and transform builds the new array
  • The printed shape is (rows, columns)

Read the output column by column. The first column still holds the original values 2, 3 and 0.5. The second column holds those values squared, and the third holds them cubed. That matches the pattern the source describes: the first output column is the original feature, and the remaining columns are its higher powers, up to the chosen degree. The source example uses a degree of 10 and turns off the constant column in the same way.

Reading the Shape Before the Values

The shape tells you how many rows and columns the array has before you look at any individual number. The number of rows does not change. The transformer produces one row per sample, so an input of shape $(n, 1)$ becomes an output of shape $(n, k)$. Only the number of columns changes, and $k$ is set by the degree and by whether the constant column is included.

Worked example

Predicting the width of a new transform

You have a single input column with 5 samples. You set degree=4 and include_bias=False. What shape will transform return?

  1. 1
    Rows

    The transformer keeps one row per sample, so there are 5 rows.

  2. 2
    Feature count

    With include_bias=False, the original feature plus its powers 2, 3 and 4 gives 4 columns.

  3. 3
    Shape

    Combine the two: the output shape is (5, 4).

Result

The transformed array has shape (5, 4).

How Degree and include_bias Set the Width

The degree setting is the highest power that gets generated. With include_bias=False, a single input column produces one feature for each power from 1 up to the degree. The source example, with a degree of 10, produces 10 features for a single column, and the transformed array has one row per sample.

The include_bias setting controls the constant column. When it is turned off, as in the earlier example, the output starts at the original feature. When it is turned on, a constant column is added to the output, and it is placed first in the array in the example below. That constant column adds one to the width of the output for the same degree.

python
Output (expected)
(3, 4)
[1. 2. 4. 8.]
Notes on the code
  • The same input X is used, so the row count stays at 3
  • include_bias=True adds one column, giving 4 columns
  • The first value in each row is the constant 1
degree 22 columnsdegree 23 columnsdegree 33 columnsdegree 34 columns
How many columns appear when degree goes from 2 to 3, and how does turning include_bias on or off change that count?

Matching Each Column to Its Power

A shape tells you how many columns exist, but not what they mean. The transformer reports the meaning of each column through its feature-name method, which gives the exponent each feature was raised to. The source calls this method get_feature_names; current scikit-learn releases name the method get_feature_names_out. In both cases the output lists the original feature first and then each of its powers.

Column 0x0 (power 1)Column 1x0^2 (power 2)Column 2x0^3 (power 3)
Which output column holds each power of the input, and where does it sit by position?
python
Output (expected)
['x0', 'x0^2', 'x0^3']
Notes on the code
  • The first name is the original feature
  • Each following name is the same feature with a higher exponent
  • Position 0 in the names matches position 0 in the array

Reading the names gives you a check against your expectation. The first name should always be the original feature, the first output column should match the input, and the remaining names should step through the powers in order. When include_bias=True, the constant column appears with the name 1, which makes it easy to spot in the list.

Large Values From Higher Powers

Once you see the generated columns, you may notice that their magnitudes are very different. The source points out that the second column reaches values above 20,000 in its example, which is orders of magnitude larger than most of the others. This follows from raising values to powers. When a value is larger than one in absolute value, each higher power multiplies it further. Values between negative one and one shrink toward zero as the power rises.

Mistakes When Reading Polynomial Output

  • Common mistake

    Expecting the number of rows to change

    What it looks like

    Transforming 3 samples and expecting 9 rows because there are 3 powers

    Why it fails

    The transformer produces one row per sample. Only the columns grow with the degree.

    Do this instead

    Keep the row count equal to the number of samples and compute only the column count from the settings.

  • Common mistake

    Forgetting that include_bias adds a column

    What it looks like

    Predicting 3 columns for degree=3 when include_bias=True

    Why it fails

    The constant column is an extra column, so the width goes up by one.

    Do this instead

    Add one column for the constant when include_bias is on, then count the powers.

  • Common mistake

    Assuming the first column is always the constant

    What it looks like

    Reading column 0 as all ones when include_bias=False

    Why it fails

    With the constant turned off, the first output column is the original feature.

    Do this instead

    Check the feature names to confirm which column holds which feature.

  • Common mistake

    Treating the large values in the higher-power columns as a bug in the transformer

Finding Where the Output Diverges

When a polynomial result looks wrong, check three things in order: the shape, the first column against the original input, and the feature names. The first place where the observed output disagrees with your expectation tells you where the problem is. If any of these checks fail, the problem is in the setup, not in the model.

Shape matchesexpectation?rows and columnsFirst column equalsinput?original featureFeature names stepthrough powers?x0, x0^2 , x0^3 Setup checks passlook elsewhereProblem is in setupcheck degree andinclude_biasCheck input totransformdata passed inCheck names andsettingsexponentsnoyesnoyesnoyes
In what order should I check shape, the first column, and the feature names to find where the output stops matching my expectation?

Practice Tracing a Transform

EASY

Without running code, predict the shape and the first column for a transform with degree=2 and include_bias=True applied to an input of shape (4, 1) with values 1, 2, 3 and 4. Then write down the feature names you expect, in order. Run the code and check your three predictions: shape, first column, and names.

Hints
  • Rows stay at 4 because there is one row per sample.
  • Columns are the constant, the original feature, and the square.
  • With include_bias=True the first column should be all ones and the names should start with 1.

Key Takeaways

  • PolynomialFeatures creates new columns by raising an existing feature to powers, up to the chosen degree.
  • The number of rows stays equal to the number of samples; the number of columns depends on the degree and on whether include_bias is on.
  • The first output column is the original feature when include_bias is off, and the feature names list each column's exponent so you can match columns to their meaning.
  • Large spreads in magnitude across polynomial columns are expected, and when output looks wrong, check shape, then the first column, then the feature names.

Related concepts

More concepts starting with F