Concepts / Interactions Between Features

Interactions Between Features

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

  • Programming

Why a Straight Line Sometimes Fits Poorly

A linear model combines its input features with weights and adds them up. When the input has one column, that means the model draws a straight-line relationship between the input and the target. If the real relationship between input and 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. The new columns are called polynomial features. The model still adds weighted columns together, but now some of those columns are curved functions of the original input. This article uses a code-first path: you run the transformation, predict what the output should look like, and then use the output to explain how each column was built. The goal is to learn to read the result and to find the exact place where your expectation and the real output disagree.

What PolynomialFeatures Adds to a Column

Definition

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

Suppose the input has a single column called x. The transformer produces a new array in which the first column still holds the original values, and the remaining columns hold the same values raised to higher powers, up to the chosen degree. So a degree of 3 gives you x, then x squared, then x cubed, written as $x^{1}$, $x^{2}$, and $x^{3}$. The first output column is always the original feature, and everything after it is a higher power of that feature.

The transformer is a tool that expands a single column into a set of columns. Each new column is a fixed, predictable function of the original column, so you can work out in advance what the output must contain.

Setting Up the Transformer

The class 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 degree setting controls the highest power. The include_bias setting controls whether a constant column of ones is also added at the front. The source example uses a degree of 10 and turns the constant column off.

python
Output (expected)
[[ 2.  4.  8.]
 [ 3.  9. 27.]]
Notes on the code
  • The input X has two rows and one column, so it holds two samples with one feature each.
  • degree=3 asks for powers up to three.
  • include_bias=False turns off the constant column, so the first output column is the original value.
  • The output has one row per sample and one column per power.

Predicting the Shape Before Running

A shape tells you how many rows and columns the array has. For a single input column, the number of rows equals the number of samples, and the number of columns depends on the degree and on include_bias. With include_bias turned off, a single input column produces one column per power, so a degree of 10 produces 10 features, and the transformed array keeps one row per sample. Turning include_bias on adds one more leading constant column. The table below shows how the column count changes as you vary the two settings.

degreeinclude_bias=False columnsinclude_bias=True columns
112
223
334
101011

Output columns for a single input column, with one row per sample in every case

What do you think happens?

You transform a one-column array with degree=3 and include_bias=False. How many columns does the output have?

  • 2

  • 3

  • 4

Reveal answer
Answer:

3

With include_bias=False the constant column is removed, leaving one column for each power: x, x squared, and x cubed.

Reading the Generated Columns

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 that each feature was raised to. In the source this method is called get_feature_names. Current scikit-learn releases expose the same idea as get_feature_names_out. Its output lists the original feature first, then each power of it, in the same order as the columns of the array.

x0original value, power 1x0^2 power 2x0^3 power 3
Which column in the output array corresponds to which original feature and power, and how do the feature names line up with those columns by position?

Read the names by position. Column 0 is named x0 and holds the original values, so its name tells you it is power 1. Column 1 is named x02 and holds the squared values. Column 2 is named x03 and holds the cubed values. The names step through the powers in order, and that ordering is the check you will use later.

Tracing the Transform on One Value

Worked example

Following one sample through degree 3

A one-column input holds the value 3, and the transformer is set to degree=3 with include_bias=False. What row should appear in the output, and what should each column be named?

  1. 1
    Start with the original value

    The input row is [3]. The first output column always equals the original feature, so column 0 starts at 3.

  2. 2
    Raise to the next power

    Column 1 holds the square of the value, which is $3^{2} = 9$.

  3. 3
    Raise to the third power

    Column 2 holds the cube of the value, which is $3^{3} = 27$.

  4. 4
    Check the names

    The names in order are x0, x02, and x03, which match the powers 1, 2, and 3 in column order.

Result

The output row is [3, 9, 27], with feature names x0, x02, and x03.

The same pattern explains why the columns can differ so much in size. Once you see the generated columns, their magnitudes may be very different. The source points out that the second column reaches values above 20,000, which is far larger than most of the others. This follows directly from raising values to powers. A value larger than one in absolute value grows with each higher power, while a value between negative one and one shrinks toward zero. For example, an input of 300 gives $300^{2} = 90000$ in the second column, which is far larger than the original 300.

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 fails, the fault is in the setup of the transformer, not in the model that comes afterward.

Check shaperows and columnsCheck first columnequals original inputCheck feature namesx0, then powers in orderSetup problemfix degree or include_biasOutput as expectedall checks passmatchesdiffersmatchesdiffersmatchesdiffers
In what order should you check shape, the first column, and the feature names to find where the output first diverges from what you expected?

Mistakes When Reading Polynomial Output

  • Common mistake

    Expecting a constant column without setting include_bias

    What it looks like

    Using PolynomialFeatures(degree=3) and assuming the output has four columns with a leading column of ones.

    Why it fails

    The constant column is controlled by include_bias, so the shape depends on that setting as well as on degree.

    Do this instead

    Decide include_bias on purpose and predict the column count from it before you run the transform.

  • Common mistake

    Assuming the first column is a power other than the original feature

    What it looks like

    Reading column 0 as x squared because it looks like a curve.

    Why it fails

    The first output column corresponds exactly to the original feature. The remaining columns are its higher powers.

    Do this instead

    Compare column 0 against the original input first. If they match, the first column is correct.

  • Common mistake

    Matching columns to features by guesswork rather than by position

    What it looks like

    Looking at the values and deciding which column is the cube without reading the names.

    Why it fails

    The feature names line up with the columns by position, so the name list is the reliable guide to meaning.

    Do this instead

    Read the feature names in order and match each one to the column at the same index.

  • Common mistake

    Treating large column values as a bug

    What it looks like

    Stopping a model run because one column holds values in the tens of thousands.

    Why it fails

    Raising values to powers produces large spreads in scale. This is an expected property of the representation.

    Do this instead

    Check the shape, first column, and names. If they are correct, handle the scale when preparing features for the model.

Practice Checking a Transformed Array

MEDIUM

A colleague sets PolynomialFeatures(degree=4, include_bias=True) on a one-column input with 5 samples and says the output will have 4 columns. Predict the shape of the output, then check the first column and the feature names in that order. Where does the colleague's expectation first disagree with what the transformer returns?

Hints
  • Start from the degree and count the powers, then add one for the constant column because include_bias is on.
  • The number of rows should equal the number of samples, which is 5.
  • The first column should match the original input, and the names should step through the powers in order.

Key Takeaways

  • PolynomialFeatures adds new columns by raising an existing feature to powers, up to the chosen degree, so a linear model can represent a curved relationship.
  • With include_bias=False, a single input column produces one output column per power, and include_bias=True adds one leading constant column.
  • The first output column is the original feature, and the feature names list each column's source feature and exponent in the same order as the columns.
  • Higher powers can produce very different magnitudes, which is expected and should be considered when preparing features.
  • When output looks wrong, check shape, then the first column, then the feature names, and the first disagreement shows where the setup went wrong.

Related concepts

More concepts starting with I