Concepts / Polynomial Features in Model Evaluation

Polynomial Features in Model Evaluation

The best parameters reported by a grid search are the winning combination of settings, here polynomial degree 2 and ridge alpha 10.

  • Programming

Starting From a Finished Search

This article begins at the point where a grid search has already finished running. The search compared several combinations of settings for a model that includes polynomial features and a ridge regression step, and it recorded which combination performed best under cross-validation. Your job is not to run the search again. Your job is to read the results carefully, check them against what you expect, and locate the exact line of output that answers each question.

What do you think happens?

Before reading any output, what do you expect the best combination of settings to be? Is it reasonable to assume that a higher polynomial degree always gives the best score?

Reveal answer
Answer:

No. A higher polynomial degree is not automatically better. The source reports that degree three performs much worse than degree one or two, so the winning combination is not the highest degree.

Polynomial features with a larger degree give the model more flexibility, which makes it tempting to assume a higher degree must score better. The search results are the evidence that settles the question, and they show that flexibility alone does not guarantee a good score.

Reading the Winning Settings

The search stored its findings on an object named grid. Two attributes of that object do most of the work in this section. The first is grid.best_params_, which holds the winning combination of settings. The second is grid.score(X_test, y_test), which measures how well the selected model does on test data. These two answer different questions. The first tells you which settings won the search. The second tells you how the winning model performs on data it did not see during the search. Keeping them apart is the first habit to build.

The best parameters are reported as a dictionary with two keys: polynomialfeatures__degree and ridge__alpha. The first key sets the degree of the polynomial features, and the search chose the value 2. The second key sets the regularization strength for the ridge step, and the search chose 10. Each key names a component of the model and then the setting being tuned. This naming scheme is why the search can tune settings that belong to different parts of the same model at once.

python
Notes on the code
  • The first line reports the winning combination of settings from the search.
  • The second line reports the test-set score, shown with two digits after the decimal point.
  • The code assumes that grid, X_test, and y_test already exist from earlier steps; only the two print statements are shown in the source.

Why the Test Score Stands Apart

The test-set score measures the chosen model on held-out data and is separate from the cross-validation results that selected the settings. The cross-validation results are the evidence used to pick degree 2 and alpha 10. The test score is a separate measurement taken after that choice has been made, on data that played no part in choosing the settings. This is why the two lines of output are not the same kind of number, even though both describe model quality.

Candidate settingsdegree and alpha pairsCross-validationresultsused to rank combinationsbest_params_degree 2, alpha 10grid.score(X_test,y_test)held-out test dataReported test scoretwo decimal placesevaluatedbest combination selectedwinning model scoredprinted
At which step is the test set touched, and what does each stage pass to the next?

Read the diagram from left to right. The test data only enters at the scoring step. Everything to the left of that step belongs to choosing settings. If you were to score the model on the same data that guided the choice, the number would look more favorable than it should, because the settings were tuned toward that data. Reporting the test score separately keeps the final measurement honest.

What Polynomial Degree Adds

Polynomial features take an input and add new columns built from powers of that input. As the degree increases, the model receives more columns and can bend its fitted curve in more ways. The source's setting of polynomialfeatures__degree controls how many powers are created. Degree 2 keeps the original feature and adds its square, which is one reason the search can compare simple and flexible versions of the same input.

Worked example

Expanding one feature at degree 2

Suppose a single input feature has the value 3. Show the columns a degree 2 polynomial expansion would produce for that feature.

  1. 1
    Start with the original feature

    The original column is $x$, and here $x = 3$, so the first column holds 3.

  2. 2
    Add the square

    Degree 2 adds the term $x^{2}$, and $3^{2} = 9$, so the second column holds 9.

  3. 3
    Read the expanded row

    The single feature has become two columns, 3 and 9, which the ridge step can weight separately.

Result

One input value of 3 becomes two columns, $x = 3$ and $x^{2} = 9$. At degree 3, a third column $x^{3} = 27$ would be added, so each higher degree adds more columns and more flexibility.

Ridge alpha works against this flexibility. The source sets ridge__alpha to 10 in the winning combination. Alpha controls the regularization strength of the ridge step. Stronger regularization discourages the model from relying heavily on any single large coefficient, which limits how wildly the fitted curve can bend. A high degree paired with a weak penalty can produce a curve that follows noise, and a high degree paired with a stronger penalty is held in check. The search tests these pairs together, which is why degree and alpha are read as a combination rather than as two separate choices.

Comparing Degrees on the Search Results

The source states that degree three performs much worse than degree one or two. The winning degree in this search is 2. Degree 1 and degree 2 are the two strong candidates, and degree 3 is the clear loser. Notice what this comparison shows. More flexibility was available at degree 3, yet it produced worse evaluation results. Flexibility that the data cannot support does not improve the score.

Degree 1strong candidateDegree 2selected winnerDegree 3much worse than 1 or 2
How do the three polynomial degrees rank on the search results, and where does the higher degree fall short?

Two claims are safe to make from the source. Degree 2 is the winner, and degree 3 is much worse than degrees 1 and 2. The source does not give a full table of scores for every degree, so do not invent exact numbers for the degrees it does not report. When you read your own search output, the same reasoning applies: find the best row, then check how far the other rows fall below it before concluding that a setting matters.

Tracing a Mismatch to Its Source

Sometimes an output disagrees with what you expected. The source's advice is to identify which stage produced that line before changing the code. Each stage in the path hands one result to the next, so a wrong-looking number can be traced backward until your expectation and the output first diverge.

Expectation writtendownSearch finishedgrid holds the findingsbest_params_ linewinning settingsTest score lineheld-out measurementMatches expectation?Find first divergentstagefix there, not everywherecompare againstreadsthen readscheckno
Which stage produces the discrepancy, and what does each step pass to the next?
MEDIUM

A colleague says the search must be wrong because the test-set score is lower than the cross-validation results. Using the path in the diagram, decide which stage to inspect first and explain why the two numbers are not expected to be identical.

Hints
  • Ask which stage uses the test data for the first time.
  • Remember that the cross-validation results are the evidence used to pick degree 2 and alpha 10.
  • A lower test score is not automatically a bug; the test score is a separate measurement.

Mistakes When Reading Search Output

  • Common mistake

    Assuming the highest degree must have the best score

    What it looks like

    Reading the degrees and concluding that degree 3 should win because it has the most flexibility.

    Why it fails

    The source reports that degree three performs much worse than degree one or two.

    Do this instead

    Compare the evaluation results for each degree and accept the combination the search actually selected.

  • Common mistake

    Treating the test score as one more cross-validation result

    Why it fails

    The test score is a separate measurement on held-out data, while the cross-validation results are what selected the settings.

    Do this instead

    Keep the search results and the test-set score in separate categories when you report or compare them.

  • Common mistake

    Reading best_params_ as a full model you can inspect

    Why it fails

    The search reports the winning combination of settings, not a line-by-line model.

    Do this instead

    Use the winning settings as a starting point, then compare them against the other combinations the search evaluated.

  • Common mistake

    Changing the code before identifying which output line disagrees with expectation

Key Takeaways

  • grid.best_params_ reports the winning combination of settings; in this search that is polynomialfeatures__degree 2 and ridge__alpha 10.
  • grid.score(X_test, y_test) is a separate measurement on held-out test data, not the same kind of number as the cross-validation results that selected the settings.
  • A higher polynomial degree adds flexibility but is not automatically better; the source reports that degree 3 performs much worse than degrees 1 and 2.
  • When output disagrees with expectation, identify which stage produced that line, then trace backward to the first stage where the expectation and output diverge before changing the code.

Related concepts

More concepts starting with P