Model Evaluation using R^2 Score
Feature engineering selects and transforms raw data into features suitable for modeling, and it can significantly affect model performance.
Why One Number Hides a Pipeline
A regression model learns from the columns you give it, and those columns are not always the raw values that were recorded. Feature engineering is the process of selecting and transforming raw data into features that are suitable for modeling. It can significantly affect how well a model performs. The same underlying dataset can produce a weak model or a useful one, depending on how its information is presented to the learner.
This article follows one practical example built on a citibike dataset. The example takes a time-based index from that dataset, turns part of it into a feature, fits a regressor, and then reports how well the model performs on held-out test data. The goal is to work out what the reported number means and where it came from. The number itself is a single R2 score, and the feature that produces it is one column called X_hour.
Guess the Meaning of 0.60
Before reading the explanation, make a guess. Suppose an evaluation reports a test-set R2 score of 0.60 for a model. Decide, in your own words, what that number is telling you about the target and about the model's predictions. Then compare your guess with the reading below.
What do you think happens?
A test-set R2 score of 0.60 is reported. What does that number tell you?
Reveal answer
The model's inputs explain 60 percent of the variance in the target on held-out data.
The source gives this reading directly: a test-set R2 score of 0.60 means that 60 percent of the variance in the target is explained by the model on held-out data. The score is a proportion, so 0.60 corresponds to roughly 60 percent. It is not a percentage of correct predictions, and it is not an average error.
Following the Raw Index to the Score
The raw data in this example is a time-based index on the citibike dataset. The regressor never receives that index directly in the code shown. Instead, the hour component is extracted and reshaped into X_hour, which is the feature matrix the evaluation uses. Everything the score reflects comes from that single feature.
Read the diagram as a chain of hand-offs. Each arrow is a point where the data can change shape or meaning. The extraction step decides which part of the timestamp becomes the feature, so a different choice at that step produces a different model input. The reshape step does not add information; it only changes how the values are arranged so the rest of the pipeline receives them in the layout it expects. The fit and predict steps then turn that feature matrix into a score.
Turning One Timestamp into a Feature
A single timestamp is a point in time, and a regressor cannot split on it the way it can split on a number. Extracting the hour gives a plain integer for each row, such as 8 for a row recorded at 08:15 or 17 for a row recorded at 17:45. Each timestamp in the index becomes one numeric value in the feature column.
Index([8, 17])pd.to_datetimeturns text timestamps into datetime values that have an hour component- idx.hour extracts the hour of each entry as a plain integer
- The printed type can differ slightly between pandas versions; the values 8 and 17 are the point
This snippet is an illustration of the extraction idea, not the exact code behind the citibike score. The source shows the call and its output but not the full definition of eval_on_features or the code that created the regressor and the target. Treat those pieces as given and focus on the single feature that the first line produces.
What the 0.60 Says About Explained Variance
The source gives a concrete reading of the value in this example. An R2 score of 0.60 indicates that 60 percent of the variance in the target variable is explained by the model. The score is a proportion between 0 and 1 that is commonly reported as a percentage, so $R2 = 0.60$ means that roughly 60 percent of the variability in the target is accounted for by the model's inputs.
The 60 percent figure refers to held-out data. The model is fit on one set of rows and scored on another set that it did not see during fitting. A test-set score therefore reports how much of the target's variation the model accounts for on rows that were not used to learn it. This is why the evaluation is computed on the test set rather than the training set: the test rows show whether the learned pattern carries over to new data.
Changing One Step at a Time
When a score is surprising, trace the pipeline step by step from raw index to evaluation and change one thing at a time. Because the score depends on each stage, a change anywhere upstream can alter the score. The worked example below follows one such change.
Tracing the effect of changing the extraction step
Suppose the extraction step is changed so that it produces a different component of the timestamp, while the rest of the pipeline stays the same. Trace which parts of the result change and how you would attribute any change in the reported score.
- 1Identify the changed step
The extraction step decides which part of the timestamp becomes the feature. Changing it changes the model input, even though the regressor and the evaluation code are untouched.
- 2Follow the downstream columns
The reshape step arranges whatever the extraction produced into the layout the pipeline expects. The feature matrix now holds different values, so every later step receives different inputs.
- 3Refit and rescore
The regressor is fit on the new feature matrix and scored on held-out rows. The test-set R2 can rise or fall because the model now has a different input to explain the target with.
- 4Attribute the change carefully
The score does not say which step caused the change. To attribute it, compare runs that differ only in the feature being tested, holding every other step fixed.
A changed score is evidence that the feature change affected what the model can explain, but only a controlled comparison, where one step changes and the others do not, tells you that the extraction choice is the cause.
Not every step carries the same kind of change. The extraction step can change which information reaches the model. The reshape step does not add information; it changes only the layout. When you trace a result, ask of each step whether it alters what the model can see or only how that information is arranged.
Mistakes Beginners Make with R^2
Common mistake Reading 0.60 as 60 percent of predictions being correct
Why it failsR2 is about explained variance in the target, not a count of correct predictions.
Do this insteadRead 0.60 as the model explaining 60 percent of the variance in the target on held-out data.
Common mistake Scoring the model on the rows it was trained on
Why it failsTraining rows show how well the model fits what it has already seen, not whether the pattern carries over to new data.
Do this insteadReport the score computed on the held-out test set, as in this example.
Common mistake Assuming a changed score points to the feature that changed
Why it failsThe score summarizes the whole pipeline, and several steps may have changed at once.
Do this insteadChange one step at a time and compare runs that differ only in the feature under test.
Common mistake Feeding the raw timestamp directly to the regressor and expecting it to find the hour pattern
Why it failsThe regressor in this example receives the engineered feature X_hour, not the raw index, so the hour information must be extracted first.
Do this insteadExtract the component you want, such as the hour, into a numeric column before fitting.
Common mistake Forgetting that a reshape step can change the layout of values without adding any new information
Practice Tracing a Feature Pipeline
A colleague reports a test-set R2 of 0.60 for a model whose only feature is X_hour, derived from a datetime index. Then they change the extraction step so that it produces a different timestamp component, rerun the same evaluation, and report a lower score. List the steps in the pipeline that the new score depends on, state what the 0.60 and the new score each mean, and explain why the lower score alone does not identify which step was responsible.
Hints
- Start from the raw index and follow each hand-off until the score is produced.
- A proportion of 0.60 means 60 percent of the target variance is explained on held-out rows.
- Ask whether the change touched only the extraction step or also the rows used for fitting and scoring.
Key Takeaways
- A regressor learns from the columns it is given, and feature engineering turns raw data, such as a datetime index, into numeric features like X_hour that the model can use.
- A test-set R2 of 0.60 means that 60 percent of the variance in the target is explained by the model on held-out data.
- Every step from raw index to reported score can change the result, so trace the pipeline one step at a time when a score is surprising.
- The score does not identify which feature caused a change; attribute changes by comparing runs that differ only in the feature being tested.