Data Transformation for Linear Models
The evaluation line trains a ridge model with training features and targets, then scores it with test features and targets, and the score is printed to three decimal places.
Why a Transformed Feature Needs an Evaluation
After a feature has been transformed, the natural next question is whether the transformation actually helped the model. A transformation can change the numbers a model sees, so a model built on transformed data may behave differently than one built on the raw data. Saying that a model fits better is a claim, and the evaluation is what turns that claim into a number you can inspect.
In the source, the ridge model is built on the new, transformed data and the result is described as a much better fit. This article follows the single evaluation line that produces that number. You will read it, trace the data through it, and learn to ask which inputs produced the score you are looking at.
Reading the Chained Line
The evaluation is written as one chained line. The source excerpt shows the training features as X_train_log and the test features as X_test_log. The target variables are not named in the excerpt, so this article uses y_train and y_test as placeholder names for the training and test targets.
Ridge()creates a model object- .fit(X_train_log,
y_train) trains that model on the training features and targets and returns the fitted model - .score(X_test_log,
y_test) is called on the fitted model and returns one number summarizing its performance on the test data - The result is stored in the variable score
- The format string {:.3f} controls only how the number is displayed
The line is a chain of three operations. Ridge() produces a model object. The fit method uses the training data to adjust that model, and it returns the fitted model so that the next call can be made on it. The score method then takes the test data and returns a single number that summarizes how the fitted model performs on that data.
What do you think happens?
What does .fit(X_train_log, y_train) return, and what does the .score call receive?
Reveal answer
The fitted model itself, which then receives the test data in the .score call
The source states that fit returns the fitted model so the next call can be made on it. The score method is then called on that fitted model with the test features and test targets.
Tracing the Fit and Score Pipeline
When a printed score is not what you expected, the fastest way to find the cause is to trace the data flow through the line. Think of the evaluation as a pipeline with two inputs entering the fit step, the fitted model and the test inputs entering the score step, and then the printed output. Each arrow is a place where a wrong input could silently produce a plausible-looking number.
Read the diagram in order. The two training inputs enter the fit step, which produces the fitted model. The fitted model and the two test inputs enter the score step. The score is then stored and printed. The training inputs only ever shape the model, and the test inputs only ever judge it.
Matching Transformations Between Training and Test
The features in the score step are X_test_log, and the features in the fit step are X_train_log. The names follow the pattern of the same transformation being applied to both sets. The source excerpt does not show how these variables were created, so when you trace a result you should confirm that both were produced by the same transformation step earlier in the notebook.
This matters because the model learns from the training features in whatever form they were given. A score computed on test features in a different form is measuring something else. The number is still produced and printed, but it no longer describes how the fitted model handles inputs of the kind it was trained on.
Following One Score Through the Line
Tracing where the printed score comes from
A colleague shows you a printed score from the evaluation line and asks whether it reflects the transformed features. How do you check?
- 1Find the fit inputs
Identify X_train_log and
y_trainas the inputs to .fit. Ask where X_train_log was created and which transformation produced it. - 2Find the score inputs
Identify X_test_log and
y_testas the inputs to .score. Confirm X_test_log was produced by the same transformation step as X_train_log. - 3Confirm the chain order
Check that .score is called on the object returned by .fit, so the scored model is the one trained on X_train_log.
- 4Check the display
Recognize that {:.3f} only changes how many decimals are shown. The stored value in score is the number the evaluation actually produced.
If both feature sets came from the same transformation and the chain order is intact, the printed score is a fair measure of the fitted model on transformed test data. If either check fails, the printed number is not a reliable answer, even though it looks valid.
Judging Whether a Transformation Helped
The source reports a much better fit after building the ridge model on the transformed data. Reading that claim correctly requires a comparison. A single score on transformed data shows how well the model does; it does not, by itself, show that the transformation caused the improvement. To make that claim, you need a baseline score, computed the same way on the untransformed features, and then a comparison between the two.
The two scores are only comparable when everything except the transformation is held fixed. The same training split, the same test split, the same model call, and the same printed precision should be used for both. If the baseline changes several things at once, the difference cannot be credited to the transformation alone.
Mistakes When Reading the Evaluation Line
Common mistake Treating the printed score as proof that the transformation helped
What it looks likeReporting that the log transform improved the model because one printed score looks good
Why it failsThe source says a meaningful improvement claim requires a comparison against a baseline score. One number on its own has nothing to be compared against.
Do this insteadCompute the same score on the untransformed features and compare the two under identical conditions.
Common mistake Assuming the test features were transformed the same way as the training features
What it looks likeRefitting a transformation on the test data and then passing those features to .score
Why it failsA test transformation fitted separately is a different mapping from the one the model was trained on, so the score no longer describes the fitted model on comparable inputs. The line still runs without error.
Do this insteadTrace X_test_log back to the same transformation step that produced X_train_log, and use that same transformation for the test data.
Common mistake Believing the format string changes the score
What it looks likeThinking that {:.3f} rounds the stored value and that rounding caused a difference in results
Why it failsThe source states that the format string affects only the display, not the stored value. The printed text and the stored score are the same underlying number at different precision.
Do this insteadWhen debugging, inspect the score variable itself and treat the printed digits as a presentation of that value.
Common mistake Reading the chain out of order, so the score is attributed to the wrong fitted model
Trace the Pipeline Yourself
A notebook defines X_train_log and X_test_log, but you cannot see the cell that created them. Using the evaluation line from this article, describe the exact checks you would run before trusting the printed score, and say which step would most likely produce a number with no error message.
Hints
- Start from the fit inputs and work toward the printed output.
- Ask which two variables must come from the same transformation step.
- Consider which step could differ silently while the code still runs.
Key Takeaways
- The evaluation line chains
Ridge(), .fit on the training features and targets, and .score on the test features and targets, storing one number in score. - Each call in the chain receives the object returned by the previous call, so .score is applied to the model that was just fitted.
- A score on transformed test features is only meaningful when those features were produced by the same transformation used for the training features.
- The format string {:.3f} changes only the display of the score, not the stored value.
- Showing that a transformation helped requires a baseline score computed under the same conditions without the transformation.