Regression Modeling
A regression model learns from the columns you give it, and those columns are not always the raw values that were recorded.
A Score Before the Explanation
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. This matters because feature engineering 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. Before reading the explanation, run the example and look at the reported score. The goal is to work out what the number is telling you and where it came from.
What do you think happens?
The regressor is trained on the full raw timestamp index from the citibike data. Which part of that index reaches the model in this example?
Reveal answer
Only the hour component, turned into a feature called X_hour
The regressor never receives the raw index directly. The hour component is extracted and reshaped into X_hour, which is the only feature the evaluation uses.
Whatever you guessed, the reported value is the one to interpret. The rest of this article explains what the R2 score measures, so that a value such as 0.60 can be read correctly rather than memorized.
Tracing the Raw Index
The raw data in this example is a time-based index on the citibike dataset. A datetime index holds a full timestamp for each row, but a regressor cannot use a timestamp as a single number in a meaningful way. In the example, the hour component of the index is extracted and reshaped into X_hour. That reshaped result is the feature matrix the evaluation uses. The regressor never receives the index itself.
Read the diagram as a chain of decisions. The extraction step decides which part of the timestamp becomes the feature, so the choice made there determines what information is even available to the model. 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.
Index([8, 17], dtype='int32')pd.to_datetimeturns text timestamps into a datetime index- index.hour pulls out only the hour component of each timestamp
- The minutes are discarded by this extraction, which is the kind of choice the feature engineering step makes
Why the Feature Set Sets the Limit
The score in this example reflects only the single feature X_hour that the first line produces. Everything the model can explain has to come through that one column. If a pattern in the target depends on information that the feature does not carry, then no regressor fitted on that feature can capture it, no matter how the fitting is done.
This is why the same dataset can yield a weak or a useful model. Imagine the raw index handed to the model in a form it cannot read well, versus the same information turned into a feature that expresses the part of time that matters. The data is identical in both cases. Only the representation differs, and the test-set score is where that difference shows up.
Reading the 0.60 Score
R2 on held-out test data is the proportion of variance in the target that the model explains. In this example an R2 score of 0.60 means that 60 percent of the variance in the target variable is explained by the model on the held-out data.
The score is a proportion between 0 and 1, and it 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 remaining part of the variance is left unexplained by this model.
The 40 percent in the unexplained column is derived directly from the 60 percent figure, since the two shares make up the whole. The score does not say why the remaining variance is unexplained. It could reflect missing information in the feature, or other factors the model does not have access to. The score only reports the share.
Following the Steps Through the Pipeline
When a score is surprising, the source recommends tracing the pipeline step by step from raw index to evaluation and changing one thing at a time. The pipeline in this example has four stages. Each arrow is a point where the data can change shape or meaning, so each arrow is a place where the final score can depend on what happened.
Notice where the result depends on each step. The extraction step sets the information content of the feature. The reshape step leaves that content unchanged but determines the layout the fitting step expects. The fitting step learns a relationship from X_hour, and the evaluation step measures how much of the target that relationship accounts for on held-out data. A problem at any upstream stage shows up later as a different score, but the score alone does not tell you which stage caused it.
Mistakes Beginners Make With Feature Engineering
Common mistake Assuming the model sees the raw timestamp
What it looks likeReading the course example and believing the regressor is fitted on the full datetime index.
Why it failsThe source states the regressor never receives the index directly. Only the extracted hour, reshaped into X_hour, is the feature the evaluation uses.
Do this insteadTrace the path from the raw index through the extraction and reshape steps before deciding what the model can use.
Common mistake Treating the R2 score as proof of which feature caused a change
What it looks likeChanging the feature set, seeing the test-set score move, and concluding the new feature is the reason.
Why it failsThe score shows that the change affected what the model can explain, but it does not say which feature caused the change.
Do this insteadCompare runs that differ only in the feature being tested, changing one thing at a time.
Common mistake Reading 0.60 as a measure of accuracy on the training data
What it looks likeTaking the 0.60 score as a sign of how well the model fits the rows it was trained on.
Why it failsThe source reads the score as explained variance on held-out test data.
Do this insteadInterpret 0.60 as: the model accounts for 60 percent of the target variance on data it did not train on.
Common mistake Memorizing the number 0.60 without knowing what it measures, which makes it impossible to judge whether a different score is a real improvement
Practice Tracing a Feature
A colleague says the model's score of 0.60 means the regressor was trained on the full timestamp and got 60 percent of the rows right. Using the pipeline from this article, explain what is wrong with this reading. Then describe one change you would make, one step at a time, to test whether a different extracted component changes the score.
Hints
- Start from the raw index and identify which step decides what the feature contains.
- R2 is a proportion of explained variance on held-out data, not a row-level accuracy count.
- Change only one stage of the pipeline between runs so that any change in the score can be attributed to that stage.
Key Takeaways
- Feature engineering selects and transforms raw data into features suitable for modeling, and it can significantly change how well a model performs on the same dataset.
- In the citibike example, only the hour component of the datetime index is extracted and reshaped into X_hour, which is the single feature the regressor is evaluated on.
- A test-set R2 of 0.60 means the model explains 60 percent of the variance in the target on held-out data, leaving 40 percent unexplained.
- Each step in the pipeline, from extraction to reshape to fitting to evaluation, is a point where the final score can depend on the result, so trace one step at a time when a score surprises you.